Java实现基础银行ATM系统
时间:2022-12-05 12:54:14|栏目:JAVA代码|点击: 次
本文实例为大家分享了Java实现银行ATM系统的具体代码,供大家参考,具体内容如下
一、前言
银行ATM系列简单操作
二、使用步骤
1.创建用户信息类Account.java
代码如下(示例):
package ATM; public class Account { private String id; private String name; private double balance; private double norm; private String password; public Account() { } public Account(String id, String name, String password,double balance, double norm) { this.id = id; this.name = name; this.password=password; this.balance = balance; this.norm = norm; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getBalance() { return balance; } public void setBalance(double balance) { if(balance<0){ this.balance=0; } this.balance = balance; } public double getNorm() { return norm; } public void setNorm(double norm) { if(norm<=0||norm>50000){ this.norm=50000; } this.norm = norm; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2.编写用户界面和操作功能(代码中含有注释)
ATMSystem.java代码如下(示例):
package ATM; import java.util.*; public class ATMSystem { public static void main(String[] args) { //1.准备系统需要的容器对象,用于存储账户对象 ArrayList<Account> account=new ArrayList<>(100); //提前准备了,甄嬛用户信息,可删 ——自行注册 account.add(new Account("11111111","甄嬛","111",0.0, 30000)); //2.准备系统首页,登录和开户 //调用shouMain方法 shouMain(account); } //首页 public static void shouMain(ArrayList<Account> account){ Scanner sc=new Scanner(System.in); while(true){ System.out.println("=========欢迎您进入赵氏ATM系统========="); System.out.println("1.登录账户"); System.out.println("2.注册开户"); System.out.println("请输入命令(1、2)选择对应操作:"); String a1=sc.next(); switch(a1){ case "1": //登录 DL(account,sc); break; case "2": //开户 ZC(account,sc); break; default: System.out.println("错误:命令输入不规范!"); }} } //开户功能的实现 public static void ZC(ArrayList<Account> account,Scanner sc){ System.out.println("=========用户开户功能========="); System.out.println("请输入姓名:"); String name=sc.next(); String password=""; while(true){ System.out.println("请设置密码:"); password=sc.next(); System.out.println("请再输入一次密码:"); String againPassword=sc.next(); if(password.equals(againPassword)){ break; }else{ System.out.println("两次密码不一致,请重新设置密码!"); } } System.out.println("请输入取现限额:"); Double norm=sc.nextDouble(); if(norm<=0||norm>50000){ norm=50000.0; System.out.println("您的取现额度不规范,默认为50000元"); } String id=ID(account); Account accounts=new Account(id,name,password,0,norm); account.add(accounts); System.out.println("您的卡号为:"+id); } //随机生成八位数卡号不与已存在的卡号重复 //关键 !!!!!此方法与此方法下方的两个方法效果一样,任选其一 public static String ID(ArrayList<Account> account){ while(true){ Random r=new Random(); String id=""; for(int i=0;i<1;i++){ int q=r.nextInt(2); id+=q; }int T=0; for(int j=0;j<account.size();j++){ Account ac=account.get(j); if(ac.getId().equals(id)){ T=1; break; } } if(T==0){ return id; } } } //随机生成八位数卡号不与已存在的卡号重复 // public static String ID(ArrayList<Account> account){ // while(true){ // Random r=new Random(); // String id=""; // for(int i=0;i<8;i++){ // int q=r.nextInt(10); // id+=q; // } // //判断卡号是否重复了 // Account ac=pdid(account,id); // if(ac==null){ // //说明当前卡号没有重复 // return id; // } // } // public static Account pdid(ArrayList<Account> account,String id){ // int acount=0; // //如果有重复的返回重复的对象 // for(int i=0;i<account.size();i++){ // Account ac=account.get(i); // if(id.equals(ac.getId())){ // return ac; // } // } // return null; // } //登录 public static void DL(ArrayList<Account> account,Scanner sc){ //调用检查卡号的JC方法 Account acc=JC(account,sc); while(true){ //核对输入密码 System.out.println("请输入密码:"); String password=sc.next(); if(acc.getPassword().equals(password)){ //登录后页面//操作页面 System.out.println("恭喜您,"+acc.getName()+"成功进入系统!"); showUser(account,acc,sc); return; }else{ System.out.println("密码错误!"); } } } //检查登录卡号是否存在 public static Account JC(ArrayList<Account> account,Scanner sc){ while(true){ System.out.println("请输入卡号:"); String id=sc.next(); for(int i=0;i<account.size();i++){ Account acc=account.get(i); if(id.equals(acc.getId())){ return acc; }} System.out.println("警告:卡号错不存在!"); } } //用户操作页面 private static void showUser(ArrayList<Account> account,Account acc,Scanner sc) { 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.println("请输入命令(1、2、3、4、5、6、7)选择对应操作:"); String a2=sc.next(); switch(a2){ case "1"://查询 CX(acc); break; case "2"://存款 CK(acc,sc); break; case "3"://取款 QK(acc,sc); break; case "4"://转账 ZZ(account,acc,sc); break; case "5"://修改密码 XG(acc,sc); return; case "6"://退出 System.out.println("欢迎下次登录!"); return; case "7"://注销 SC(account,acc); return; default: System.out.println("错误:命令输入不规范!"); } } } //简单的删除该对象 private static void SC(ArrayList<Account> account,Account acc) { account.remove(acc); return; } //通过Account类中set方法修改对象的密码,可举一反三修改取款限额 private static void XG(Account acc, Scanner sc) { while(true){ System.out.println("请输入修改后的密码:"); String mima=sc.next(); System.out.println("请再输入一次:"); String mm=sc.next(); if(mima.equals(mm)){ acc.setPassword(mima); System.out.println("密码修改成功!"); System.out.println("请重新登录~~"); return; } } } //转账有账户个数限制,转账的卡号是否存在,并核对姓名,限制转账金额 //通过Account类中set方法修改对象的余额 private static void ZZ(ArrayList<Account> account,Account acc, Scanner sc) { if(account.size()<2){ System.out.println("当前系统,账户不足2个,不支持转账!"); return; }else{ while(true){ System.out.println("请您输入转账的账户卡号:"); String cards=sc.next(); for(int i=0;i<account.size();i++){ Account car=account.get(i); if(cards.equals(car.getId())&&!cards.equals(acc.getId())){ QR(car,sc); while(true){ System.out.println("请您输入转账的金额:"); double money=sc.nextDouble(); if(money>acc.getNorm()){ System.out.println("转账金额不能大于限额:"+acc.getNorm()+"元"); }else if(money>acc.getBalance()){ System.out.println("您的余额为:"+acc.getBalance()+"元,不支持大额度的转账金额!"); }else{ acc.setBalance(acc.getBalance()-money); car.setBalance(acc.getBalance()+money); System.out.println("转账成功,现在你的余额为:"+acc.getBalance()); break; } } break; }else{System.out.println("警告:卡号错,不存在!");}break; }break; } } } //确认转账对象的姓名,避免转错对象 private static void QR( Account car, Scanner sc) { String name="**"+car.getName().substring(2); while(true){ System.out.println("您确定要为"+name+"转账!"); System.out.println("请您输入对方姓名确认:"); String names=sc.next(); if(names.equals(car.getName())){ System.out.println("请继续转账"); break; }else{ System.out.println("姓名不符合"); } } } //取款是简单的修改余额,注意取款限额 private static void QK(Account acc, Scanner sc) { while(true){ System.out.println("输入您要取款的金额:"); double money=sc.nextDouble(); if(money>acc.getNorm()){ System.out.println("取款金额不能大于限额:"+acc.getNorm()+"元"); }else if(money>acc.getBalance()){ System.out.println("您的余额为:"+acc.getBalance()+"元,不支持大额度的取款金额!"); }else{ acc.setBalance(acc.getBalance()-money); System.out.println("取款成功,现在你的余额为:"+acc.getBalance()); break; } } } //存款是简单的修改余额 private static void CK(Account acc, Scanner sc) { System.out.println("输入您要存款的金额:"); double money=sc.nextDouble(); double balance=acc.getBalance(); double bal=money+balance; acc.setBalance(bal); System.out.println("存款成功~您现在的金额为"+bal+"元"); } //查询信息,通过Account类中get方法读出想知道的数据 private static void CX(Account acc) { System.out.println("您的帐户信息如下:"); System.out.println("卡号:"+acc.getId()); System.out.println("姓名:"+acc.getName()); System.out.println("余额:"+acc.getBalance()); System.out.println("当前取现额度:"+acc.getNorm()); } }