首页 > 代码库 > 简易ATM系统练习

简易ATM系统练习

执行简单的ATM功能:主要练习JavaOO
参考了阿里巴巴java内部规范,希望自己所书写的程序能够更加的规范性;


package com.lovo.ATM.bean; import java.util.Scanner;
/** * ATM系统类 * @author 侯熙 * @version 1.1 * @since jdk1.8.0_25 */ public class ATM { private UserInfo[] users; private UserInfo user; private int cash;//现金 public final int MAX_CASH = 200000; public ATM(){ this.users = new UserInfo[4]; for (int i = 0; i < users.length; i++) { this.users[i] = new UserInfo("li"+i,"123456"+i,251+400*i); } this.cash = 100000; } /** * 执行ATM运行,能够调用内部所以方法 */ public void run(){ this.welcome(); boolean flag = this.login(); if(flag){ while(true){ int choice = this.choiceMenu(); switch(choice){ case 1: this.queryMoney(); break; case 2: this.saveMoney(); break; case 3: this.getMoney(); break; case 4: this.changePwd(); break; case 5: this.quit(); break; default: System.out.println("没有该选项,请看清提示。"); } } }else{ System.out.println("三次登录失败,没收您的卡片。"); System.out.println("请带上身份证到银行柜台处理,谢谢合作。"); } } /** * 欢迎界面 */ private void welcome(){ System.out.println("======================================="); System.out.println("=================欢 迎================"); System.out.println("======使用爱存不存银行ATM=================="); System.out.println("=======================保证你爽!========"); System.out.println("======================================="); System.out.println("======================================="); } /** * 登录界面,执行登录操作共有三次机会 * @return 三次输入错误返回false,输入正确返回true */ private boolean login(){ for(int i = 0; i < 3; i++){ System.out.println("请输入用户名:"); String inputCardNum = new Scanner(System.in).nextLine(); System.out.println("请输入密 码:"); String inputPwd = new Scanner(System.in).nextLine(); for (int j = 0; j < users.length; j++) { if(inputCardNum.equals(this.users[j].getCardNum()) && inputPwd.equals(this.users[j].getPassword())){ System.out.println("登录成功,欢迎您---用户:" + inputCardNum); this.user = this.users[j]; return true; } } System.out.println("用户名/密码有误,您还有" + (2-i) + "次机会"); } return false; } /** * 主菜单界面,执行余额、存款、取款、修改密码、退出功能 * @return 返回值选择的功能(int) */ private int choiceMenu(){ int choice = 0; System.out.println("请选择您要执行的操作:"); System.out.println("1、查询余额;2、存款;3、取款;4、修改密码;5、退出。"); choice = new Scanner(System.in).nextInt(); return choice; } /** * 存钱操作: * 1、判断是否存入百元面值钞票 * 2、ATM机容量 * 3、执行存钱操作 */ private void saveMoney(){ System.out.println("请输入您要存入的金额:"); float account = new Scanner(System.in).nextFloat(); if (inspect(account)) { if (this.judgeAtmStatus(account)) { this.user.setAccount(this.user.getAccount() + account); System.out.println("存钱成功"); }else{ System.out.println("对不起,本机容量不足,请减少存入金额或换机操作"); } }else{ System.out.println("对不起,本机只接受百元面值的钞票"); } } /** * 取钱操作: * 1、判断取出金额是否正确 * 2、判断ATM内余额 * 3、执行取钱 */ private void getMoney(){ System.out.println("请输入您要取出的金额"); float account = new Scanner(System.in).nextFloat(); if (inspect(account)) { if (this.judgeAtmStatus(account)) { if (this.user.getAccount() >= account) { this.user.setAccount(this.user.getAccount() - account); System.out.println("请收好现金"); }else { System.out.println("对不起,您的卡余额不足,请查询确认"); } }else{ System.out.println("对不起,本机余额不足,请减少取出金额或换机操作"); } }else{ System.out.println("对不起,本机只接受百元面值的钞票"); } } /** * 查询余额 */ private void queryMoney(){ System.out.println("您的余额为:"); System.out.println(this.user.getAccount()); } /** * 修改密码操作 */ private void changePwd(){ System.out.println("请修改密码"); System.out.println("请输入原密码:"); if (this.judgePassword(new Scanner(System.in).nextLine())) { System.out.println("请输入新密码"); String setPassword = new Scanner(System.in).nextLine(); System.out.println("请再次输入密码"); if (setPassword.equals(new Scanner(System.in).nextLine())) { this.user.setPassword(setPassword); System.out.println("设置密码成功!"); return; } } System.out.println("设置密码失败"); } /** * 结束操作,退出系统 */ private void quit(){ System.out.println("谢谢使用,请收好您的卡片。"); System.exit(0);//关闭虚拟机 } /** * 判断存取金额是否是百元面值 * @param money 存取金额 * @return 是百元面值为true */ private boolean inspect(float money){ return money%100 == 0; } /** * 判断输入密码是否正确 * @param inputPwd 输入的密码 * @return密码正确为true */ private boolean judgePassword(String inputPwd){ for (int i = 0; i < users.length; i++) { if (this.user.getPassword().equals(inputPwd)) { return true; } } return false; } /** * * @param cash * @return */ private boolean judgeAtmStatus(float cash){ if (this.cash + cash >= 0 && this.cash + cash <= 200000) { this.cash += cash; return true; } return false; } } package com.lovo.ATM.bean; /** * ATM系统用户信息类 * @author 侯熙 * @version 1.0 * @since jdk1.8.0_25 */ public class UserInfo { private String cardNum;//卡号 private String password;//密码 private float account;//余额 public UserInfo() { // TODO Auto-generated constructor stub } public UserInfo(String cardNum, String password, float account) { super(); this.cardNum = cardNum; this.password = password; this.account = account; } public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public float getAccount() { return account; } public void setAccount(float account) { this.account = account; } } package com.lovo.ATM.test; import com.lovo.ATM.bean.*; public class TestMain { public static void main(String[] args) { // TODO Auto-generated method stub ATM atm = new ATM(); atm.run(); } }

 

简易ATM系统练习