ATM系统(Java)
1、实现要求
实现基本的ATM系统功能,包括注册,登录,查询,取款,存款,以及修改密码等。
2、代码实现
2.1 Test
package com.ham;public class Test {public static void main(String[] args) {ATM atm=new ATM();atm.start();}
}
2.2 Acoount
package com.ham;public class Account {private String carId;private String name;private char sex;private String password;private double money;private double limitMoney;public Account(String carId, String name, char sex, String password, double money, double limitMoney) {this.carId = carId;this.name = name;this.sex = sex;this.password = password;this.money = money;this.limitMoney = limitMoney;}public Account() {}public String getCarId() {return carId;}public void setCarId(String carId) {this.carId = carId;}public String getName() {return name+(sex=='男'?"先生":"女士");}public void setName(String name) {this.name = name;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}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 getLimitMoney() {return limitMoney;}public void setLimitMoney(double limitMoney) {this.limitMoney = limitMoney;}
}
2.3ATM
package com.ham;import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;public class ATM {private ArrayList<Account> acconuts=new ArrayList<>();private Scanner sc=new Scanner(System.in);//注册功能private void Register(){Account account =new Account();System.out.println("=======进入到开户操作=========");System.out.println("输入账号名");String name=sc.next();account.setName(name);while (true) {System.out.println("输入密码");String password=sc.next();System.out.println("输入确认密码");String password1=sc.next();if (password.equals(password1)){account.setPassword(password);break;}else {System.out.println("输入不正确");}}while (true) {System.out.println("输入性别");char sex=sc.next().charAt(0);if(sex=='男'||sex=='女'){account.setSex(sex);break;}else {System.out.println("输入不正确,重新输入");}}System.out.println("输入账号限额");double limitMoney=sc.nextDouble();account.setLimitMoney(limitMoney);//生成卡号String cardId=cardId();account.setCarId(cardId);//添加到Arrylistacconuts.add(account);System.out.println("恭喜您"+account.getName()+"开户成功,你的卡号是:"+cardId);}//登录功能private void Login(){if (acconuts.size()==0){System.out.println("用户未注册");return;}while (true) {System.out.println("请输入卡号");String CardId=sc.next();Account acc=getAccountCardId(CardId);if (acc!=null){while (true) {System.out.println("请输入密码");String pw=sc.next();if (pw.equals(acc.getPassword())){System.out.println("欢迎"+acc.getName()+"进入ATM系统,你的卡号是"+acc.getCarId());//登录系统界面showUser(acc);return;}else{System.out.println("密码错误");}}}else {System.out.println("输入账号不存在");}}}//展示登录后的操作界面private void showUser(Account acc){while (true) {System.out.println(acc.getName()+"你可以选择如下功能");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.println("请选择操作");String index=sc.next();switch (index){case "1"://查询账号shouAccount(acc);break;case "2"://存款AddMoney(acc);break;case "3"://取款downMoney(acc);break;case "4":break;case "5":updataPassword(acc);return;case "6":System.out.println(acc.getName()+"退出成功");return;case "7"://注销if (deleteUser(acc)){return;}break;default:System.out.println("你选择的操做不正确");}}}//展示账号信息private void shouAccount(Account acc){System.out.println("======信息如下======");System.out.println("卡号"+acc.getCarId());System.out.println("户主"+acc.getName());System.out.println("性别"+acc.getSex());System.out.println("余额"+acc.getMoney());System.out.println("最大限额"+acc.getLimitMoney());}//修改密码private void updataPassword(Account acc){System.out.println("=======修改密码==========");while (true) {System.out.println("请你输入当前密码");String pw=sc.next();if (pw.equals(acc.getPassword())){while (true) {System.out.println("请输入新密码");String pw1=sc.next();System.out.println("请重新输入新密码");String pw2=sc.next();if (pw1.equals(pw2)){acc.setPassword(pw2);System.out.println("密码修改成功,你的新密码是"+pw2);return;}else {System.out.println("输入密码不一致");}}}else {System.out.println("密码错误");}}}//销户private boolean deleteUser(Account acc){System.out.println("======销户操作======");System.out.println("你确定要销户吗? Y/N");String num=sc.next();switch (num){case "Y":if (acc.getMoney()!=0){System.out.println("你的账号不为零不能销户");return false;}else {acconuts.remove(acc);System.out.println("你的账号已经成功修销户");return true;}default:System.out.println("你的账号保留");return false;}}//取款private void downMoney(Account acc){System.out.println("=====取钱操作=======");if(acc.getMoney()<100){System.out.println("你的账号余额不足");return;}while (true) {System.out.println("请输入取款金额");double money=sc.nextDouble();if (acc.getMoney()>=100){if (money>acc.getLimitMoney()){System.out.println("你的取款金额超过限额"+acc.getLimitMoney());}else {acc.setMoney(acc.getMoney()-money);System.out.println("你取款金额为"+money+"账号余额为"+acc.getMoney());break;}}else {System.out.println("你的账号余额不足,账户余额为"+acc.getMoney());}}}//存款private void AddMoney(Account acc){System.out.println("=====存钱操作=======");System.out.println("请输入存款金额");double num=sc.nextDouble();double num1=acc.getMoney();acc.setMoney(num+num1);System.out.println("存款成功,你的存款金额为"+num+"总金额为"+acc.getMoney());}//生成卡号private String cardId(){while (true) {String cardId="";Random rd=new Random();for (int i=0;i<8;i++){cardId+=rd.nextInt(10);}//判断cardId重复Account acc=getAccountCardId(cardId);if (acc==null){return cardId;}}}//查卡号是否重复private Account getAccountCardId(String cardId){for (int i=0;i<acconuts.size();i++){Account acc=acconuts.get(i);if (cardId.equals(acc.getCarId())){return acc;}}return null;}//启动public void start(){while (true) {System.out.println("======欢迎进入ATM系统=========");System.out.println("1.用户登录");System.out.println("2、用户开户");System.out.println("请选择");String index =sc.next();switch (index){case "1":Login();break;case "2":Register();break;default:System.out.println("输入的不合法");}}}}
3、实现结果