java基础:模拟ATM取款机
package com.atm; import java.util.Scanner; /** * ATM类实现 * * @author 向往的生活 */ public class ATM { public static void main(String[] args) { String[] user = new String[2]; String[] password = new String[2]; double[] amount = new double[2]; loginSelect(user, password, amount); } /** * 登录业务的菜单选择 * * @param user * @param password */ public static void loginSelect(String[] user, String[] password, double[] amount) { System.out.println("#####欢迎#####"); int n = 1; while (true) { System.out.println("A.开户"); System.out.println("B.登录"); System.out.println("C.退出"); System.out.println("D.修改密码"); Scanner scanner = new Scanner(System.in); System.out.println("选择菜单:"); String number = scanner.nextLine(); number = number.toUpperCase(); char[] chr = number.toCharArray(); switch (chr[0]) { case 'A': openAnAccount(user, password); loginSelect(user, password, amount); break; case 'B': int index = login(user, password);// 记录登录人的下标 if (index != -1) { select(user, password, amount, index); } else { n++;// 登录3次失败,则退出 if (n == 3) { break; } } break; case 'C': System.out.println("退出ATM"); System.exit(-1); break; case 'D': updatePassword(user, password); System.out.println("修改密码"); break; default: System.out.println("录入的操作错误!"); break; } } } /** * 核心业务菜单选择 * * @param user * @param password */ public static void select(String[] user, String[] password, double[] amount, int index) { while (true) { System.out.println("###########您好:" + user[index] + "#########"); System.out.println("A.存款"); System.out.println("B.取款"); System.out.println("C.转账"); System.out.println("D.查询账户余额"); System.out.println("E.返回主菜单"); Scanner scanner = new Scanner(System.in); System.out.println("选择菜单:"); String input = scanner.nextLine(); input = input.toUpperCase(); char chr = input.toCharArray()[0]; switch (chr) { case 'A': saveMoney(user, amount, index); break; case 'B': getMoney(user, amount, index); break; case 'C': transferAccounts(user, amount, index); break; case 'D': query(user, amount, index); break; case 'E': loginSelect(user, password, amount); break; default: System.out.println("操作错误请重新输入!"); break; } } } /** * 登录 * * @return */ public static int login(String[] user, String[] password) { Scanner input = new Scanner(System.in); System.out.println("请输入用户名:"); String userName = input.nextLine(); System.out.println("请输入密码:"); String pwd = input.nextLine(); // 通过userName数据,找到user下标, int index = -1; for (int i = 0; i < user.length; i++) { if (user[i] == null) { System.out.println("请去开户,您没有注册卡号!"); loginSelect(user, password, null); return -1; } else { if (user[i].equals(userName)) { index = i; break; } } } // 然后通过user下标找到password中相应的密码。 if (password[index].equals(pwd)) { return index; } else { System.out.println("用户名或者密码输入错误,请从新输入!"); } return -1;// 因为密码不匹配因此返回-1 } /** * 开户 * * @return */ public static void openAnAccount(String[] user, String[] password) { int index = -1; // 搜索下标 for (int i = 0; i < user.length; i++) { if (user[i] == null) { index = i; break; } } if (index == -1) {// 如果存储空间满了,则提示联系管理员处理 System.out.println("系统存储已经满了,请联系管理员处理!"); return; } // 新增用于与密码 Scanner scanner = new Scanner(System.in); System.out.println("输入登录名:"); String loginName = scanner.nextLine(); user[index] = loginName; System.out.println("输入密码:"); String pwdValue = scanner.nextLine(); password[index] = pwdValue; } /*************************************************************************** * 查询账户余额 * * @return */ public static void query(String[] user, double[] amount, int index) { System.out.println(user[index] + "您的余额为:" + amount[index]); } /** * 取款 * * @return */ public static void getMoney(String[] user, double[] amount, int index) { System.out.println("A 100 B 200 "); System.out.println("C 300 D 500 "); System.out.println("E 1000 F 2000 "); System.out.println("G 其他金额 "); Scanner scanner = new Scanner(System.in); System.out.println("选择取款金额:"); String money = scanner.nextLine(); money = money.toUpperCase(); double number = 0.0; char chr = money.toCharArray()[0]; switch (chr) { case 'A': number = 100; break; case 'B': number = 200; break; case 'C': number = 300; break; case 'D': number = 500; break; case 'E': number = 1000; break; case 'F': number = 2000; break; case 'G': Scanner input = new Scanner(System.in); System.out.println("输入取款金额:"); number = input.nextDouble(); break; default: System.out.println("您输入的金额错误!"); break; } // 检查存款的金额校验 if (!checkMoney(number)) { return; } if(amount[index] < number) { System.out.println("您账户余额不足,不能取款!"); return; } amount[index] = amount[index] - number; System.out.println("取款成功,余额为:" + amount[index]);// 显示余额 return; } /** * 检查存款的金额校验 * * @param number * @return */ public static boolean checkMoney(double number) { if (number < 100) { System.out.println("取款失败:只能取出大于等100的金额!"); return false; } // 只能存入100的整数 if (number % 100 != 0) { System.out.println("取款失败:只能取出100的整数!"); return false; } return true; } /** * 存款 */ public static void saveMoney(String[] user, double[] amount, int index) { Scanner scanner = new Scanner(System.in); System.out.println("输入存款金额:"); double number = scanner.nextDouble(); // 检查存款的金额校验 if (!checkMoney(number)) { return; } amount[index] = amount[index] + number; System.out.println("存款成功,余额为:" + amount[index]);// 显示余额 return; } /** * 转账 * * @return */ public static void transferAccounts(String[] user, double[] amount, int index) { int tragetIndex = -1; for (int i = 0; i < 3; i++) { tragetIndex = ifHaveUser(user); if (tragetIndex != -1) { break; } else {// 没找到相应的用户 System.out.println("第" + (i + 1) + "次,您输入的用户不存在!"); } } if (tragetIndex == -1) { System.out.println("超过3次返回上级菜单!"); return; } if (tragetIndex == index) {// 如果下标相等提示您不能给自己转账 System.out.println("您不能给自己转账"); } else { for (int i = 0; i < amount.length; i++) { Scanner scanner = new Scanner(System.in); System.out.println("输入您的转账金额:"); double tranAmount = scanner.nextDouble(); // 检查存款的金额校验 if (!checkMoney(tranAmount)) { return; } if (tranAmount > amount[index]) {// 如果转账金额大于,账户余额提示 System.out.println("您余额不足!"); break; } else { amount[index] = amount[index] - tranAmount; amount[tragetIndex] = tranAmount; break; } } } System.out.println("您的余额为:" + amount[index]); } /*************************************************************************** * * @return */ public static int ifHaveUser(String[] user) { Scanner scanner = new Scanner(System.in); System.out.println("输入转账用户:"); String userName = scanner.nextLine(); int index = -1; for (int i = 0; i < user.length; i++) { if (userName.equals(user[i])) { index = i; } } return index; } /*************************************************************************** * 修改密码 * * @return */ public static void updatePassword(String[] user, String[] password) { Scanner input = new Scanner(System.in); System.out.println("请输入用户名:"); String userName = input.nextLine(); while (true) { int index = -1; String oldPwd = null; for (int i = 0; i < user.length; i++) {//首先要找到当前用户 if (userName.equals(user[i])) { index = i; oldPwd = password[i]; } } if(index == -1) { System.out.println("还没有开户,请开户后再来修改密码!"); return ; } System.out.println("请输入旧密码:"); String inputOldPwd = input.nextLine(); System.out.println("请输入新密码:"); String newpwd = input.nextLine(); System.out.println("请输入确认密码:"); String confimpwd = input.nextLine(); if(!inputOldPwd.equals(oldPwd)) {//如果输入的旧密码不等于新密码,则重新输入 System.out.println("您输入的旧密码不正确!"); continue; } if(!newpwd.equals(confimpwd)) {//新密码与确认密码不匹配 System.out.println("新密码与确认密码不匹配!"); continue; } password[index] = newpwd; System.out.println("密码修改成功!"); break; } } }