Java基于控制臺界面實現(xiàn)ATM系統(tǒng)
本文實例為大家分享了Java實現(xiàn)ATM系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
這應該算最基礎的Javase項目了,但其中邏輯還是得想想的。
功能還算完善,只是對輸入數(shù)據(jù)的校驗沒做全,之后做web時再加上。
沒有數(shù)據(jù)庫。
完整代碼在最后。
流程圖
登錄
想模擬提款機插卡登錄的,因此沒做注冊賬號的功能,手動先塞了三個賬號。
有三次輸入密碼機會。
查詢
取款
存款
轉賬
修改密碼
退出
代碼
文件結構
下面兩個類復制過去放到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;//卡 鎖定狀態(tài) ? ? 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 "銀行:新航路銀行\(zhòng)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(); ? ? ? ? //根據(jù)卡號,判斷此賬號是否存在 ? ? ? ? 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.打印發(fā)票||"); ? ? ? ? ? ? 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("銀行:新航路銀行\(zhòng)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(); ? ? ? ? //根據(jù)卡號,判斷此賬號是否存在 ? ? ? ? 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(); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java Volatile應用單例模式實現(xiàn)過程解析
這篇文章主要介紹了Java Volatile應用單例模式實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11mybatis 多表關聯(lián)mapper文件寫法操作
這篇文章主要介紹了mybatis 多表關聯(lián)mapper文件寫法操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12Spring?Boot?Admin?添加報警提醒和登錄驗證功能的具體實現(xiàn)
報警提醒功能是基于郵箱實現(xiàn)的,當然也可以使用其他的提醒功能,如釘釘或飛書機器人提醒也是可以的,但郵箱報警功能的實現(xiàn)成本最低,所以本文我們就來看郵箱的報警提醒功能的具體實現(xiàn)2022-01-01Java 數(shù)據(jù)結構中二叉樹前中后序遍歷非遞歸的具體實現(xiàn)詳解
樹是一種重要的非線性數(shù)據(jù)結構,直觀地看,它是數(shù)據(jù)元素(在樹中稱為結點)按分支關系組織起來的結構,很象自然界中的樹那樣。樹結構在客觀世界中廣泛存在,如人類社會的族譜和各種社會組織機構都可用樹形象表示2021-11-11