最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏

 更新時(shí)間:2022年03月16日 15:07:43   作者:Ara~追著風(fēng)跑  
這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能,文中實(shí)現(xiàn)流程寫的非常清晰全面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

系統(tǒng)準(zhǔn)備內(nèi)容分析

每個(gè)用戶的賬戶信息都是一個(gè)對(duì)象,需要提供賬戶類

需要準(zhǔn)備一個(gè)容器,用戶存儲(chǔ)系統(tǒng)全部賬戶對(duì)象信息

首頁(yè)值需要包含:登入和注冊(cè)2個(gè)功能

1.??系統(tǒng)準(zhǔn)備,首頁(yè),用戶開戶功能

系統(tǒng)準(zhǔn)備,首頁(yè)設(shè)計(jì)

系統(tǒng)準(zhǔn)備內(nèi)容分析:

  • 每個(gè)用戶的賬戶信息都是一個(gè)對(duì)象,需要提供賬戶類
  • 需要準(zhǔn)備一個(gè)容器,用于存儲(chǔ)和系統(tǒng)全部賬戶對(duì)象信息
  • 首頁(yè)只需要包含:登入和注冊(cè)2個(gè)功能

實(shí)現(xiàn)步驟:

  • 定義賬戶類,用于后期創(chuàng)建賬戶對(duì)象封裝用戶的賬戶信息
  • 賬戶類中的信息至少需要包含(卡號(hào),姓名,密碼,余額,取現(xiàn)額度)
package com.wangxinhua;

import java.util.ArrayList;
import java.util.Scanner;

public class ATMSystem
{
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts);
    }
    public static void showMain(ArrayList<Account> accounts)
    {
        System.out.println("========歡迎進(jìn)入首頁(yè)=====");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登入
                    break;
                case 2:
                    //開戶
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }
}

總結(jié)

  • 用戶的賬戶信息,系統(tǒng)如何表示的?

定義賬戶類Account,定義系統(tǒng)關(guān)心的屬性信息

  • 系統(tǒng)采用什么來(lái)儲(chǔ)存全部用戶的賬戶對(duì)象信息?
ArrayList<Account> accounts = new ArrayList<> ();

用戶開戶功能實(shí)現(xiàn)

  • 分析

開戶功能其實(shí)就是往系統(tǒng)的集合容器中存入一個(gè)新的賬戶對(duì)象的信息

  • 開戶功能實(shí)現(xiàn)步驟

定義方法完成開戶:

public static void register(ArrayList<Account>  accounts){...}
  • 鍵盤錄入姓名,秘密,確認(rèn)密碼(需保證兩次密碼一致)
  • 生成賬戶卡號(hào),卡號(hào)必須由系統(tǒng)自動(dòng)生成8位數(shù)字(必須保證卡號(hào)的唯一)
  • 創(chuàng)建Account賬戶類對(duì)象用戶封裝賬戶信息(姓名,密碼,卡號(hào))
  • 把Account賬戶類對(duì)象存入到集合accounts中去。
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem
{
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

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


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;//查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

總結(jié)

開戶功能的實(shí)現(xiàn)需要哪幾步操作,需要注意什么問(wèn)題?

  • 開戶功能應(yīng)該獨(dú)立定義成方法,并傳入當(dāng)前集合對(duì)象給該方法。
  • 錄入開戶信息(姓名,密碼)
  • 卡號(hào)要自動(dòng)生成且唯一
  • 把開戶的信息封裝成Account對(duì)象,存入到集合中去。

2.??用戶登入,操作頁(yè)展示,查詢賬戶,退出賬戶

用戶登入功能實(shí)現(xiàn)

分析

  • 定義方法:
public static void login(ArrayList<Account>accounts){...}
  • 讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象。
  • 如果沒(méi)有找到了賬戶對(duì)象,說(shuō)明卡號(hào)不存在,繼續(xù)輸入卡號(hào)
  • 如果找到了賬戶對(duì)象,說(shuō)明卡號(hào)存在,繼續(xù)輸入密碼
  • 如果密碼不正確,提示繼續(xù)輸入密碼
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem
{
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

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


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true) {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
//
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;//查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

總結(jié)

登錄功能如何實(shí)現(xiàn)的?

  • 根據(jù)卡號(hào)去集合中查詢對(duì)應(yīng)的賬戶對(duì)象
  • 如果找到了賬戶對(duì)象,說(shuō)明卡號(hào)存在,繼續(xù)輸入密碼
  • 如果密碼正確,則登錄成功

用戶操作頁(yè)設(shè)計(jì),查詢賬戶,退出賬戶功能

用戶操作頁(yè),查詢賬戶,退出賬戶功能分析

  • 用戶登錄成功后,需要進(jìn)入用戶操作頁(yè)、
  • 查詢就是直接展示當(dāng)前登錄成功的賬戶對(duì)象的信息
  • 退出賬戶是需要回到首頁(yè)的
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開戶首頁(yè)的意思
//            ArrayList<Account> accounts   使用方法定義功能傳入容器中  accounts是傳參
    {
        System.out.println("=============歡迎進(jìn)入首頁(yè)===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true) {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
                        //展示操作頁(yè)面
                        showUserCommand(sc,acc);
                        return;//繼續(xù)結(jié)束登錄方法
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc) {
        System.out.println("=========用戶操作頁(yè)面========");
        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.注銷賬戶");
        while (true)
        {
            System.out.println("請(qǐng)您輸入操作命令:");
            int command = sc.nextInt();
            switch (command)
            {
                case 1:
                    //查詢賬戶
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    break;
                case 3:
                    //取款
                    break;
                case 4:
                    //轉(zhuǎn)賬
                    break;
                case 5:
                    //修改密碼
                    break;
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨!!");
                    return; //結(jié)束當(dāng)前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注銷賬戶
                    break;
                default:
                    System.out.println("您輸入有誤!");
            }
        }
    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========當(dāng)前賬戶詳情=========");
        System.out.println("卡號(hào)" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余額" + acc.getMoney());
        System.out.println("當(dāng)次限額:" + acc.getQuotaMoney());

    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;//查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

總結(jié)

用戶操作頁(yè)設(shè)計(jì),查詢賬戶,退出賬戶功能注意事項(xiàng)

  • 我們應(yīng)該注意接入登錄界面后應(yīng)該注意用戶操作頁(yè)面有哪些
  • 設(shè)置好操作界面后需要接入退出接口

3.??用戶存款與取款

用戶存款

存款分析

  • 存款就是拿到當(dāng)前賬戶對(duì)象
  • 然后讓用戶輸入存款金額
  • 調(diào)用賬戶對(duì)象的setMoney方法將戰(zhàn)虎余額修改成存錢后的余額
  • 存錢后需要查新一下賬戶信息,確認(rèn)是否存錢成功了!
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開戶首頁(yè)的意思
//            ArrayList<Account> accounts   使用方法定義功能傳入容器中  accounts是傳參
    {
        System.out.println("=============歡迎進(jìn)入首頁(yè)===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true) {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
                        //展示操作頁(yè)面
                        showUserCommand(sc,acc);
                        return;//繼續(xù)結(jié)束登錄方法
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc) {
        while (true)
        {
            System.out.println("=========用戶操作頁(yè)面========");
            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.注銷賬戶");
            System.out.println("請(qǐng)您輸入操作命令:");
            int command = sc.nextInt();
            switch (command)
            {
                case 1:
                    //查詢賬戶
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    depositMoney(acc,sc);
                    break;
                case 3:
                    //取款
                    break;
                case 4:
                    //轉(zhuǎn)賬
                    break;
                case 5:
                    //修改密碼
                    break;
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨??!");
                    return; //結(jié)束當(dāng)前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注銷賬戶
                    break;
                default:
                    System.out.println("您輸入有誤!");
            }
        }
    }

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

        //直接把金額修改到賬戶對(duì)象的money屬性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========當(dāng)前賬戶詳情=========");
        System.out.println("卡號(hào)" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余額" + acc.getMoney());
        System.out.println("當(dāng)次限額:" + acc.getQuotaMoney());

    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;
        //查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

總結(jié)

存款分析

  • 存款就是拿到當(dāng)前賬戶對(duì)象
  • 然后讓用戶輸入存款的金額
  • 調(diào)用賬戶對(duì)象的setMoney方法將賬戶余額修改成存錢后的余額
  • 存錢后需要查詢一下賬戶信息,確認(rèn)是否存錢成功了!

取款分析

  • 取款需要先判斷賬戶是否有錢
  • 有錢則拿到自己賬戶對(duì)象
  • 然后讓用戶輸入取款金額
  • 判斷取款金額是否超過(guò)了當(dāng)次限額,以及金額是否足夠
  • 滿足要求則調(diào)用賬戶對(duì)象的setMoney方法完成金額的修改
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開戶首頁(yè)的意思
//            ArrayList<Account> accounts   使用方法定義功能傳入容器中  accounts是傳參
    {
        System.out.println("=============歡迎進(jìn)入首頁(yè)===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true) {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
                        //展示操作頁(yè)面
                        showUserCommand(sc,acc);
                        return;//繼續(xù)結(jié)束登錄方法
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc) {
        while (true)
        {
            System.out.println("=========用戶操作頁(yè)面========");
            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.注銷賬戶");
            System.out.println("請(qǐng)您輸入操作命令:");
            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:
                    //轉(zhuǎn)賬
                    break;
                case 5:
                    //修改密碼
                    break;
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨!!");
                    return; //結(jié)束當(dāng)前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注銷賬戶
                    break;
                default:
                    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("請(qǐng)您輸入取款的金額:");
                double money = sc.nextDouble();
                //2.判斷整個(gè)金額有沒(méi)有超過(guò)當(dāng)次限額
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您當(dāng)次取款金額超過(guò)每次限額,不要取那么多,每次最多可以?。? + acc.getQuotaMoney());
                }
                else
                {
                    //3.判斷當(dāng)前余額是否足夠你取錢
                    if (acc.getMoney() >= money)
                    {
                        //夠了,可以取錢了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取錢" + money + "成功了!當(dāng)前賬戶還剩余:" + acc.getMoney());
                        return;//取錢后干掉了取錢方法
                    }
                    else
                    {
                        System.out.println("余額不足啊??!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金額沒(méi)有超過(guò)100元,該努力工作了~~~");
        }

    }

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

        //直接把金額修改到賬戶對(duì)象的money屬性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========當(dāng)前賬戶詳情=========");
        System.out.println("卡號(hào)" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余額" + acc.getMoney());
        System.out.println("當(dāng)次限額:" + acc.getQuotaMoney());

    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;
        //查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

總結(jié)溫習(xí)

  • 取款需要先判斷賬戶是否有錢
  • 有錢則拿到自己賬戶對(duì)象
  • 然后讓用戶輸入取款金額
  • 判斷取款金額是否超過(guò)了當(dāng)次限額,以及金額是否足夠
  • 滿足要求則調(diào)用賬戶對(duì)象的setMoney方法完成金額的修改

4.??用戶轉(zhuǎn)賬,修改密碼,銷戶

用戶轉(zhuǎn)賬功能

分析

  • 轉(zhuǎn)賬功能需要判斷系統(tǒng)中是否有2個(gè)賬戶對(duì)象及以上
  • 同時(shí)還要判斷總結(jié)賬戶是否有錢
  • 接下來(lái)需要輸入對(duì)方卡號(hào),判斷對(duì)方賬戶是否存在
  • 對(duì)方賬戶存在還需要認(rèn)證對(duì)方戶主的姓氏
  • 滿足要求則可以把自己賬戶對(duì)象的金額修改到對(duì)方賬戶對(duì)象中去
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開戶首頁(yè)的意思
//            ArrayList<Account> accounts   使用方法定義功能傳入容器中  accounts是傳參
    {
        System.out.println("=============歡迎進(jìn)入首頁(yè)===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true) {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
                        //展示操作頁(yè)面
                        showUserCommand(sc,acc,accounts);
                        return;//繼續(xù)結(jié)束登錄方法
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts)
    {
        while (true)
        {
            System.out.println("=========用戶操作頁(yè)面========");
            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.注銷賬戶");
            System.out.println("請(qǐng)您輸入操作命令:");
            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:
                    //轉(zhuǎn)賬
                    transferMoney(accounts,acc ,sc);
                    break;
                case 5:
                    //修改密碼
                    break;
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨?。?);
                    return; //結(jié)束當(dāng)前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注銷賬戶
                    break;
                default:
                    System.out.println("您輸入有誤!");
            }
        }
    }

    /**
     * 轉(zhuǎn)賬功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
    {
        //1.判斷系統(tǒng)中是否有2個(gè)賬戶及以上
        if (accounts.size() < 2)
        {
            System.out.println("對(duì)不起,系統(tǒng)中無(wú)其他賬戶,您不可以轉(zhuǎn)賬??!");
            return;
        }

        //2.判斷自己的賬戶對(duì)象中是否有錢
        if (acc.getMoney() == 0)
        {
            System.out.println("對(duì)不起,您自己都快吃土了,就別裝逼了?。?);
            return;
        }

        //3.開始轉(zhuǎn)賬邏輯
        while (true)
        {
            System.out.println("請(qǐng)您輸入對(duì)方賬戶的卡號(hào):");
            String cardId = sc.next();
            Account account = getAccountBycardId(cardId,accounts);
            //判斷整個(gè)賬戶對(duì)象是否存在,存在說(shuō)明對(duì)方卡號(hào)輸入正確
            if (account != null)
            {
                //判斷這個(gè)賬戶對(duì)象是否是當(dāng)前自己登錄的賬戶
                if (account.getCardId().equals(acc.getCardId()))
                {
                    //也就是這里企圖想給自己轉(zhuǎn)賬
                    System.out.println("您不能給自己轉(zhuǎn)賬!");
                }
                else
                {
                    //確認(rèn)對(duì)方的姓氏
                    String name = "*" + account.getUserWord().substring(1);
                    System.out.println("請(qǐng)您確認(rèn)【" + name + "】的姓氏:");
                    String preName = sc.next();
                    //判斷
                    if (account.getUserWord().startsWith(preName))
                    {
                        //真正的轉(zhuǎn)賬才剛剛開始
                        System.out.println("請(qǐng)您輸入轉(zhuǎn)賬的金額:");
                        double money = sc.nextDouble();
                        //判斷這個(gè)金額是否超過(guò)了自己的金額
                        if (money > acc.getMoney())
                        {
                            System.out.println("對(duì)不起,您要轉(zhuǎn)賬的金額太多,您最多可以轉(zhuǎn)賬:" + acc.getMoney());
                        }
                        else
                        {
                            //開始了
                            acc.setMoney(acc.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println("恭喜您,轉(zhuǎn)賬成功了,已經(jīng)為" + account.getUserWord() + "轉(zhuǎn)賬了:" + money);
                            showAccount(acc);
                            return;

                        }
                    }
                    else
                    {
                        System.out.println("對(duì)不起,您認(rèn)證的信息有誤~~~");
                    }
                }

            }
            else
            {
                System.out.println("對(duì)不起,您輸入的轉(zhuǎn)賬卡號(hào)有問(wèn)題?。?);

            }
        }
    }

    /**
     * 取款操作
     * @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("請(qǐng)您輸入取款的金額:");
                double money = sc.nextDouble();
                //2.判斷整個(gè)金額有沒(méi)有超過(guò)當(dāng)次限額
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您當(dāng)次取款金額超過(guò)每次限額,不要取那么多,每次最多可以?。? + acc.getQuotaMoney());
                }
                else
                {
                    //3.判斷當(dāng)前余額是否足夠你取錢
                    if (acc.getMoney() >= money)
                    {
                        //夠了,可以取錢了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取錢" + money + "成功了!當(dāng)前賬戶還剩余:" + acc.getMoney());
                        return;//取錢后干掉了取錢方法
                    }
                    else
                    {
                        System.out.println("余額不足?。?!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金額沒(méi)有超過(guò)100元,該努力工作了~~~");
        }

    }

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

        //直接把金額修改到賬戶對(duì)象的money屬性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========當(dāng)前賬戶詳情=========");
        System.out.println("卡號(hào)" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余額" + acc.getMoney());
        System.out.println("當(dāng)次限額:" + acc.getQuotaMoney());

    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;
        //查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

總結(jié)溫習(xí)

  • 轉(zhuǎn)賬功能需要判斷系統(tǒng)中是否有2個(gè)賬戶對(duì)象及以上
  • 同時(shí)還要判斷總結(jié)賬戶是否有錢
  • 接下來(lái)需要輸入對(duì)方卡號(hào),判斷對(duì)方賬戶是否存在
  • 對(duì)方賬戶存在還需要認(rèn)證對(duì)方戶主的姓氏
  • 滿足要求則可以把自己賬戶對(duì)象的金額修改到對(duì)方賬戶對(duì)象中去
  • 修改密碼與銷戶

分析

  • 修改密碼就是把當(dāng)前對(duì)現(xiàn)象的密碼屬性使用set方法進(jìn)行更新
  • 銷戶是從集合對(duì)象中刪除當(dāng)前對(duì)象,并回到首頁(yè)

這里為止所有的ATM系統(tǒng)的操作代碼就已經(jīng)完成

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開戶首頁(yè)的意思
//            ArrayList<Account> accounts   使用方法定義功能傳入容器中  accounts是傳參
    {
        System.out.println("=============歡迎進(jìn)入首頁(yè)===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true) 
        {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
                        //展示操作頁(yè)面
                        showUserCommand(sc,acc,accounts);
                        return;//繼續(xù)結(jié)束登錄方法
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts)
    {
        while (true)
        {
            System.out.println("=========用戶操作頁(yè)面========");
            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.注銷賬戶");
            System.out.println("請(qǐng)您輸入操作命令:");
            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:
                    //轉(zhuǎn)賬
                    transferMoney(accounts,acc ,sc);
                    break;
                case 5:
                    //修改密碼
                    updataPassWord(acc,sc);
                    return;//結(jié)束當(dāng)前…………
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨!!");
                    return; //結(jié)束當(dāng)前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注銷賬戶
                    //從當(dāng)前集合中抹掉當(dāng)前賬戶對(duì)象即可
                    accounts.remove(acc);
                    System.out.println("銷戶成功了??!");
                    return;
                default:
                    System.out.println("您輸入有誤!");
            }
        }
    }

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

                System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
                String okNewPassWord = sc.next();

                if (newPassWord.equals(okNewPassWord))
                {
                    //修改賬戶對(duì)象的密碼為新密碼
                    acc.setPassWord(newPassWord);
                    return;//直接結(jié)束方法!
                }
                else
                {
                    System.out.println("您兩次輸入的密碼不一致~~");
                }
            }
            else
            {
                System.out.println("當(dāng)前輸入的密碼不正確~~~");
            }
        }

    }

    /**
     * 轉(zhuǎn)賬功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
    {
        //1.判斷系統(tǒng)中是否有2個(gè)賬戶及以上
        if (accounts.size() < 2)
        {
            System.out.println("對(duì)不起,系統(tǒng)中無(wú)其他賬戶,您不可以轉(zhuǎn)賬?。?);
            return;
        }

        //2.判斷自己的賬戶對(duì)象中是否有錢
        if (acc.getMoney() == 0)
        {
            System.out.println("對(duì)不起,您自己都快吃土了,就別裝逼了??!");
            return;
        }

        //3.開始轉(zhuǎn)賬邏輯
        while (true)
        {
            System.out.println("請(qǐng)您輸入對(duì)方賬戶的卡號(hào):");
            String cardId = sc.next();
            Account account = getAccountBycardId(cardId,accounts);
            //判斷整個(gè)賬戶對(duì)象是否存在,存在說(shuō)明對(duì)方卡號(hào)輸入正確
            if (account != null)
            {
                //判斷這個(gè)賬戶對(duì)象是否是當(dāng)前自己登錄的賬戶
                if (account.getCardId().equals(acc.getCardId()))
                {
                    //也就是這里企圖想給自己轉(zhuǎn)賬
                    System.out.println("您不能給自己轉(zhuǎn)賬!");
                }
                else
                {
                    //確認(rèn)對(duì)方的姓氏
                    String name = "*" + account.getUserWord().substring(1);
                    System.out.println("請(qǐng)您確認(rèn)【" + name + "】的姓氏:");
                    String preName = sc.next();
                    //判斷
                    if (account.getUserWord().startsWith(preName))
                    {
                        //真正的轉(zhuǎn)賬才剛剛開始
                        System.out.println("請(qǐng)您輸入轉(zhuǎn)賬的金額:");
                        double money = sc.nextDouble();
                        //判斷這個(gè)金額是否超過(guò)了自己的金額
                        if (money > acc.getMoney())
                        {
                            System.out.println("對(duì)不起,您要轉(zhuǎn)賬的金額太多,您最多可以轉(zhuǎn)賬:" + acc.getMoney());
                        }
                        else
                        {
                            //開始了
                            acc.setMoney(acc.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println("恭喜您,轉(zhuǎn)賬成功了,已經(jīng)為" + account.getUserWord() + "轉(zhuǎn)賬了:" + money);
                            showAccount(acc);
                            return;

                        }
                    }
                    else
                    {
                        System.out.println("對(duì)不起,您認(rèn)證的信息有誤~~~");
                    }
                }

            }
            else
            {
                System.out.println("對(duì)不起,您輸入的轉(zhuǎn)賬卡號(hào)有問(wèn)題?。?);

            }
        }
    }

    /**
     * 取款操作
     * @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("請(qǐng)您輸入取款的金額:");
                double money = sc.nextDouble();
                //2.判斷整個(gè)金額有沒(méi)有超過(guò)當(dāng)次限額
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您當(dāng)次取款金額超過(guò)每次限額,不要取那么多,每次最多可以?。? + acc.getQuotaMoney());
                }
                else
                {
                    //3.判斷當(dāng)前余額是否足夠你取錢
                    if (acc.getMoney() >= money)
                    {
                        //夠了,可以取錢了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取錢" + money + "成功了!當(dāng)前賬戶還剩余:" + acc.getMoney());
                        return;//取錢后干掉了取錢方法
                    }
                    else
                    {
                        System.out.println("余額不足?。?!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金額沒(méi)有超過(guò)100元,該努力工作了~~~");
        }

    }

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

        //直接把金額修改到賬戶對(duì)象的money屬性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========當(dāng)前賬戶詳情=========");
        System.out.println("卡號(hào)" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余額" + acc.getMoney());
        System.out.println("當(dāng)次限額:" + acc.getQuotaMoney());

    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;
        //查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

5. ??源代碼在這里這里拿

package com.wangxinhua;


/**
    賬戶類
 */
public class Account {
    private String CardId;//卡號(hào)
    private String UserWord;//客戶名稱
    private String PassWord;//密碼
    private double Money;//余額
    private double QuotaMoney;//當(dāng)次取現(xiàn)限額

//無(wú)參函數(shù)
    public Account() {
    }

//    構(gòu)造好了有參函數(shù),那么就會(huì)有無(wú)參函數(shù)
//    有參函數(shù)
    public Account(String cardId, String userWord, String passWord, double quotaMoney) {
        CardId = cardId;
        UserWord = userWord;
        PassWord = passWord;
        QuotaMoney = quotaMoney;
    }

    public String getCardId() {
        return CardId;
    }

    public void setCardId(String cardId) {
        CardId = cardId;
    }

    public String getUserWord() {
        return UserWord;
    }

    public void setUserWord(String userWord) {
        UserWord = userWord;
    }

    public String getPassWord() {
        return PassWord;
    }

    public void setPassWord(String passWord) {
        PassWord = passWord;
    }

    public double getMoney() {
        return Money;
    }

    public void setMoney(double money) {
        Money = money;
    }

    public double getQuotaMoney() {
        return QuotaMoney;
    }

    public void setQuotaMoney(double quotaMoney) {
        QuotaMoney = quotaMoney;
    }
}

這里是第一個(gè)類用于構(gòu)造函數(shù) 下面這個(gè)是第二個(gè)類

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.準(zhǔn)備系統(tǒng)需要的容器對(duì)象,用戶存儲(chǔ)賬戶對(duì)象
        ArrayList<Account> accounts = new ArrayList();

//        2.準(zhǔn)備系統(tǒng)的首頁(yè),登入,開戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開戶首頁(yè)的意思
//            ArrayList<Account> accounts   使用方法定義功能傳入容器中  accounts是傳參
    {
        System.out.println("=============歡迎進(jìn)入首頁(yè)===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("請(qǐng)您輸入您想做的操作:");
            System.out.println("1.登錄");
            System.out.println("2.開戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用戶登錄
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必須系統(tǒng)中存在賬戶才可以登錄
        if (accounts.size() == 0)
        {
            //沒(méi)有任何賬戶
            System.out.println("當(dāng)前系統(tǒng)中無(wú)任何賬戶,您需要先注冊(cè)!");
            return;//直接結(jié)束方法的執(zhí)行!
        }
        //2.讓用戶鍵盤錄入卡號(hào),根據(jù)卡號(hào)查詢賬戶對(duì)象
        while (true)
        {
            System.out.println("請(qǐng)您輸入登錄的卡號(hào):");
            String cardId = sc.next();
            //根據(jù)卡號(hào)查詢賬戶對(duì)象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判斷賬戶對(duì)象是否存在,存在說(shuō)明卡號(hào)沒(méi)問(wèn)題
            if (acc != null)
            {
                while (true)
                {
//                    4.讓用戶繼續(xù)輸入密碼
                    System.out.println("請(qǐng)您輸入登錄的密碼:");
                    String password = sc.next();
//              5.判斷密碼是否正確
                    if (acc.getPassWord().equals(password))
                    {
                        //密碼正確,登入成功
                        //展示系統(tǒng)登錄后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號(hào)是:" + acc.getCardId());
                        //展示操作頁(yè)面
                        showUserCommand(sc,acc,accounts);
                        return;//繼續(xù)結(jié)束登錄方法
                    }
                    else
                    {
                        System.out.println("您的密碼有誤,請(qǐng)確認(rèn)!");

                    }
                }
            }
            else
            {
                System.out.println("對(duì)不起,不存在該卡號(hào)的賬戶!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts)
    {
        while (true)
        {
            System.out.println("=========用戶操作頁(yè)面========");
            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.注銷賬戶");
            System.out.println("請(qǐng)您輸入操作命令:");
            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:
                    //轉(zhuǎn)賬
                    transferMoney(accounts,acc ,sc);
                    break;
                case 5:
                    //修改密碼
                    updataPassWord(acc,sc);
                    return;//結(jié)束當(dāng)前…………
                case 6:
                    //退出
                    System.out.println("歡迎下次光臨??!");
                    return; //結(jié)束當(dāng)前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注銷賬戶
                    //從當(dāng)前集合中抹掉當(dāng)前賬戶對(duì)象即可
                    accounts.remove(acc);
                    System.out.println("銷戶成功了!!");
                    return;
                default:
                    System.out.println("您輸入有誤!");
            }
        }
    }

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

                System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
                String okNewPassWord = sc.next();

                if (newPassWord.equals(okNewPassWord))
                {
                    //修改賬戶對(duì)象的密碼為新密碼
                    acc.setPassWord(newPassWord);
                    return;//直接結(jié)束方法!
                }
                else
                {
                    System.out.println("您兩次輸入的密碼不一致~~");
                }
            }
            else
            {
                System.out.println("當(dāng)前輸入的密碼不正確~~~");
            }
        }

    }

    /**
     * 轉(zhuǎn)賬功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
    {
        //1.判斷系統(tǒng)中是否有2個(gè)賬戶及以上
        if (accounts.size() < 2)
        {
            System.out.println("對(duì)不起,系統(tǒng)中無(wú)其他賬戶,您不可以轉(zhuǎn)賬?。?);
            return;
        }

        //2.判斷自己的賬戶對(duì)象中是否有錢
        if (acc.getMoney() == 0)
        {
            System.out.println("對(duì)不起,您自己都快吃土了,就別裝逼了??!");
            return;
        }

        //3.開始轉(zhuǎn)賬邏輯
        while (true)
        {
            System.out.println("請(qǐng)您輸入對(duì)方賬戶的卡號(hào):");
            String cardId = sc.next();
            Account account = getAccountBycardId(cardId,accounts);
            //判斷整個(gè)賬戶對(duì)象是否存在,存在說(shuō)明對(duì)方卡號(hào)輸入正確
            if (account != null)
            {
                //判斷這個(gè)賬戶對(duì)象是否是當(dāng)前自己登錄的賬戶
                if (account.getCardId().equals(acc.getCardId()))
                {
                    //也就是這里企圖想給自己轉(zhuǎn)賬
                    System.out.println("您不能給自己轉(zhuǎn)賬!");
                }
                else
                {
                    //確認(rèn)對(duì)方的姓氏
                    String name = "*" + account.getUserWord().substring(1);
                    System.out.println("請(qǐng)您確認(rèn)【" + name + "】的姓氏:");
                    String preName = sc.next();
                    //判斷
                    if (account.getUserWord().startsWith(preName))
                    {
                        //真正的轉(zhuǎn)賬才剛剛開始
                        System.out.println("請(qǐng)您輸入轉(zhuǎn)賬的金額:");
                        double money = sc.nextDouble();
                        //判斷這個(gè)金額是否超過(guò)了自己的金額
                        if (money > acc.getMoney())
                        {
                            System.out.println("對(duì)不起,您要轉(zhuǎn)賬的金額太多,您最多可以轉(zhuǎn)賬:" + acc.getMoney());
                        }
                        else
                        {
                            //開始了
                            acc.setMoney(acc.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println("恭喜您,轉(zhuǎn)賬成功了,已經(jīng)為" + account.getUserWord() + "轉(zhuǎn)賬了:" + money);
                            showAccount(acc);
                            return;

                        }
                    }
                    else
                    {
                        System.out.println("對(duì)不起,您認(rèn)證的信息有誤~~~");
                    }
                }

            }
            else
            {
                System.out.println("對(duì)不起,您輸入的轉(zhuǎn)賬卡號(hào)有問(wèn)題??!");

            }
        }
    }

    /**
     * 取款操作
     * @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("請(qǐng)您輸入取款的金額:");
                double money = sc.nextDouble();
                //2.判斷整個(gè)金額有沒(méi)有超過(guò)當(dāng)次限額
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您當(dāng)次取款金額超過(guò)每次限額,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
                }
                else
                {
                    //3.判斷當(dāng)前余額是否足夠你取錢
                    if (acc.getMoney() >= money)
                    {
                        //夠了,可以取錢了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取錢" + money + "成功了!當(dāng)前賬戶還剩余:" + acc.getMoney());
                        return;//取錢后干掉了取錢方法
                    }
                    else
                    {
                        System.out.println("余額不足啊??!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金額沒(méi)有超過(guò)100元,該努力工作了~~~");
        }

    }

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

        //直接把金額修改到賬戶對(duì)象的money屬性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========當(dāng)前賬戶詳情=========");
        System.out.println("卡號(hào)" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余額" + acc.getMoney());
        System.out.println("當(dāng)次限額:" + acc.getQuotaMoney());

    }


    /**
     * 用戶開戶功能
     * @param accounts 賬戶的集合對(duì)象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用戶開戶功能==========");
        //鍵盤錄入 姓名 密碼 確認(rèn)密碼
        System.out.println("請(qǐng)您輸入開戶名稱:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開戶密碼:");
            password = sc.next();
            System.out.println("請(qǐng)您輸入確認(rèn)密碼:");
            String okPassword = sc.next();

//        判斷兩次輸入的密碼是否一致
            if (okPassword.equals(password))
                            //字符串比較用equals
            {
                break;
            }
            else
            {
                System.out.println("兩次密碼必須一致~~~");
            }
        }


        System.out.println("請(qǐng)您輸入當(dāng)次限額:");
        double quotaMoney = sc.nextDouble();

//        3.生成賬戶的卡號(hào),卡號(hào)是8位,而且不能與其他賬戶卡號(hào)重復(fù)。
        String cardId = creatCardId(accounts);


//        4.創(chuàng)建一個(gè)賬戶對(duì)象封裝賬戶的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

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


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位隨機(jī)的數(shù)字代表卡號(hào)
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判斷卡號(hào)是否重復(fù)了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            說(shuō)明當(dāng)前卡號(hào)沒(méi)有重復(fù)
                return cardId;
            }
        }
    }

    public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
    {
//        根據(jù)卡號(hào)查詢對(duì)象
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId))
            {
                return acc;
            }
        }
        return null;
        //查無(wú)此賬戶,說(shuō)明卡號(hào)沒(méi)有重復(fù)了!
    }
}

??到這里我們的ATM系統(tǒng)就已經(jīng)實(shí)現(xiàn)完成了,肝了兩天兩夜,喜歡的小伙伴可以復(fù)制源代碼去試試看,挺有趣的噢

到此這篇關(guān)于Java實(shí)現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏的文章就介紹到這了,更多相關(guān)Java ATM系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例

    java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例

    在Java中,我們可以使用第三方庫(kù)來(lái)實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音的功能,本文主要介紹了java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例,選擇jacob技術(shù)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • Mybatis批量插入的三種實(shí)現(xiàn)方法

    Mybatis批量插入的三種實(shí)現(xiàn)方法

    在日常開發(fā)中,如果要操作數(shù)據(jù)庫(kù)的話,或多或少都會(huì)遇到批量數(shù)據(jù)的處理,本文主要介紹了Mybatis批量插入的三種實(shí)現(xiàn)方法,感興趣的可以了解一下
    2023-10-10
  • java中i = i++和i =++i的深入講解

    java中i = i++和i =++i的深入講解

    這篇文章主要介紹了java中i = i++和i =++i的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • RocketMQ源碼分析之Broker過(guò)期消息清理機(jī)制

    RocketMQ源碼分析之Broker過(guò)期消息清理機(jī)制

    這篇文章主要為大家介紹了RocketMQ源碼分析之Broker過(guò)期消息清理機(jī)制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Java項(xiàng)目導(dǎo)入IDEA的流程配置以及常見(jiàn)問(wèn)題解決方法

    Java項(xiàng)目導(dǎo)入IDEA的流程配置以及常見(jiàn)問(wèn)題解決方法

    通常一個(gè)團(tuán)隊(duì)中可能有人用eclipse,有人用intelliJ,那么經(jīng)常會(huì)出現(xiàn)需要導(dǎo)入別人用eclipse建好的web項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Java項(xiàng)目導(dǎo)入IDEA的流程配置以及常見(jiàn)問(wèn)題解決方法的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • SpringBoot中實(shí)現(xiàn)Druid前端監(jiān)控界面自動(dòng)登錄功能

    SpringBoot中實(shí)現(xiàn)Druid前端監(jiān)控界面自動(dòng)登錄功能

    這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)Druid前端監(jiān)控界面自動(dòng)登錄功能,需要的朋友可以參考下
    2024-08-08
  • Java并發(fā)容器相關(guān)知識(shí)總結(jié)

    Java并發(fā)容器相關(guān)知識(shí)總結(jié)

    今天給大家?guī)?lái)的文章是Java并發(fā)容器的相關(guān)知識(shí),文中有非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)Java并發(fā)容器的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • Java基于PDFbox實(shí)現(xiàn)讀取處理PDF文件

    Java基于PDFbox實(shí)現(xiàn)讀取處理PDF文件

    PDFbox是一個(gè)開源的、基于Java的、支持PDF文檔生成的工具庫(kù),它可以用于創(chuàng)建新的PDF文檔,修改現(xiàn)有的PDF文檔,還可以從PDF文檔中提取所需的內(nèi)容。本文將具體介紹一下PDFbox讀取處理PDF文件的示例代碼,感興趣的可以學(xué)習(xí)一下
    2022-02-02
  • java實(shí)現(xiàn)連接mysql數(shù)據(jù)庫(kù)單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼

    java實(shí)現(xiàn)連接mysql數(shù)據(jù)庫(kù)單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼

    下面小編就為大家?guī)?lái)一篇java實(shí)現(xiàn)連接mysql數(shù)據(jù)庫(kù)單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • SpringMVC Json自定義序列化和反序列化的操作方法

    SpringMVC Json自定義序列化和反序列化的操作方法

    這篇文章主要介紹了SpringMVC Json自定義序列化和反序列化的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01

最新評(píng)論

外汇| 金平| 双流县| 东平县| 安丘市| 常宁市| 神农架林区| 综艺| 罗甸县| 怀化市| 喜德县| 宁远县| 武川县| 阜宁县| 阿克陶县| 阜康市| 霍林郭勒市| 油尖旺区| 合川市| 滨州市| 乌鲁木齐市| 阆中市| 军事| 古交市| 厦门市| 正安县| 伊宁市| 广宗县| 中宁县| 太仓市| 长子县| 邳州市| 肃宁县| 仁布县| 珲春市| 岳西县| 汉源县| 铜山县| 都兰县| 连江县| 乌拉特后旗|