Java模拟实现ATM机
Java模拟ATM机,供大家参考,具体内容如下
实现登录,查询,转账,取款,修改密码,退出功能。
源码
package bank;import java.io.*;import java.util.Scanner;//ATM类public class Atm { private String[] user;//用户全部信息 private double money;//修改钱数 private double userMoney;//用户的钱 private String newPassword; private String userInFo; private int index; private int a =0; private int count = 10; public void show(){//显示界面 index = logIn(); if(index != -1){ working(); } } private String[] newStringUser(String[] str){ count=count+10; String[] newUser = new String[count]; for(int i=0;i<a;i++) newUser[i] = str[i]; return newUser; } private void getUser(){//从文件获取全部用户 String str; String[] strings = new String[count]; File file = new File('srcbankuser'); FileReader fileReader = null; BufferedReader bufferedReader = null; try{ fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); while((str = bufferedReader.readLine())!=null){ if(a<=count) strings[a++] = str; else strings = newStringUser(strings); } user = new String[a]; for(int i=0;i<a;i++) user[i] = strings[i]; strings = null; }catch(Exception e){ e.printStackTrace(); if((fileReader!=null)&&(bufferedReader!=null)){ try { bufferedReader.close(); fileReader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } private int logIn(){//用户登录 getUser(); String name,password,user; String[] a; int i = 0; int number = -1; Scanner input = new Scanner(System.in); a:while(i<3){ System.out.println('请输入用户名:'); name = input.nextLine(); System.out.println('请输入用户密码:'); password = input.nextLine(); user = name + '*' + password; for(int j=0;j<this.user.length;j++){ a = this.user[j].split('*'); userInFo = a[0]+'*'+a[1]; if(userInFo.equals(user)){ number = j; break a; } } i++; System.out.println('账户或密码错误请重新输入。。。'); } if(number!=-1){ System.out.println('登录成功'); try{ Thread.sleep(1000); }catch(Exception e){ e.printStackTrace(); } } else System.out.println('您已输入错误三次,卡已被吞!请到银行柜台询问!'); return number; } private int anthorLogin(){//查询转账用户是否存在 Scanner input = new Scanner(System.in); String antherUserName; String[] a; int x=-1; System.out.println('请输入要转账的用户名:'); antherUserName = input.nextLine(); for(int i=0;i<user.length;i++){ a = this.user[i].split('*'); if(a[0].equals(antherUserName)){ x=i; break; } } return x; } private void show1(){ System.out.println('**********************'); System.out.println('t欢迎使用ATM'); System.out.println('1,账户余额查询n2,存钱n3,取钱n4,转账n5,修改用户密码n6,退出系统n'); System.out.println('**********************'); } private void changeUser(int x){//改变用户数组里的数据 String[] str = user[index].split('*'); if(x==1) user[index] = str[0]+'*'+newPassword+'*'+str[2]; else user[index] = str[0]+'*'+str[1]+'*'+userMoney; } private void working(){//atm办理的业务 String number; setMoney(); do{ show1(); System.out.println('请输入要办理的业务序号:'); Scanner input = new Scanner(System.in); number = input.nextLine(); switch(number){ case '1': look(); break; case '2': saveMoney(); break; case '3': getMoney(); break; case '4': giveMoney(); break; case '5': changePassword(); break; case '6': System.out.println('欢迎下次光临!'); write(); break; default: System.out.println('您输入有误,请重新输入。。。。'); } }while(!number.equals('6')); } private void setMoney(){ String u = user[index]; userMoney = Double.parseDouble(u.split('*')[2]); } private void look(){//办理查看余额业务 System.out.println('用户余额为:'+userMoney); try{ Thread.sleep(2000); }catch(Exception e){ e.printStackTrace(); } } private void saveMoney(){//办理存钱业务 money = howMuch('存钱'); userMoney = userMoney+money; changeUser(2); look(); if(isContinue()) saveMoney(); } private void getMoney(){//办理取钱业务 money = howMuch('取钱'); if(money <= userMoney){ userMoney = userMoney-money; changeUser(2); look(); if(isContinue()) getMoney(); } else System.out.println('您的余额不足!'); } private void giveMoney(){//办理转账业务 int anthorIndex = anthorLogin(); if(anthorIndex!=-1){ money = howMuch('转账'); if(money <= userMoney){ userMoney = userMoney - money; changeUser(2); String anthorUser = user[anthorIndex]; String[] str =anthorUser.split('*'); double money1 = Double.parseDouble(str[2]); money = money + money1; user[anthorIndex] = str[0]+'*'+str[1]+'*'+money; System.out.println('转账成功!'); look(); } else System.out.println('您的余额不足!'); } else System.out.println('该用户不存在。。。。'); } private double howMuch(String str){ System.out.println('欢迎办理'+str+'业务。。。。。。'); System.out.println('请输入金额(只能是整数且是100的倍数,最多为10000):'); Scanner input = new Scanner(System.in); double money = input.nextDouble(); if(money%10==0) return money; else{ System.out.println('您输入有误!'); return 0.0; } } private void changePassword(){//办理修改密码业务 System.out.println('请输入新密码:'); Scanner input = new Scanner(System.in); newPassword = input.nextLine(); changeUser(1); System.out.println('密码修改成功!'); } private boolean isContinue(){ System.out.println('是否继续办理该项业务?(请输入Y(y)/N(n))'); Scanner input = new Scanner(System.in); String str = input.nextLine(); if(str.equalsIgnoreCase('y')) return true; else return false; } private void write(){ String str = ''; String s; for(int i=0;i<user.length;i++){ s = user[i]; if(i!=user.length-1) str = str + s + 'n'; else str = str + s; } File file = new File('srcbankuser'); FileWriter out = null; try { out = new FileWriter(file); out.write(str); out.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
package bank;//银行类public class Bank { private Atm atm = new Atm(); public void welcome(User user){ System.out.println('欢迎使用atm'); user.useAtm(atm); }}
package bank;//用户类public class User { public void useAtm(Atm atm){ atm.show(); }}
//创建user文件当数据库张三*456*100.0李四*123*300.0王五*789*200.0
package bank;//测试类public class Text { public static void main(String[] args){ Bank bank =new Bank(); User user = new User(); bank.welcome(user); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章:
1. IDEA 重新导入依赖maven 命令 reimport的方法2. Docker究竟是什么 为什么这么流行 它的优点和缺陷有哪些?3. idea打开多个窗口的操作方法4. Intellij IDEA 阅读源码的 4 个绝技(必看)5. JavaScript实现网页版五子棋游戏6. Java14发布了,再也不怕NullPointerException了7. 如何通过vscode运行调试javascript代码8. IntelliJ IDEA 统一设置编码为utf-8编码的实现9. IntelliJ IDEA 2020.2正式发布,两点多多总能助你提效10. IntelliJ IDEA设置编码格式的方法
