Java專案實現模擬ATM機
阿新 • • 發佈:2020-05-28
本文例項為大家分享了Java實現模擬ATM機的具體程式碼,供大家參考,具體內容如下
專案名稱
模擬ATM機
專案描述
簡單實現ATM機功能
程式碼實現
測試類
public class Test { //模擬多功能ATM機 public static void main(String[] args) { ATM atm = new ATM(); atm.opearte(); } }
主類:實現主方法
public class ATM { private Bank bank; public ATM(){ bank = new Bank(); } private void success(BankCard bankCard) { while (true) { System.out.println("請選擇:1、查詢餘額,2、取款,3、存款,4、轉賬,5、退卡"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); if (choice == 5) { return; } switch (choice) { case 1: int money = bank.printf(bankCard); System.out.println("餘額:" + money); break; case 2: System.out.println("請輸入取款金額:"); money = scanner.nextInt(); bankCard.withdraw(money); break; case 3: System.out.println("請輸入存款金額:"); money = scanner.nextInt(); bankCard.deposit(money); break; case 4: System.out.println("請輸入對方賬戶id:"); int id = scanner.nextInt(); BankCard card = bank.transfer(id); if(card != null){ System.out.println("請輸入轉賬金額:"); money = scanner.nextInt(); if(money <= bankCard.getMoney()){ bankCard.withdraw(money); card.deposit(money); }else { System.out.println("轉賬失敗"); } }else { System.out.println("未找到該賬戶"); } break; default: } } } private void login(){ //登入 System.out.println("請輸入賬號,密碼"); Scanner scanner = new Scanner(System.in); int id = scanner.nextInt(); int password = scanner.nextInt(); BankCard bankCard = bank.search(id,password); if(bankCard != null){ System.out.println("登入成功"); success(bankCard); }else { System.out.println("登入失敗,請註冊"); } } private void register(){ //註冊 System.out.println("請輸入賬號,密碼"); Scanner scanner = new Scanner(System.in); int id = scanner.nextInt(); int password = scanner.nextInt(); BankCard bankCard = bank.search(id,password); if(bankCard != null){ System.out.println("該賬號已存在,請直接登入"); }else { bank.addcard(id,password); System.out.println("註冊成功"); } } public void opearte() { while (true) { System.out.println("請選擇:1、登入,2、註冊,3、退出"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); if (choice == 3) { return; } switch (choice) { case 1: login(); break; case 2: register(); break; default: } } } }
銀行類:一個銀行對應多個ATM機
public class Bank { private BankCard[] bankCards; private final int INITSIZE = 10; private int size; public Bank(){ bankCards = new BankCard[INITSIZE]; size = 0; } public BankCard search(int id,int password){ //搜尋 BankCard[] bankCard = bankCards; for(int i=0;i<size;i++){ if(bankCards[i].getId() == id && bankCards[i].getPassword() == password){ return bankCard[i]; } } return null; } public void addcard(int id,int password){ //註冊 BankCard bankCard = new BankCard(id,password); if (size == bankCards.length){ bankCards = Arrays.copyOf(bankCards,bankCards.length * 2); } bankCards[size++] = bankCard; } public int printf(BankCard bankCard){ //顯示餘額 return bankCard.getMoney(); } public BankCard transfer(int id){ for(int i=0;i<size;i++){ if(bankCards[i].getId() == id){ return bankCards[i]; } } return null; } }
銀行卡類:不同賬戶的操作
public class BankCard { private int id; private int password; private int money; private int size = 0; public BankCard(int id,int password){ this.id = id; this.password = password; money = 0; } public int getId(){ return id; } public int getPassword(){ return password; } public int getMoney(){ return money; } public void withdraw(int money){ //取款 if(this.money >= money){ this.money -= money; }else { System.out.println("取款失敗"); } } public void deposit(int money){ //存款 this.money += money; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。