ATM取款机主函数
package ATM;import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;public class ATMSystem {public static void main(String[] args) {ArrayList<Account> accounts = new ArrayList<>();Scanner sc = new Scanner(System.in);showMain(accounts, sc);}public static void showMain(ArrayList<Account> accounts, Scanner sc) {while (true) {System.out.println("请您输入您想要做的操作:");System.out.println("1、登录账户");System.out.println("2、注册账户");System.out.print("您可以输入命令了:");int command = sc.nextInt();switch (command) {case 1:login(accounts, sc);break;case 2:register(accounts, sc);break;default:System.out.println("您当前输入的操作指令不被支持!");}}}private static void register(ArrayList<Account> accounts, Scanner sc) {System.out.println("===============用户开户功能==============");System.out.println("请您输入开户名称:");String password = "";String name = sc.next();while (true) {System.out.println("请您输入用户密码:");password=sc.next();System.out.println("请您输入确认密码:");String okpassword = sc.next();if (okpassword.equals(password)) {break;} else {System.out.println("两次密码必须一致---");}}System.out.println("请您输入每次限额:");double quotaMoney = sc.nextDouble();String cardId = createCarId(accounts);Account account = new Account(cardId, name, password, quotaMoney);accounts.add(account);System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCarId() + "请您妥善保管");}public static String createCarId(ArrayList<Account> accounts) {while (true) {String cardId = "";Random r = new Random();for (int i = 0; i < 8; i++) {cardId += r.nextInt(10);}Account acc = getAccountBuCardId(cardId, accounts);if (acc == null) {return cardId;}}}public static Account getAccountBuCardId(String cardId, ArrayList<Account> accounts) {for (int i = 0; i < accounts.size(); i++) {Account acc = accounts.get(i);if (acc.getCarId().equals(cardId)) {return acc;}}return null;}public static void login(ArrayList<Account> accounts, Scanner sc) {if (accounts.size() == 0) {System.out.println("当前系统无任何账号,您需要先注册!");return;}while (true) {System.out.println("请您输入登录卡号:");String cardId = sc.next();Account acc = getAccountBuCardId(cardId, accounts);if (acc != null) {System.out.println("请您输入登录的密码:");String password = sc.next();if (acc.getPassWord().equals(password)) {System.out.println("恭喜您," + acc.getUserName() + "先生成功进入系统,您的卡号是:" + acc.getCarId());showUserCommand(sc, acc, accounts);return;} else {System.out.println("您的密码有误,请确认!");}} else {System.out.println("对不起,不存在该卡号的账号!");}}}private static void showUserCommand(Scanner sc, Account acc, ArrayList<Account> accounts) {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、注销账户");System.out.printf("请您输入操作命令:");int comand = sc.nextInt();switch (comand) {case 1:showAccount(acc);break;case 2:deposit(acc, sc);break;case 3:withdrawal(acc, sc);break;case 4:transfer(acc, sc, accounts);break;case 5:changePassword(acc, sc, accounts);return;case 6:System.out.println("欢迎下次光临!!");return;case 7:accounts.remove(acc);System.out.println("销户成功!");return;default:System.out.println("您输入的命令有误");}}}private static void changePassword(Account acc, Scanner sc, ArrayList<Account> accounts) {System.out.println("========欢迎您进入修改密码界面========");while (true) {System.out.println("请您输入当前账户的密码:");String password = sc.next();if (acc.getPassWord().equals(password)) {while (true) {System.out.println("请你输入新的密码:");String password1 = sc.next();System.out.println("请您确认密码:");String password2 = sc.next();if (password1.equals(password2)) {acc.setPassWord(password1);System.out.println("恭喜您,密码修改成功,请重新登录");return;} else {System.out.println("两次密码输入不一致,请重新输入:");}}} else {System.out.println("您输入的密码错误,请重新输入:");}}}private static void transfer(Account acc, Scanner sc, ArrayList<Account> accounts) {System.out.println("======欢迎您进入黑马银行用户转账界面======");if (accounts.size() < 2) {System.out.println("当前系统,账户不足2个,不能转账!");return;}if (acc.getMoney() <= 0) {System.out.println("您自己都没钱,就别转了!");return;} else {while (true) {System.out.println("请您输入转账的账户卡号:");String cardId = sc.next();Account acct = getAccountBuCardId(cardId, accounts);if (acc.getCarId().equals(acct.getCarId())) {System.out.println("您不可以为自己转账!");}if (acct == null) {System.out.println("您输入的卡号有误,请重新输入");continue;} else {System.out.println("您当前要为【*" + acct.getUserName().substring(1) + "】转账!");while (true) {System.out.println("请您输入姓氏确认:");String name = sc.next();if (acct.getUserName().charAt(0) == name.charAt(0)) {System.out.println("请您确认是否转账:");String s = sc.next();if (s.equals("是"))transferMoney(acc, acct, sc);elseSystem.out.println("您已退出转账系统:");return;} else {System.out.println("输入姓氏错误,请您重新输入:");}}}}}}private static void transferMoney(Account acc, Account acct, Scanner sc) {while (true) {System.out.println("请您输入转账的金额");double money = sc.nextDouble();if (money > acc.getQuotaMoney())System.out.println("您当前转账超过了当前限额!最多可转:" + acc.getQuotaMoney());else {if (money < acc.getMoney()) {acc.setMoney(acc.getMoney() - money);System.out.println("恭喜您,转账" + money + "成功!当前账户还剩余:" + acc.getMoney());return;} else {System.out.println("您的余额不足,最多可转账:" + acc.getMoney());}}}}private static void withdrawal(Account acc, Scanner sc) {System.out.println("======欢迎您进入黑马银行用户取款界面======");if (acc.getMoney() < 100) {System.out.println("账户余额不足100元,就别取啦吧!");return;} else {while (true) {System.out.println("请您输入取款的金额");double money = sc.nextDouble();if (money > acc.getQuotaMoney())System.out.println("您当前取款超过了当前限额!最多可取:" + acc.getQuotaMoney());else {if (money < acc.getMoney()) {acc.setMoney(acc.getMoney() - money);System.out.println("恭喜您,取款" + money + "成功!当前账户还剩余:" + acc.getMoney());return;} else {System.out.println("您的余额不足,最多可取:" + acc.getMoney());}}}}}private static void deposit(Account acc, Scanner sc) {System.out.println("======欢迎您进入黑马银行用户存款界面======");System.out.println("请您输入存款的金额");double money = sc.nextDouble();acc.setMoney(acc.getMoney() + money);System.out.println("存款完成!!");showAccount(acc);}private static void showAccount(Account acc) {System.out.println("================当前账户详情==================");System.out.println("卡号:" + acc.getCarId());System.out.println("姓名:" + acc.getUserName());System.out.println("余额:" + acc.getMoney());System.out.println("当次限额:" + acc.getQuotaMoney());}
}
ATM取款机对象类
package ATM;public class Account {private String carId;private String userName;private String passWord;private double money;private double quotaMoney;public Account() {}public Account(String carId, String userName, String passWord, double quotaMoney) {this.carId = carId;this.userName = userName;this.passWord = passWord;this.quotaMoney = quotaMoney;}public String getCarId() {return carId;}public void setCarId(String carId) {this.carId = carId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}public double getQuotaMoney() {return quotaMoney;}public void setQuotaMoney(double quotaMoney) {this.quotaMoney = quotaMoney;}
}