Java仿ATM操作系統(tǒng)

image.png

前言

本文主要介紹使用Java簡單實現(xiàn)仿ATM操作系統(tǒng)。
該ATM系統(tǒng)主要包括:登錄、開戶、查詢、存款、取款、轉(zhuǎn)賬、修改密碼、退出、注銷賬戶功能。
該ATM系統(tǒng)主要使用while循環(huán)來實現(xiàn)各項功能的循環(huán)操作。
結(jié)構(gòu)組成:賬戶實體類和主系統(tǒng)操作類。

一、賬戶實體Account類

該類主要包含賬戶需要的所有字段和存取款的操作方法。

1. 定義字段

定義字段并實現(xiàn)setter和getter方法。

public class Account {
    // 卡號
    private String cardId;
    // 用戶名
    private String userName;
    // 密碼
    private String password;
    // 余額
    private double money;
    // 單次取款限額
    private double quotaMoney;

    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 getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public double getQuotaMoney() {
        return quotaMoney;
    }

    public void setQuotaMoney(double quotaMoney) {
        this.quotaMoney = quotaMoney;
    }
}

2 重寫構(gòu)造方法

    public Account(String cardId, String userName, String password, double quotaMoney) {
        this.cardId = cardId;
        this.userName = userName;
        this.password = password;
        this.quotaMoney = quotaMoney;
    }

3 存款

    public void inMoney(double money) {
        this.money += money;
    }

4 取款

0:取款額大于余額
1:取款成功
2:取款額大于單次取款限額

    public int outMoney(double money) {
        if (money > this.money) {
            return 0;
        } else if (money > this.quotaMoney) {
            return 2;
        } else {
            this.money -= money;
            return 1;
        }
    }

二、主系統(tǒng)操作ATMSystem類

該類包括ATM系統(tǒng)的所有操作功能的實現(xiàn)。主要包括系統(tǒng)主頁面和登錄之后個人頁面。

1 系統(tǒng)主頁面

系統(tǒng)主頁面主要實現(xiàn)登錄和開戶的功能。

1.1 啟動

本系統(tǒng)使用List集合來存儲賬戶信息。

public class ATMSystem {
    public static void main(String[] args) {
        // 存儲賬戶信息
        List<Account> accounts = new ArrayList<>();
        start(accounts);
    }

    /* 起始主頁 */
    public static void start(List<Account> accounts) {
        System.out.println("==========歡迎您進入ATM系統(tǒng)==========");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1、登錄");
            System.out.println("2、開戶");
            System.out.println("請您選擇操作:");
            int type = scanner.nextInt();
            switch (type) {
                case 1:
                    login(scanner, accounts);
                    break;
                case 2:
                    register(scanner, accounts);
                    break;
                default:
                    System.out.println("您選擇的操作有誤。");
            }
        }
    }
}

1.2 登錄

實現(xiàn)邏輯:
1、判斷系統(tǒng)中賬戶個數(shù)。
2、通過輸入的卡號查詢對應(yīng)賬戶進行判斷。
3、判斷輸入的密碼。
4、進入個人主頁面。

    /* 登錄 */
    public static void login(Scanner scanner, List<Account> accounts) {
        System.out.println("==========歡迎您進入到登錄操作==========");
        if (accounts.size() == 0) {
            System.out.println("當前系統(tǒng)無任何賬戶,請先開戶再登錄!");
            return;
        }
        Account account = null;
        while (true) {
            System.out.println("請您輸入卡號:");
            String cardId = scanner.next();
            account = queryAccountByCardId(accounts, cardId);
            if (account == null) {
                System.out.println("您輸入的卡號有誤!");
            } else {
                break;
            }
        }
        while (true) {
            System.out.println("請您輸入密碼:");
            String password = scanner.next();
            if (account.getPassword().equals(password)) {
                break;
            } else {
                System.out.println("您輸入的密碼有誤!");
            }
        }
        System.out.println("尊敬的" + account.getUserName() + ",歡迎您進入系統(tǒng)!\n您的卡號是:" + account.getCardId());
        enterSystem(scanner, accounts, account);
    }
    /* 查詢賬戶 */
    public static Account queryAccountByCardId(List<Account> accounts, String cardId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account a = accounts.get(i);
            if (a.getCardId().equals(cardId)) {
                return a;
            }
        }
        return null;
    }

1.3 開戶

實現(xiàn)邏輯:
1、輸入賬戶基本信息。
2、生成8位由數(shù)字組成的不重復(fù)的卡號。
3、存儲新賬戶信息。

    /* 開戶 */
    public static void register(Scanner scanner, List<Account> accounts) {
        System.out.println("==========歡迎您進入到開戶操作==========");
        System.out.println("請您輸入姓名:");
        String name = scanner.next();
        String password;
        while (true) {
            System.out.println("請您輸入密碼:");
            password = scanner.next();
            System.out.println("請您確認密碼:");
            if (scanner.next().equals(password)) {
                break;
            } else {
                System.out.println("您兩次輸入的密碼不一致,請重新設(shè)置");
            }
        }
        System.out.println("請您設(shè)置單次最大取現(xiàn)金額:");
        double quotaMoney = scanner.nextDouble();
        String cardId = createCardId(accounts);
        Account account = new Account(cardId, name, password, quotaMoney);
        accounts.add(account);
        System.out.println("恭喜您開戶成功!您的卡號是:\"" + account.getCardId() + "\",請您妥善保管。");
    }
    /* 生成卡號 */
    public static String createCardId(List<Account> accounts) {
        Random random = new Random();
        while (true) {
            String carId = "";
            for (int i = 0; i < 8; i++) {
                carId += random.nextInt(10);
            }
            Account a = queryAccountByCardId(accounts, carId);
            if (a == null) {
                return carId;
            }
        }
    }

2 個人主頁面

登錄成功之后進入個人主頁面主要實現(xiàn)查詢、存款、取款、轉(zhuǎn)賬、修改密碼、退出、注銷賬戶功能。

    /* 進入個人主頁面 */
    public static void enterSystem(Scanner scanner, List<Account> accounts, Account account) {
        while (true) {
            System.out.println("==========歡迎您進入到個人操作頁面==========");
            System.out.println("1、查詢");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、轉(zhuǎn)賬");
            System.out.println("5、修改密碼");
            System.out.println("6、退出");
            System.out.println("7、注銷賬戶");
            int type = scanner.nextInt();
            switch (type) {
                case 1:
                    showAccount(account);
                    break;
                case 2:
                    depositMoney(scanner, accounts, account);
                    break;
                case 3:
                    drawMoney(scanner, accounts, account);
                    break;
                case 4:
                    transferMoney(scanner, accounts, account);
                    break;
                case 5:
                    updatePassword(scanner, accounts, account);
                    return;
                case 6:
                    logout(accounts);
                    return;
                case 7:
                    destroyAccount(accounts, account);
                    return;
                default:
            }
        }
    }

2.1 查詢

主要展示當前登錄賬戶信息。

    /* 展示賬戶信息 */
    public static void showAccount(Account account) {
        System.out.println("==========當前賬戶信息==========");
        System.out.println("卡號:" + account.getCardId() + "\n" + "戶主:" + account.getUserName() + "\n" + "余額:" + account.getMoney() + "\n" + "當次取現(xiàn)額度:" + account.getQuotaMoney());
    }

2.2 存款

實現(xiàn)邏輯:
1、當前賬戶調(diào)用存款方法進行存款。
2、存款完成展示當前賬戶最新信息。

    /* 存款 */
    public static void inMoney(Scanner scanner, List<Account> accounts, Account account) {
        System.out.println("==========存款操作==========");
        System.out.println("請輸入存款金額:");
        double money = scanner.nextDouble();
        account.inMoney(money);
        System.out.println("存款完成!");
        showAccount(account);
    }

2.3 取款

實現(xiàn)邏輯:
1、當前賬戶調(diào)用取款方法進行取款。
2、根據(jù)當前賬戶取款方法返回值給出相應(yīng)的提示。
3、取款完成展示當前賬戶最新信息。

    /* 取款 */
    public static void outMoney(Scanner scanner, List<Account> accounts, Account account) {
        System.out.println("==========取款操作==========");
        while (true) {
            System.out.println("請輸入取款金額:");
            double money = scanner.nextDouble();
            int status = account.outMoney(money);
            switch (status) {
                case 1:
                    System.out.println("取款完成!");
                    showAccount(account);
                    return;
                case 0:
                    System.out.println("賬戶余額不足!");
                    break;
                case 2:
                    System.out.println("超出單次取現(xiàn)額度!");
                    break;
                default:
                    System.out.println("系統(tǒng)故障!");
            }
        }
    }

2.4 轉(zhuǎn)賬

實現(xiàn)邏輯:
1、判斷當前系統(tǒng)中賬戶個數(shù)。
2、判斷當前賬戶余額。
3、判斷轉(zhuǎn)賬賬戶是否存在。
4、判斷轉(zhuǎn)賬賬戶是否是自己。
5、對接收賬戶進行信息確認。
6、判斷當前賬戶取款方法返回值。
7、轉(zhuǎn)賬成功展示當前賬戶最新信息。

    /* 轉(zhuǎn)賬 */
    public static void transferMoney(Scanner scanner, List<Account> accounts, Account account) {
        System.out.println("==========轉(zhuǎn)賬操作==========");
        if (accounts.size() < 2) {
            System.out.println("當前只有一個賬號,暫無法轉(zhuǎn)賬!");
            enterSystem(scanner, accounts, account);
            return;
        }
        if (account.getMoney() == 0) {
            System.out.println("對不起,您的賬戶沒錢,就別轉(zhuǎn)了~~");
            return;
        }
        while (true) {
            System.out.println("請輸入轉(zhuǎn)賬賬號:");
            Account a = queryAccountByCardId(accounts, scanner.next());
            if (a == null) {
                System.out.println("賬號不存在!");
            } else  {
                if (account.getCardId().equals(a.getCardId())) {
                    System.out.println("您不可以給自己轉(zhuǎn)賬!");
                    continue;
                }
                System.out.println("請確認【*" + a.getUserName().substring(1) + "】的姓氏:");
                String surname = scanner.next();
                if (surname.equals(a.getUserName().substring(0, 1))) {
                    while (true) {
                        System.out.println("請輸入轉(zhuǎn)賬金額:");
                        double money = scanner.nextDouble();
                        int status = account.outMoney(money);
                        switch (status) {
                            case 1:
                                a.inMoney(money);
                                System.out.println("轉(zhuǎn)賬成功!您為" + a.getUserName() + "轉(zhuǎn)賬了:" + money);
                                showAccount(account);
                                return;
                            case 0:
                                System.out.println("賬戶余額不足!您最多可以轉(zhuǎn):" + account.getMoney());
                                break;
                            case 2:
                                System.out.println("超出單次取現(xiàn)額度!");
                                break;
                            default:
                                System.out.println("系統(tǒng)故障!");
                        }
                    }
                } else {
                    System.out.println("對不起,您認證的信息錯誤!");
                }
            }
        }
    }

2.5 修改密碼

實現(xiàn)邏輯:
1、舊密碼確認。
2、判斷兩次輸入的新密碼是否一致。
3、修改當前賬戶密碼。
4、在循環(huán)的switch中直接return結(jié)束循環(huán)返回到系統(tǒng)主頁面。

    /* 修改密碼 */
    public static void updatePassword(Scanner scanner, List<Account> accounts, Account account) {
        System.out.println("==========修改密碼操作==========");
        while (true) {
            System.out.println("請輸入密碼:");
            String password = scanner.next();
            if (password.equals(account.getPassword())) {
                break;
            } else {
                System.out.println("密碼錯誤!");
            }
        }
        while (true) {
            System.out.println("請輸入新密碼:");
            String password1 = scanner.next();
            System.out.println("請確認新密碼:");
            String password2 = scanner.next();
            if (password1.equals(password2)) {
                System.out.println("密碼修改成功!");
                account.setPassword(password1);
                break;
            } else {
                System.out.println("密碼不一致!");
            }
        }
    }

2.6 退出

實現(xiàn)邏輯:
1、給出提示之后在循環(huán)的switch中直接return結(jié)束循環(huán)返回到系統(tǒng)主頁面。

    /* 退出 */
    public static void logout(List<Account> accounts) {
        System.out.println("歡迎下次光臨!");
    }

2.7 注銷賬戶

實現(xiàn)邏輯:
1、將當前賬戶從賬戶集合中移除。
2、在循環(huán)的switch中直接return結(jié)束循環(huán)返回到系統(tǒng)主頁面。

    /* 注銷賬戶 */
    public static void destroyAccount(List<Account> accounts, Account account) {
        accounts.remove(account);
        System.out.println("您的賬戶已注銷!");
    }

總結(jié)

以上就是關(guān)于Java仿ATM操作系統(tǒng)實現(xiàn)的全部內(nèi)容。

如果有什么問題,我們可以一起交流討論解決。

最后,希望可以幫助到有需要的碼友。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容