您好,登錄后才能下訂單哦!
這篇文章主要講解了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; } }
看完上述內容,是不是對Java項目如何實現模擬ATM機有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。