Java实现简单银行ATM功能
作者:小黑来修仙
这篇文章主要为大家详细介绍了Java实现银行ATM简单功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Java实现简单银行ATM功能的具体代码,供大家参考,具体内容如下
实现功能
1、用户需要通过输入银行卡号和密码才能进入ATM系统
2、用户可以在ATM中实现取款、存款、转账、余额查询、退出系统等功能
简单分析
1、创建User类(cardNo,identity,phone,username,password,balance(余额))
2、创建Bank类,主要实现初始化用户、用户登录、显示菜单、取款、存款、转账、余额查询、退出系统等功能。
代码实现
User.java
public class User { private String username; private int password; private String cardNo; private String identity; private String phone; private double blance; public User() { } public User(String username, int password, String cardNo, String identity, String phone, double blance) { this.username = username; this.password = password; this.cardNo = cardNo; this.identity = identity; this.phone = phone; this.blance = blance; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } public String getIdentity() { return identity; } public void setIdentity(String identity) { this.identity = identity; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public double getBlance() { return blance; } public void setBlance(double blance) { this.blance = blance; } }
Bank.java
public class Bank { //创建用户数组 private User[] users=new User[100]; //用户个数 private int size=0; //当前登录的用户 private User loginuser; public Bank() { initial();//初始化用户 } /** * 初始化用户 */ public void initial(){ User user1=new User("钟爱",12346,"6226789234023434","130324192309123074","13133565435",2312313); User user2=new User("冷冬",12354,"6226789234023567","1305472309123074","13446745675",2333); User user3=new User("小龙",9893,"6226789234023564","13032414575467457","13145745435",255553); User user4=new User("赵丽颖",6342,"6226789234029324","130324192647456774","13145675435",288883); User user5=new User("徐三哥",8445,"6226789234025487","1303241923456744","1457785435",28989); users[0]=user1; users[1]=user2; users[2]=user3; users[3]=user4; users[4]=user5; size=5; } /** * 用户登录 */ public User login(String cardNo,int password){ for (int i = 0; i <size ; i++) { if((users[i].getCardNo().equals(cardNo))&&(users[i].getPassword()==password)){ loginuser=users[i]; return users[i]; } } return null; } /** * 显示菜单 */ public void showMenus(){ Scanner input =new Scanner(System.in); do { System.out.println("**********************************欢迎进入ATM系统*****************************************"); System.out.println("*********************1 存款 2 取钱 3 转账 4 查询余额 5 修改密码 0 退出***********************"); int choice=input.nextInt(); switch (choice){ case 1: //取款 save(); break; case 2: //存钱 withdraw(); break; case 3: //转账 trans(); break; case 4: //查询余额 query(); break; case 5: //修改密码 revise(); break; case 0: //退出ATM系统 System.out.println("欢迎下次使用本系统"); return; } } while (true); } /** * 存款操作 */ public void save(){ System.out.println("请输入您的存款金额:"); Scanner input=new Scanner(System.in); int money=input.nextInt(); if(loginuser!=null){ if(money>0){ loginuser.setBlance(loginuser.getBlance()+money); System.out.println("存款成功"); System.out.println("您的当前余额为"+loginuser.getBlance()); }else{ System.out.println("输入错误!"); } }else{ System.out.println("您还未进行账户登录"); } } /** * 取款操作 */ public void withdraw(){ Scanner input=new Scanner(System.in); System.out.println("请输入你要取出的金额为:"); int money=input.nextInt(); if(loginuser!=null){ if(money>0&&money<=loginuser.getBlance()){ loginuser.setBlance(loginuser.getBlance()-money); System.out.println("取款成功"); System.out.println("您的当前余额为:"+loginuser.getBlance()); }else{ System.out.println("输入错误"); } }else{ System.out.println("您还未进行账户登录"); } } /** * 转账操作 */ public void trans(){ Scanner input=new Scanner(System.in); System.out.println("请输入要转入账户的卡号:"); String cardNo=input.next(); User nuser=null;//对方账户 boolean IsExit=false;//判断对方账户是否存在 for (int i = 0; i <size; i++) { if(users[i].getCardNo().equals(cardNo)){ IsExit=true;//检索到对方账户 nuser=users[i]; } } if(loginuser==nuser){ System.out.println("不可以自己给自己账户进行转账"); return; } if(loginuser!=null){ System.out.println("请输入要转账的金额:"); int money=input.nextInt(); if(money>0&&money<=loginuser.getBlance()){ loginuser.setBlance(loginuser.getBlance()-money); nuser.setBlance(nuser.getBlance()+money); System.out.println("转账成功"); System.out.println("您当前余额为"+loginuser.getBlance()); System.out.println(nuser.getBlance()); }else{ System.out.println("输入错误"); } }else{ System.out.println("请登录账户"); } } /** * 查询余额 */ public void query(){ System.out.println("您的余额为:"+loginuser.getBlance()); } /** * 修改密码 */ public void revise(){ Scanner input=new Scanner(System.in); System.out.println("请输入您的新密码"); int newpassword=input.nextInt(); for (int i = 0; i < size; i++) { if(users[i]==loginuser){ users[i].setPassword(newpassword); //进行修改 } } System.out.println("密码修改成功"); } }
BankSystem.java
public class BankSystem { public static void main(String[] args) { Scanner input=new Scanner(System.in); Bank bank=new Bank(); System.out.println("请输入您的卡号:"); String cardNo=input.next(); System.out.println("请输入您的密码:"); int password=input.nextInt(); User user = bank.login(cardNo, password);//用户登录 if(user!=null){ System.out.println(user.getUsername()+"登陆成功"); bank.showMenus(); }else{ System.out.println("登陆失败"); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。