JAVA基礎 - ATM系統項目

一、前言

  該項目的材料和源碼均來自於黑馬程序員

二、技術選型分析

 

三、項目收穫

四、賬戶類、首頁設計分析

五、用戶開戶功能實現分析

六、用戶登錄功能實現分析

 

 七、用戶查詢、退出賬戶功能分析

 

 八、用戶存款分析

 

 九、用戶取款功能

 

 十、轉賬功能

 十一、項目源碼

1、Account.java

package com.tech.testcase;

/**
 賬戶類
 */
public class Account {
    private String cardId;//卡號
    private String userName;//客戶名稱
    private String passWord;//密碼
    private double money;//餘額
    private double quoteMoney;//當次取現限額

    public Account(String cardId, String userName, String passWord,  double quoteMoney) {
        this.cardId = cardId;
        this.userName = userName;
        this.passWord = passWord;
        //this.money = money;
        this.quoteMoney = quoteMoney;
    }

    public Account() {
    }

    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 getQuoteMoney() {
        return quoteMoney;
    }

    public void setQuoteMoney(double quoteMoney) {
        this.quoteMoney = quoteMoney;
    }
}

 2、ATMSystem.java

package com.tech.testcase;import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 1、用戶的賬戶信息,系統如何表示?
 定義賬戶類Account,定義系統關心的屬性信息
 2、系統採用什麼來存儲全部用戶的賬戶對象信息?
 ArrayList<Account> accounts = new ArrayList<>();
 3、登錄功能如何實現?
 根據卡號去集合中查詢對應的賬戶對象;如果找到了賬戶對象,說明卡號存在,繼續輸入密碼;如果密碼也正確,則登錄成功
 */

public class ATMSystem {
    public static void main(String[] args) {
        //1、準備系統需要的窗口對象,用於存儲賬戶對象
        ArrayList<Account> accounts = new ArrayList<>();

        //2、準備系統的首頁:登錄、開戶
        showMain(accounts);
    }
    public static void showMain(ArrayList<Account> accounts){
        System.out.println("===========歡迎進入首頁=================");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("請您輸入您想做的操作:");
            System.out.println("1、登錄");
            System.out.println("2、開戶");
            System.out.print("您可以輸入命令了:");
            int command =sc.nextInt();
            switch (command){
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當前輸入的操作命令不被支持");
            }
        }
    }

    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc) {
        //必須系統中存在用戶纔可以登錄
        if(accounts.size()==0){
            //沒有任何用戶
            System.out.println("當前系統中無任何用戶,您需要先註冊");
            return;//直接結束方法的執行
        }

        //2、讓用戶鍵盤錄入卡號,根據卡號查詢賬戶對象
        while (true) {
            System.out.println("請你輸入登錄的卡號:");
            String carId = sc.next();
            //根據卡號查詢賬戶對象
            Account acc = getAccountByCardid(carId,accounts);

            //3、判斷賬戶對象是否存在,存在說明卡號正常
            if(acc != null){
                while (true) {
                    //4、讓用戶繼續輸入密碼
                    System.out.println("請您輸入登錄的密碼");
                    String password = sc.next();
                    //5、判斷密碼是否正確
                    if(acc.getPassWord().equals(password)){
                        //密碼正確,登錄成功
                        //展示系統登錄後的操作界面
                        System.out.println("恭喜您,"+acc.getUserName()+"先生/女士成功進入系統,您的卡號是:"+acc.getCardId());
                        //展示操作界面
                        showUserCommand(sc,acc,accounts);
                        return;//繼續結束登錄方法
                    }else {
                        System.out.println("您的密碼輸入有誤,請確認");
                    }
                }
            }else {
                System.out.println("對不起,不存在卡號賬戶");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts) {
        while (true) {
        System.out.println("===========用戶操作界面===============");
        System.out.println("1、查詢賬戶");
        System.out.println("2、存款");
        System.out.println("3、取款");
        System.out.println("4、轉賬");
        System.out.println("5、修改密碼");
        System.out.println("6、退出");
        System.out.println("7、註銷賬戶");
        System.out.println("請您輸入操作命令:");
        int command = sc.nextInt();
        switch (command){
                case 1:
                    //查詢賬戶
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    depositMoney(acc,sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc,sc);
                    break;
                case 4:
                    //轉賬
                    transferMoney(accounts,acc,sc);
                    break;
                case 5:
                    //修改密碼
                    updatePassWord(acc,sc);
                    return;//結束當前操作的方法
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨");
                    return;//結束當前操作的方法
                case 7:
                    //註銷賬戶
                    //從當前集合中抹掉當前對象即可
                    accounts.remove(acc);
                    System.out.println("銷戶成功了");
                    return;//結束當前方法,回到登錄,登錄再return,回到首頁
                default:
                    System.out.println("您的命令輸入有誤");
            }
        }
    }

    private static void updatePassWord(Account acc,Scanner sc) {
        System.out.println("=======修改密碼============");
        while (true) {
            System.out.println("請您輸入正確的密碼");
            String okPassWord = sc.next();
            //判斷密碼是否正確
            if(acc.getPassWord().equals(okPassWord)){
                while (true) {
                    //可以輸入新密碼
                    System.out.println("請您輸入新密碼:");
                    String newPassWord = sc.next();

                    System.out.println("請您輸入確認密碼:");
                    String okNewPassWord = sc.next();

                    if(newPassWord.equals(okNewPassWord)){
                        //修改賬戶對象的密碼爲新密碼
                        acc.setPassWord(newPassWord);
                        return;//直接結束掉
                    }else {
                        System.out.println("兩次輸入的密碼不一致");
                    }
                }

            }else {
                System.out.println("當前輸入的密碼不正確");
            }
        }

    }

    /**
     * 轉賬功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc) {
        //1、判斷系統中是否有2個賬戶及以上
        if(accounts.size()<2){
            System.out.println("對不起,系統中無其他賬戶,您不可以轉賬");
            return;
        }

        //2、判斷自己的賬戶對象中是否有錢
        if(acc.getMoney()==0){
            System.out.println("對不起,您賬戶沒錢");
            return;
        }
        //3、開始轉賬邏輯
        while (true) {
            System.out.println("請您輸入對方賬戶的卡號:");
            String cardId= sc.next();
            Account account = getAccountByCardid(cardId,accounts);
            //判斷這個賬戶對象是否存在,存在說明對方卡號輸入正確
            if(account !=null){
                //判斷這個賬戶對象是否是當前登錄的賬戶自己
                if(account.getCardId().equals(acc.getCardId())){
                    //正在給自己轉賬
                    System.out.println("您不可以爲自己轉賬");
                }else {
                    //確認對方的姓氏
                    String name ="*"+ account.getUserName().substring(1);
                    System.out.println("請您確認【"+name+"】的姓氏:");
                    String preName = sc.next();
                    //判斷
                    if(account.getUserName().startsWith(preName)){
                        //真正開始轉賬
                        System.out.println("請您輸入轉賬的金額:");
                        double money = sc.nextDouble();
                        //判斷這個金額是否超過了自己的餘額
                        if(money>acc.getMoney()){
                            System.out.println("對不起,你要轉賬的金額太多,您最多可以轉賬"+acc.getMoney());
                        }else {
                            //真的可以轉了
                            acc.setMoney(acc.getMoney()-money);
                            account.setMoney(account.getMoney()+money);
                            System.out.println("恭喜轉賬成功,已經爲"+account.getUserName()+"轉賬"+money);
                            showAccount(acc);
                            return;
                        }
                    }else {
                        System.out.println("對不起,您認證的信息有誤");
                    }
                }
            }else {
                System.out.println("對不起,您輸入的轉賬卡號有問題");
            }
        }

    }

    /**
     * 取款
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc) {
        System.out.println("========取款操作==========");
        //1、判斷它的賬戶是否足夠100元
        if(acc.getMoney()>=100){
            while (true) {
                System.out.println("請您輸入取款的金額:");
                double money = sc.nextDouble();
                //2、判斷這個金額有沒有超過當次限額
                if(money>acc.getQuoteMoney()){
                    System.out.println("您當次取款金額超過每次限制,每次最多可以取:"+acc.getQuoteMoney());
                }else {
                    //3、判斷當前餘額是否足夠取錢
                    if(acc.getMoney()>=money){
                        //夠錢,可以取錢了
                        acc.setMoney(acc.getMoney()-money);
                        System.out.println("恭喜您,取錢"+money+"成功了,當前賬戶剩餘:"+acc.getMoney());
                        return;//取錢後幹掉取錢方法
                    }else {
                        System.out.println("餘額不足");
                    }
                }
            }

        }else {
            System.out.println("您的賬戶沒有超過100元,不允許取款");
        }
    }

    /**
     * 存錢的
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc) {
        System.out.println("=============存錢操作===============");
        System.out.println("請您輸入存款的金額:");
        double money = sc.nextDouble();

        //直接把金額修改到賬戶對象的money屬性中去
        acc.setMoney(acc.getMoney()+money);//需要把原來的錢get出來再加上新存入的money
        System.out.println("存款完成!");
        showAccount(acc);
    }

    private static void showAccount(Account acc) {
        System.out.println("=========當前賬戶詳情================");
        System.out.println("卡號:"+acc.getCardId());
        System.out.println("姓名:"+acc.getUserName());
        System.out.println("餘額:"+acc.getMoney());
        System.out.println("當次限額:"+acc.getQuoteMoney());
    }

    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對象
     */
    private static void register(ArrayList<Account> accounts,Scanner sc) {
        //2、鍵盤錄入姓名 密碼 確認密碼
        System.out.println("請您輸入開戶名");
        String name = sc.next();

        String password = "";
        while (true) {
            System.out.println("請您輸入開戶密碼");
            password = sc.next();
            System.out.println("請您輸入確認密碼");
            String okPassword = sc.next();

            //判斷兩次輸入的密碼是否一致
            if(okPassword.equals(password)){
                break;
            }else {
                System.out.println("兩次密碼必須一致");
            }
        }
        System.out.println("請您輸入當次限額");
        double quoteMoney = sc.nextDouble();

        //3、生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重複。
        String cardId = createCardId(accounts);

        //4、創建一個賬戶對象封裝賬戶的信息
        Account account= new Account(cardId,name,password,quoteMoney);

        //5、把賬戶對象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您開戶成功,您的卡號是:"+account.getCardId()+",請您妥善保管");
    }

    public static String createCardId(ArrayList<Account> accounts){
        //生成8位隨機的數字代表卡號
        while (true) {
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++) {
                cardId += r.nextInt(10);
            }
            //判斷是否重複了
            Account acc = getAccountByCardid(cardId,accounts);
            if(acc == null){
                //說明當前卡號沒有重複
                return cardId;
            }
        }
    }
    public static Account getAccountByCardid(String cardId,ArrayList<Account> accounts){
        //根據卡號查詢賬戶對象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if(acc.getCardId().equals(cardId)){
                return acc;
            }
        }
        return null;//查無此賬戶,說明卡號沒有重複了;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章