Java基於控制臺界面實現ATM系統
本文實例為大傢分享瞭Java實現ATM系統的具體代碼,供大傢參考,具體內容如下
這應該算最基礎的Javase項目瞭,但其中邏輯還是得想想的。
功能還算完善,隻是對輸入數據的校驗沒做全,之後做web時再加上。
沒有數據庫。
完整代碼在最後。
流程圖
登錄
想模擬提款機插卡登錄的,因此沒做註冊賬號的功能,手動先塞瞭三個賬號。
有三次輸入密碼機會。
查詢
取款
存款
轉賬
修改密碼
退出
代碼
文件結構
下面兩個類復制過去放到ATM文件夾下就能運行。
package ATM; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Account { private String cardID; private String username; private String password; private double balance; private boolean status;//卡 鎖定狀態 public Account() { } public Account(String cardID, String username, String password, double balance, boolean status) { this.cardID = cardID; this.username = username; this.password = password; this.balance = balance; this.status = status; } public Account(String cardID, String username, String password, double balance) { this.cardID = cardID; this.username = username; this.password = password; this.balance = balance; } public String getCardID() { return cardID; } public void setCardID(String cardID) { this.cardID = cardID; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } @Override public String toString(){ System.currentTimeMillis(); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formatDate = sdf.format(date); return "銀行:新航路銀行\n" + "銀行賬號:"+cardID+"\n" +"用戶名:"+username+"\n" +"餘額:"+balance+"\n" +formatDate; } }
package ATM; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; public class ATM { static Scanner sc = new Scanner(System.in); static ArrayList<Account> accounts; public static void main(String[] args) { //模仿插卡ATM,因此沒做註冊用戶功能,這裡自己添加瞭幾個用戶做測試。 accounts = new ArrayList<>(); accounts.add(new Account("10001", "路飛", "ONEPIECE", 100,true)); accounts.add(new Account("10002", "索隆", "123456", 10000,false)); accounts.add(new Account("10003", "娜美", "123456", 1000000d,true)); //登錄 loginVerify(); } //登錄驗證 public static void loginVerify() { System.out.println("提示:有這些賬戶"); for (Account account : accounts) { System.out.println("卡號:"+account.getCardID() +" 用戶名:" + account.getUsername()+" 密碼:"+account.getPassword()+" 餘額:"+account.getBalance()); } System.out.println("---------------------------------------------"); //模擬插卡,手動輸入銀行卡號 System.out.print("輸入銀行卡號:"); String cardID = sc.next(); //根據卡號,判斷此賬號是否存在 for (int i = 0; i < accounts.size(); i++) { Account account = accounts.get(i); if (cardID.equals(account.getCardID())) { //判斷卡號是否存在 if(account.isStatus()) { //看該卡是否被鎖 //賬號存在,輸入密碼 System.out.print("請輸入密碼:"); int count = 3;//可輸入3次密碼 while (count > 0) { sc.useDelimiter("\n"); String pwd = sc.next(); if (pwd.equals(accounts.get(i).getPassword())) { //登錄成功,轉到主界面 mainInterface(accounts.get(i)); } else { if(--count == 0){ System.out.println("此卡已鎖,請到人工處咨詢辦理。"); accounts.get(i).setStatus(false); }else { System.out.println("還有" + count + "次輸入機會"); } } } }else{ System.out.println("此卡已鎖,請到人工處咨詢辦理。"); } break; } if(i == accounts.size()-1){ System.out.println("此卡號不存在!!"); } } } public static void mainInterface(Account account) { System.out.println("====================================="); System.out.println("|| 1.查詢餘額 5.修改密碼||"); System.out.println("|| 2.取款 6.退卡 ||"); System.out.println("|| 3.存款 ||"); System.out.println("|| 4.轉賬 ||"); System.out.println("====================================="); System.out.print("請輸入要操作的號碼:"); int i = sc.nextInt(); switch (i) { case 1: queryBalance(account); break; case 2: withdrawal(account); break; case 3: deposit(account); break; case 4: transfer(account); break; case 5: updatePassword(account); break; case 6: exitSystem(); break; default: System.out.println("輸入號碼不對!"); break; } } //查詢餘額 public static void queryBalance(Account account) { System.out.println("====================================="); System.out.println("|| "+account.getUsername() + "的餘額: " + account.getBalance()); System.out.println("====================================="); mainInterface(account); } //取款 public static void withdrawal(Account account) { System.out.print("請輸入取款金額:"); double money = sc.nextInt(); if (money <= account.getBalance()) { account.setBalance(account.getBalance() - money); System.out.println("取款成功!!"); System.out.println("====================================="); System.out.println("|| 1.返回主界面 2.打印發票||"); System.out.println("====================================="); int i = sc.nextInt(); switch (i) { case 1: mainInterface(account); break; case 2: Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formatDate = sdf.format(date); System.out.println("銀行:新航路銀行\n"+ "銀行卡:"+account.getCardID()+"\n"+ "用戶名:"+account.getUsername()+"\n"+ "本次取款:"+money+"\n"+ formatDate); mainInterface(account); break; } } else { System.out.println("oh,我可憐的孩子!餘額不足捏。"); System.out.println("====================================="); System.out.println("|| 1.返回 enter ||"); System.out.println("====================================="); sc.next(); mainInterface(account); } } //存款 public static void deposit(Account account) { System.out.print("請輸入要存款金額:"); double tempMoney = sc.nextDouble(); account.setBalance(account.getBalance() + tempMoney); System.out.println("嗶~嗶~嗶 點鈔中..."); System.out.println("存款成功"); mainInterface(account); } //轉賬 public static void transfer(Account account) { System.out.println("請輸入對方卡號(提示賬號有:10001 /10002 /10003)"); String cardID = sc.next(); //根據卡號,判斷此賬號是否存在 for (int i = 0; i < accounts.size(); i++) { if (cardID.equals(accounts.get(i).getCardID())) { System.out.print("請輸入對象用戶名驗證:"); sc.nextLine(); String next = sc.nextLine(); if(next.equals(accounts.get(i).getUsername())) { System.out.print("請輸入要轉的金額:"); double m = sc.nextDouble(); if (m > account.getBalance()) { System.out.println("你沒這麼多錢,轉賬失敗!"); break; } else { account.setBalance(account.getBalance() - m); accounts.get(i).setBalance(m + accounts.get(i).getBalance()); System.out.println("轉賬成功"); break; } }else{ System.out.println("賬號與用戶名不匹配,轉賬失敗"); break; } } if(i == accounts.size()-1) { System.out.println("此卡號不存在!!"); } } mainInterface(account); } //5. 修改密碼 public static void updatePassword(Account account){ System.out.print("請輸入新密碼:"); String s1 = sc.next(); System.out.print("請再次輸入新密碼:"); String s2 = sc.next(); if(s1.equals(s2)){ account.setPassword(s1); System.out.println("密碼修改成功,請重新登錄!"); loginVerify(); }else{ System.out.println("兩次密碼不一致"); mainInterface(account); } } //6. 退出 public static void exitSystem() { System.exit(0); //loginVerify(); } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。