溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java如何實(shí)現(xiàn)ATM系統(tǒng)

發(fā)布時(shí)間:2022-03-17 09:10:46 來(lái)源:億速云 閱讀:109 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹Java如何實(shí)現(xiàn)ATM系統(tǒng),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

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

系統(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è),登入,開(kāi)戶
        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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登入
                    break;
                case 2:
                    //開(kāi)戶
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(jié)

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

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

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

ArrayList<Account> accounts = new ArrayList<> ();

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

  • 分析

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

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

定義方法完成開(kāi)戶:

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è),登入,開(kāi)戶
        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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登入
                    break;
                case 2:
                    //開(kāi)戶
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您當(dāng)前輸入的操作命令不被支持!");
            }
        }
    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(jié)

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

  • 開(kāi)戶功能應(yīng)該獨(dú)立定義成方法,并傳入當(dāng)前集合對(duì)象給該方法。

  • 錄入開(kāi)戶信息(姓名,密碼)

  • 卡號(hào)要自動(dòng)生成且唯一

  • 把開(kāi)戶的信息封裝成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è),登入,開(kāi)戶
        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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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)的賬戶!");
            }
        }
    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(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è),登入,開(kāi)戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開(kāi)戶首頁(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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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());

    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(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è),登入,開(kāi)戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開(kāi)戶首頁(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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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());

    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(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è),登入,開(kāi)戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開(kāi)戶首頁(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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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ò)每次限額,不要取那么多,每次最多可以?。?quot; + 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());

    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(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è),登入,開(kāi)戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開(kāi)戶首頁(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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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ì)不起,您自己都快吃土了,就別裝逼了?。?quot;);
            return;
        }

        //3.開(kāi)始轉(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)賬才剛剛開(kāi)始
                        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
                        {
                            //開(kāi)始了
                            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ò)每次限額,不要取那么多,每次最多可以?。?quot; + 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());

    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

總結(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è),登入,開(kāi)戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開(kāi)戶首頁(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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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)賬?。?quot;);
            return;
        }

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

        //3.開(kāi)始轉(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)賬才剛剛開(kāi)始
                        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
                        {
                            //開(kāi)始了
                            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ò)每次限額,不要取那么多,每次最多可以?。?quot; + 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());

    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

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è),登入,開(kāi)戶
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 開(kāi)戶首頁(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.開(kāi)戶");
            System.out.println("您可以輸入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登錄
                    login(accounts,sc);
                    break;
                case 2:
                    //開(kāi)戶
                    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("歡迎下次光臨?。?quot;);
                    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)賬?。?quot;);
            return;
        }

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

        //3.開(kāi)始轉(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)賬才剛剛開(kāi)始
                        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
                        {
                            //開(kāi)始了
                            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ò)每次限額,不要取那么多,每次最多可以?。?quot; + 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());

    }


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


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


        String password = "";
        while (true)
        {
            System.out.println("請(qǐng)您輸入開(kāi)戶密碼:");
            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("恭喜您,您開(kāi)戶成功,您的卡號(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ù)了!
    }
}

Java如何實(shí)現(xiàn)ATM系統(tǒng)

以上是“Java如何實(shí)現(xiàn)ATM系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI