這篇文章主要講解了Java項(xiàng)目如何實(shí)現(xiàn)模擬ATM機(jī),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供瀘溪企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為瀘溪眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
項(xiàng)目名稱(chēng)
模擬ATM機(jī)
項(xiàng)目描述
簡(jiǎn)單實(shí)現(xiàn)ATM機(jī)功能
代碼實(shí)現(xiàn)
測(cè)試類(lèi)
public class Test { //模擬多功能ATM機(jī) public static void main(String[] args) { ATM atm = new ATM(); atm.opearte(); } }
主類(lèi):實(shí)現(xiàn)主方法
public class ATM { private Bank bank; public ATM(){ bank = new Bank(); } private void success(BankCard bankCard) { while (true) { System.out.println("請(qǐng)選擇:1、查詢(xún)余額,2、取款,3、存款,4、轉(zhuǎn)賬,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("請(qǐng)輸入取款金額:"); money = scanner.nextInt(); bankCard.withdraw(money); break; case 3: System.out.println("請(qǐng)輸入存款金額:"); money = scanner.nextInt(); bankCard.deposit(money); break; case 4: System.out.println("請(qǐng)輸入對(duì)方賬戶(hù)id:"); int id = scanner.nextInt(); BankCard card = bank.transfer(id); if(card != null){ System.out.println("請(qǐng)輸入轉(zhuǎn)賬金額:"); money = scanner.nextInt(); if(money <= bankCard.getMoney()){ bankCard.withdraw(money); card.deposit(money); }else { System.out.println("轉(zhuǎn)賬失敗"); } }else { System.out.println("未找到該賬戶(hù)"); } break; default: } } } private void login(){ //登錄 System.out.println("請(qǐng)輸入賬號(hào),密碼"); 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("登錄失敗,請(qǐng)注冊(cè)"); } } private void register(){ //注冊(cè) System.out.println("請(qǐng)輸入賬號(hào),密碼"); 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("該賬號(hào)已存在,請(qǐng)直接登錄"); }else { bank.addcard(id,password); System.out.println("注冊(cè)成功"); } } public void opearte() { while (true) { System.out.println("請(qǐng)選擇:1、登錄,2、注冊(cè),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: } } } }
銀行類(lèi):一個(gè)銀行對(duì)應(yīng)多個(gè)ATM機(jī)
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
銀行卡類(lèi):不同賬戶(hù)的操作
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; } }
看完上述內(nèi)容,是不是對(duì)Java項(xiàng)目如何實(shí)現(xiàn)模擬ATM機(jī)有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。