欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

文件操作代替数据库模拟的ATM机

时间:2023-08-03
1.账户类:用来存放账户的卡号,密码和余额等信息,简单封装一下。然后设置了一个表示状态的字段,只有在字段的状态为true时才可以修改密码,存取款等操作。

package allclass;import java.io.Serializable;public class Account implements Serializable{ private String cardid; private String password; private boolean userstatus=false; private double surplus=0.0; public Account() { } public Account(String cardid, String password) { this.cardid = cardid; this.password = password; } public String getCardid() { return cardid; } public void setCardid(String cardid) { this.cardid = cardid; } private String getPassword() { return password; } public void setPassword(String password) { if(this.userstatus) this.password = password; else System.out.println("请先登录!"); } public boolean getUserstatus() { return userstatus; } public void setUserstatus(boolean userstatus) { this.userstatus = userstatus; } public double getSurplus() { if(this.userstatus) return surplus; else{ System.out.println("请先登录!"); return 0; } } public void setSurplus(double surplus) { if(this.userstatus) this.surplus = surplus; else System.out.println("请先登录!"); } public boolean matchpassword(String password){ if(this.getPassword().equals(password)){ return true; } else{ return false; } }}

2.线程任务类:主要用来模拟和用户的交互,两个while循环来模拟登录/注册操作和存取款操作,用用户的卡号作为文件名,创建文件,并利用io流将对象写入文件,保存此账户类。注册后登录,校验用户输入的账号密码和文件中存储的是否一致。

package allclass;import java.io.*;import java.util.Scanner;public class UserAccess implements Runnable { @Override public void run() { boolean usestatus=true; Scanner userinput=new Scanner(System.in); while(usestatus){ System.out.println("登录请按1"+"n"+"注册请按2"+"n"+"退出请按3"); String options123=userinput.next(); //用户登录; System.out.println("正常输入"); if(options123.equals("1")){ System.out.println("请输入卡号:"); String userid=userinput.next(); System.out.println("请输入密码:"); String userpassword=userinput.next(); ObjectInputStream in = null; try { //获取密码,匹配密码; in = new ObjectInputStream(new FileInputStream("userdata\"+userid)); Account user=(Account) in.readObject(); in.close(); if (user.matchpassword(userpassword)){ System.out.println("登陆成功"); user.setUserstatus(true); //标记账号为登录状态,并且开始执行用户操作。 ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("userdata\"+userid)); while(user.getUserstatus()){ System.out.println("存钱请按------>1"+"n"+"取钱请按------>2"+"n"+"修改密码请按--->3"+"n"+"余额查询请按--->4n"+"推出请按------>5"); String useraction123=userinput.next(); if (useraction123.equals("1")){ System.out.println("请输入存入数额(元):"); double savemoney=userinput.nextDouble(); user.setSurplus(user.getSurplus()+savemoney); System.out.println("存钱成功"); } else if(useraction123.equals("2")){ System.out.println("请输入取款数额(元):"); double takemoney=userinput.nextDouble(); user.setSurplus(user.getSurplus()-takemoney); System.out.println("取款成功"); } else if (useraction123.equals("3")){ System.out.println("请输入新密码:"); String newpassword=userinput.next(); user.setPassword(newpassword); System.out.println("修改成功"); } else if (useraction123.equals("5")){ user.setUserstatus(false); } else{ System.out.println(user.getSurplus()); } } out.writeObject(user); out.close(); } else{ continue; } } catch (Exception e) { e.printStackTrace(); } } //用户注册; else if(options123.equals("2")){ System.out.println("请设置卡号:"); String userid=userinput.next(); System.out.println("请设置密码:"); String userpassword=userinput.next(); Account newuser=new Account(userid,userpassword); try { ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("userdata\"+userid)); out.writeObject(newuser); System.out.println("注册成功,请牢记您的账号密码!"); out.close(); } catch (IOException e) { e.printStackTrace(); } } //退出; else{ usestatus=false; continue; } } }}

3.主类:将任务线程放入线程池,开始运行线程。

package allclass;import java.util.Timer;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class AtuoBank { public static void main(String[] args){ ExecutorService atm=new ThreadPoolExecutor(1,2,3, TimeUnit.DAYS,new ArrayBlockingQueue<>(5)); UserAccess zxr=new UserAccess(); atm.execute(zxr); }}

                                                                                                                            by:河师大小菜狗

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。