需求分析
首先ATM银行系统包括两个基础大功能:开户和登陆账户(当然在系统中没有一个账户时不能登录,需要先开户)。
一名用户有6项基本信息描述:姓名、性别、银行卡号、银行卡密码、账户余额、取款限额。
在登录账户后又有7项功能:查询账户信息、存款、取款、转账、修改密码、退出、注销账户。
代码展示
// ATM.java public class ATM { private int id; private String name; private char sex; private double moneyLeft; private double moneyCon; private String code; public ATM() {} public ATM(int id, String name, char sex, double moneyLeft, double moneyCon, String code) { this.id = id; this.name = name; this.sex = sex; this.moneyLeft = moneyLeft; this.moneyCon = moneyCon; this.code = code; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public double getMoneyLeft() { return moneyLeft; } public void setMoneyLeft(double moneyLeft) { this.moneyLeft = moneyLeft; } public double getMoneyCon() { return moneyCon; } public void setMoneyCon(double moneyCon) { this.moneyCon = moneyCon; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
// ATMOperator.java public class ATMOperator { ArrayList<ATM> ATMlist = new ArrayList<>(); //开户 public void register() { ATM user = new ATM(); Scanner sc = new Scanner(System.in); System.out.println("请输入你的用户名"); String name = sc.next(); user.setName(name); while (true) { System.out.println("请输入你的性别,‘男’ 或 ‘女’ "); String tmp = sc.next(); char sex = tmp.charAt(0); if (sex != '男' && sex != '女') { System.out.println("请输入‘男’ 或 ‘女’"); } else { user.setSex(sex); break; } } while (true) { System.out.println("请输入你的密码"); String code = sc.next(); System.out.println("请确认你的密码"); String confirmCode = sc.next(); if (code.equals(confirmCode)) { user.setCode(code); break; } else { System.out.println("两次密码不一致,请重新输入密码"); } } System.out.println("请输入你的每次取款限额"); double moneyCon = sc.nextDouble(); user.setMoneyCon(moneyCon); String id = ""; Random r = new Random(); for (int i = 0; i < 7; i++) { int num = r.nextInt(10); id += num; } System.out.println("恭喜你,开户成功!卡号是:" + id); int id1 = Integer.parseInt(id); user.setId(id1); ATMlist.add(user); } //查询打印账户信息 public void print(int n) { System.out.println("----------" + ATMlist.get(n).getName() + "先生的账户信息----------"); System.out.println("账户卡号:" + ATMlist.get(n).getId()); System.out.println("账户名:" + ATMlist.get(n).getName()); System.out.println("性别:" + ATMlist.get(n).getSex()); System.out.println("余额:" + ATMlist.get(n).getMoneyLeft()); System.out.println("取款限额:" + ATMlist.get(n).getMoneyCon()); System.out.println("--------------------------------"); } //展示登录界面 public void showLogon(int user) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("请输入你需要办理的业务号"); System.out.println("1、查询打印账户信息"); System.out.println("2、存款"); System.out.println("3、取款"); System.out.println("4、转账"); System.out.println("5、修改密码"); System.out.println("6、退出"); System.out.println("7、注销账户"); String n = sc.next(); switch (n) { case "1": print(user); break; case "2": saveMoney(user); break; case "3": getMoney(user); break; case "4": transforAccounts(user); break; case "5": changePassword(user); break; case "6": return; case "7": unsubscribe(user); break; default: System.out.println("请输入正确的指令"); break; } } } //登录账户 public int logon() { if (ATMlist.size() == 0) { System.out.println("您还未开户,请先开户"); throw new RuntimeException("Exception occurred"); } Scanner sc = new Scanner(System.in); System.out.println("请输入你的卡号"); int id = sc.nextInt(); int tmp = -1; for (int i = 0; i < ATMlist.size(); i++) { if (ATMlist.get(i).getId() == id) { System.out.println(ATMlist.get(i).getName() + "先生,您已经成功登录账户"); tmp = i; break; } } if (tmp == -1) { System.out.println("您输入的卡号不存在"); throw new RuntimeException("Exception occurred"); } else { showLogon(tmp); return tmp; } } //存款 public void saveMoney(int user) { Scanner sc = new Scanner(System.in); System.out.println("请输入你要存的金额"); double saveMoney = sc.nextDouble(); double rs = saveMoney + ATMlist.get(user).getMoneyLeft(); System.out.println("存钱成功!您的余额为:" + rs); ATMlist.get(user).setMoneyLeft(rs); } //取款 public void getMoney(int user) { Scanner sc = new Scanner(System.in); System.out.println("请输入你要取的金额"); double money = sc.nextDouble(); if (money > ATMlist.get(user).getMoneyLeft()) { System.out.println("余额不足,取款失败"); } else { double rs = ATMlist.get(user).getMoneyLeft() - money; ATMlist.get(user).setMoneyLeft(rs); System.out.println("取款成功!你的余额为:" + ATMlist.get(user).getMoneyLeft()); } } //转账 public void transforAccounts(int user) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的转账对象的卡号"); int id = sc.nextInt(); for (int i = 0; i < ATMlist.size(); i++) { if (id == ATMlist.get(i).getId()) { System.out.println("请问是要给*" + ATMlist.get(i).getName().substring(1) + "转账吗?"); String tmp = sc.next(); char answer = tmp.charAt(0); if (answer == (ATMlist.get(i).getName().charAt(0))) { System.out.println("请输入你要转账的金额"); double money = sc.nextDouble(); if (money > ATMlist.get(user).getMoneyLeft()) { System.out.println("你的余额不足,转账失败"); return; } else { if (money > ATMlist.get(user).getMoneyCon()) { System.out.println("超出取款限额,请重试"); return; } else { double rs = ATMlist.get(user).getMoneyLeft() - money; ATMlist.get(user).setMoneyLeft(rs); double otherrs = ATMlist.get(i).getMoneyLeft() + money; ATMlist.get(i).setMoneyLeft(otherrs); System.out.println("转账成功!您的余额为:" + rs); } } } else { System.out.println("你输入的对方账户姓氏不正确,请重试"); return; } }else System.out.println("卡号输入有误,请重试"); } } //修改密码 public void changePassword(int user) { Scanner sc = new Scanner(System.in); System.out.println("确认为本人,请输入你的新密码"); String newcode = sc.next(); System.out.println("请确认新密码"); String confirmCode = sc.next(); if (newcode.equals(confirmCode)) { ATMlist.get(user).setCode(newcode); System.out.println("修改密码成功!请牢记密码"); } else { System.out.println("两次密码不一致,请重试"); return; } } //退出 public void exit() { ; } //注销账户 public void unsubscribe(int user) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("你确定要注销该账户?y/n"); String answer = sc.next(); if (answer.equals("y")) { System.out.println("你已成功注销账户"); ATMlist.remove(user); break; } else if (answer.equals("n")) { System.out.println("你选择了保留账户"); break; } else { System.out.println("请输入 y/n"); } } } public void start() { Scanner sc = new Scanner(System.in); while (true){ System.out.println("请选择你要进行的操作"); System.out.println(("1、开户")); System.out.println("2、登录账户"); System.out.println("0、退出系统"); String num = sc.next(); switch (num) { case "0": return; case "1": register(); break; case "2": int user = logon(); break; default: System.out.println("请输入正确的指令"); break; } } } }
// Test.java (测试用例) public class Test { public static void main(String[] args) { ATMOperator ts = new ATMOperator(); ts.start(); } }
场景测试
1、首先是在未开户情况下的登录账户操作:
2、开户
(一)性别乱填
(二)密码输入不一致
(三)正常开户
3、登录账户,但是登录密码错误
4、成功登录账户
a. 查询打印账户信息
b. 存款取款
c.转账(这里转账卡号输错情况写错了,截图懒得改了....)
转账需要2个用户,我先开2个用户,并给梨花存2000元:
现在我用梨花的账户给小王’转账2500:
余额不足,那我转1500:
超出梨花转账限额,那我转100:
成功!
d.修改密码
e.注销账户
以上就是所有基本功能!
感谢阅读!!