Home>Article>Java> How to implement a simple console version of ATM system in Java

How to implement a simple console version of ATM system in Java

PHPz
PHPz forward
2023-05-03 17:10:07 1122browse

ATM system project

Function to be realized by the ATM system

1. Query: account must exist, password (three chances, if incorrect, the card will be locked)

2. Withdrawal: the account number must exist, the password (three chances, the card will be locked if it is incorrect), the withdrawal amount cannot be greater than the deposit

3. Deposit: the account number must exist, and the deposit amount cannot be less than 0

4. Transfer: Your account and transfer account must exist, password (three chances, if wrong, the card will be locked), the transfer amount cannot exceed the balance

5. Lock card: The account must exist, use the password to freeze

6. Unlock the card: The account must exist and can only be unlocked using the ID number

7. Replace the card: Use the ID card to replenish the card

8. Change the password: Password change To change the password, you can also use your ID card to change the password

9. Exit: Save data

Project analysis

can be divided into four objects, the first bank card Object (Card stores bank card information); the second user object (Person stores user information); the third functional object (Controller's specific operation function class); the view object (Views displays the operation interface). It can be divided into four categories

Code

Card class

encapsulates the attributes of the card (card number, password, balance, card lock status)

public class Card { //封装卡的属性 private String cardid; private String password; private double money; private boolean islock; //设置封装的各属性的set、get方法 public String getCardid() { return cardid; } public void setCardid(String cardid) { this.cardid = cardid; } 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 boolean isIslock() { return islock; } public void setIslock(boolean islock) { this.islock = islock; } //重写 to String方法 @Override public String toString() { return "Card [cardid=" + cardid + ", password=" + password + ", money=" + money + ", islock=" + islock + "]"; } //构造卡的无参和有参构造方法 public Card() { super(); } public Card(String cardid, String password, double money, boolean islook) { super(); this.cardid = cardid; this.password = password; this.money = money; this.islock = islock; } }

Person class

Encapsulates user information (user name, ID number, mobile phone number, card)

public class Person { private String name; private String userid; private String phone; private Card[] card; //设置封装的各属性的set、get方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Card[] getCard() { return card; } public void setCard(Card[] card) { this.card = card; } //重写 to String方法 @Override public String toString() { return "Person [name=" + name + ", userid=" + userid + ", phone=" + phone + ", card=" + card+ "]"; } //构造无参和有参方法 public Person() { super(); } public Person(String name, String userid, String phone, Card[] card) { super(); this.name = name; this.userid = userid; this.phone = phone; this.card = card; } }

Controller class

Create three default account information and construct the operation method

public class Controller { Scanner sc = new Scanner(System.in); Card c = new Card() ; //生成卡 //创建三组信息 Card c1 = new Card("1","666666",1000,false); //默认账户1 Card c2 = new Card("2","666666",1500,false); //默认账户2 Card c3 = new Card("3","666666",1800,false); //默认账户3 public void regiser(String name,String userid,String phone,Card[] card) { //将Person类的属性赋给对象p Person p = new Person(name,userid,phone,card); } public void query(Person p) { System.out.println(p); } public boolean verify(String cardid,String password) { if(cardid.equals("1")&&password.equals("666666")) { //如果输入的是账户1的账户密码,就得到账户1属性 c.setMoney(c1.getMoney()); c.setPassword(c1.getPassword()); if(!c.isIslock()) { //判断账户1是否被锁 return true; }else { System.out.println("抱歉,该卡已被锁"); return false; } } if(cardid.equals("2")&&password.equals("666666")) { //如果输入的是账户2的账户密码,就得到账户2属性 c.setMoney(c2.getMoney()); c.setPassword(c2.getPassword()); //判断该卡是否被锁 if(!c.isIslock()) { //判断账户2是否被锁 return true; } } if (cardid.equals("3")&&password.equals("666666")) { //如果输入的是账户3的账户密码,就得到账户3属性 c.setMoney(c3.getMoney()); c.setPassword(c3.getPassword()); if(!c.isIslock()) { //判断账户3是否被锁 return true; } } return false; //一个账户信息都没对上,重新输入 } //构造查询方法 public void query(String cardid) { if(cardid.equals("1")) { //查询账户1 System.out.println(c.getMoney()); }else if(cardid.equals("2")) { //查询账户2 System.out.println(c.getMoney()); }else if(cardid.equals("3")) { //查询账户3 System.out.println(c.getMoney()); }else { System.out.println("输入有误"); } } //构造取钱方法 public void putMoney() { System.out.print("请输入需要取出的金额(将金额再确认一遍):"); double money = sc.nextDouble(); //判断钱够不够 if(money<=c.getMoney()) { money = c.getMoney()-money; c.setMoney(money); //钱够取出并更新账户余额 System.out.println("已取出"+sc.nextDouble()+"元"); System.out.println("账户余额为:"+money+"元"); }else { System.out.println("对不起,账户余额不足"); } } //构造存钱方法 public void addMoney() { System.out.println("请将纸币放于存钞口(将金额再确认一遍)"); double money = sc.nextDouble(); money = c.getMoney()+money; c.setMoney(money); //存钱并更新账户余额 System.out.println("已存入"+sc.nextDouble()+"元"); System.out.println("账户余额为:"+money+"元"); } //构造转钱方法 public void saveMoney() { System.out.println("请输入需要转入账户名:"); String name = sc.next(); System.out.print("请输入转入金额(将金额再确认一遍):"); double money = sc.nextDouble(); //判断钱够不够 if(c.getMoney()>=money) { money = c.getMoney()-money; c.setMoney(money); //钱够转钱 更新账户余额 System.out.println("已转出"+sc.nextDouble()+"元"); System.out.println("账户余额为:"+money+"元"); }else { //钱不够返回 System.out.println("对不起,账户余额不足"); } } //构造锁卡方法 public void lock() { c.setIslock(true); System.out.println("该卡已被锁"); } //构造解卡方法 public void unlock() { c.setIslock(false); System.out.println("该卡可正常使用"); } //构造补卡方法 public void newCard() { System.out.println("请输入您的姓名:"); String name = sc.nextLine(); System.out.println("请输入您的身份证号码:"); String userid = sc.nextLine(); Person p = new Person(name,userid,null,null); System.out.println("请输入重办账户号码:"); String cardid = sc.nextLine(); System.out.println("请输入要设置的密码(数字):"); String password = sc.nextLine(); //限制密码位数 if(password.length()==6) { System.out.println("密码设置成功"); }else { System.out.println("密码限制6位,请重新选择功能"); } } //构造改密方法 public void changePwd() { System.out.println("请输入密码:"); String password = sc.next(); if(password.equals(c.getPassword())){ System.out.println("请输入更改后的密码:"); String newpassword = sc.next(); System.out.println("再输入一次密码:"); String newpassword1 = sc.next(); if(newpassword.equals(newpassword1)) { System.out.println("密码修改成功!"); c.setPassword(newpassword1); }else { System.out.println("两次密码输入不一致,请重新输入"); changePwd(); } }else { System.out.println(c.getPassword()); System.out.println("密码输入错误"); } } //构造退出方法 public void save() { System.exit(0); } }

View class

Implement the welcome interface and each operation interface

public class View { Scanner s = new Scanner(System.in); private int count = 3; Card c =new Card(); Controller co = new Controller(); public static void main(String[] args) { View v = new View(); v.show(); } public void show() { System.out.println("=============欢迎使用使用本行存取一体机============="); System.out.println("请输入卡号:"); String cardid = s.nextLine(); System.out.println("请输入密码:"); String password =s.nextLine(); //登录界面,账户密码错三次就锁卡 if(!co.verify(cardid, password)) { count--; if(count == 0) { co.lock(); //调用lock System.out.println("密码输入错误"+c.isIslock()); System.out.println("请重新输入:"); } System.out.println("你还有"+count+"次输入机会,"); System.out.println("输入机会用完后,将会锁卡"); show(); } //如果账户密码正确就进入功能界面 if(co.verify(cardid, password)) { while(true) { System.out.println("请按相应的序号选择功能:1.取款 2.存款 3.查询余额 4.转账 5.修改密码 6.锁卡 7.解卡 8.补卡 9.退出"); switch(s.nextInt()) { case 1: //取款 co.putMoney(); break; case 2: //存钱 co.addMoney(); break; case 3: //查询 co.query(cardid); break; case 4: //转钱 co.saveMoney(); break; case 5: //改密 co.changePwd(); break; case 6: //锁卡 co.lock(); break; case 7: //解卡 co.unlock(); break; case 8: //补卡 co.newCard(); break; case 9: //退出 System.out.println("感谢使用,欢迎下次光临"); co.save(); break; default: //输入指令错误 System.out.println("输入有误"); break; } } } } }

The above is the detailed content of How to implement a simple console version of ATM system in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete