Object oriented 4 ATM system with java programming

Project introduction and function display





Analysis on technology selection of ATM system

Technical point analysis

  1. Object oriented programming:
    Each user Account is an object: therefore, the Account class Account needs to be designed to create an Account object and encapsulate Account information
  2. Use collection containers:
    The system needs to provide a container to store the information of these account objects. We choose the ArrayList collection
  3. Procedure flow control:
    It is necessary to combine the business logic of branch, loop, jump keyword and other related operation control procedures
  4. Using common API s
    The content comparison of login information, analysis and processing of business data need to be solved by common API s such as String
//Account class
package ATM2;

/**
 * Account class
 */
public class Account {
    private String cardId;   //Card number
    private String userName;  //Customer name
    private String passWord;  //password
    private double money;  //balance
    private double quotaMoney;  // Current cash withdrawal limit

    public Account() {
    }

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

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public double getMoney() {
        return money;
    }

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

    public double getQuotaMoney() {
        return quotaMoney;
    }

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

System preparation and homepage design

System preparation content analysis

  1. The account information of each user is an object, and the account class needs to be provided
  2. A container needs to be prepared to store all account object information of the system
  3. The home page only needs to include two functions: login and registration

Implementation steps

  1. Define an account class, which is used to create an account object later and encapsulate the user's account information
  2. The information in the account category must at least include (card number, name, password, balance, cash withdrawal limit)
  3. An ArrayList collection needs to be prepared to store the account objects of system users
  4. The welcome page needs to be displayed, including two functions: account opening function and login account
public static void main(String[] args) {
        //1. Prepare container objects required by the system for storing account objects
        ArrayList<Account> accounts = new ArrayList<>();


        //2. Prepare the home page of the system: log in and open an account
        showMain(accounts);

    }
public static void showMain(ArrayList<Account> accounts) {
        System.out.println("==================Welcome to the home page======================");
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("Please enter the action you want to do:");
            System.out.println("1,Sign in");
            System.out.println("2,Open an account");
            System.out.print("You can enter the command:");
            int command = sc.nextInt();
            switch(command){
                case 1:
                    //Sign in
                    login(accounts,sc);
                    break;
                case 2:
                    //Open an account
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("The operation command you currently entered is not supported!");
            }
        }
    }

Realization of user account opening function

analysis

The account opening function is actually to deposit the information of a new account object into the collection container of the system

Implementation steps of account opening function

  1. Define method to complete account opening
  2. Enter the name, password and confirm password on the keyboard (the two passwords shall be consistent)
  3. Generate an account card number. The card number must be automatically generated by the system with 8 digits (the uniqueness of the card number must be guaranteed)
  4. Create an Account class object to encapsulate Account information (name, password, card number)
  5. Store the Account class object into the collection accounts
/**
     * User account opening function
     * @param accounts Collection object of account
     * @param sc
     */
    public static void register(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("==================User account opening function======================");
        //2. Enter the name and password on the keyboard to confirm the password
        System.out.println("Please enter your account name:");
        String name=sc.next();

        String passWord="";
        while (true){
            System.out.println("Please enter your account opening password:");
            passWord=sc.next();
            System.out.println("Please enter your confirmation password:");
            String okPassword=sc.next();
            //Judge whether the passwords entered twice are consistent
            if(okPassword.equals(passWord)){
                break;
            }else{
                System.out.println("The two passwords must be the same~~~~~");
            }
        }
            System.out.println("Please enter the current limit:");
            double quotaMoney=sc.nextDouble();

            //3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers
            String cardId=createCardId(accounts);

            //4. Create an account object to encapsulate the information of the account
            //    public Account(String cardId, String userName, String passWord, double quotaMoney)
            Account account=new Account(cardId,name,passWord,quotaMoney);

            //5. Add the account object to the collection
            accounts.add(account);
            System.out.println("Congratulations on your successful account opening. Your card number is:"+account.getCardId()+",Please keep it properly");


    }

public static String createCardId(ArrayList<Account> accounts){
        while (true) {
            //Generate 8-digit random number to represent the card number
            String cardId="";
            Random r=new Random();
            for (int i = 0; i < 8; i++) {
                cardId+=r.nextInt(10);
            }

            //Judge whether the card number is repeated
            Account acc=getAccountByCardId(cardId,accounts);
            if(acc==null){
                //Indicates that the current card number is not duplicate
                return cardId;
            }
        }
    }
public static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
        //Query account object according to card number
        for (int i = 0; i < accounts.size(); i++) {
            Account acc=accounts.get(i);
            if(acc.getCardId().equals(cardId)){
                return acc;
            }
        }
        return null;    //If there is no such account, it means that the card number is not repeated
    }

Realization of user login function

analysis

  1. Define method to complete login
  2. Let the user enter the card number on the keyboard and query the account object according to the card number
  3. If the account object is not found, the card number does not exist, and you will be prompted to continue to enter the card number
  4. If the account object is found, it indicates that the card number exists, and continue to enter the password
  5. If the password is incorrect, you will be prompted to continue entering the password
  6. If the password is correct, you will be prompted to log in successfully!!!
    /**
     * Complete user login
     * @param accounts
     * @param sc
     */
    public static void login(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("==================User login function======================");
        //You must have an account in the system to log in
        if(accounts.size()==0){
            //No account
            System.out.println("There is no account in the current system. You need to register first!");
            return;   //Directly end the execution of the method
        }

        //2. Let the user enter the card number on the keyboard and query the account object according to the card number
        while(true){
            System.out.println("Please enter your login card number:");
            String cardId=sc.next();
            //Query account object according to card number
            Account acc=getAccountByCardId(cardId,accounts);

            //3. Judge whether the account object exists, which indicates that the card number is OK
            if(acc!=null){
                while (true){
                    //4. Let the user enter the password
                    System.out.println("Please enter your login password:");
                    String password=sc.next();
                    //5. Judge whether the password is correct
                    if(acc.getPassWord().equals(password)){
                        //The password is correct and the login is successful
                        //Display the operation interface after system login
                        System.out.println("Congratulations,"+acc.getUserName()+"sir/Ms. successfully entered the system,Your card number is:"+acc.getCardId());
                        //Display operation page
                        showUserCommand(sc,acc,accounts);
                        return;  //Continue end login method
                    }else {
                        System.out.println("Your password is incorrect, please confirm!");
                    }
                }
            }else{
                System.out.println("Sorry, there is no account with this card number!");
            }
        }
    }

User operation page design, account query and account exit function analysis

  1. After the user logs in successfully, you need to enter the user operation page
  2. Query is to directly display the information of the account object that has successfully logged in
  3. To exit the account, you need to go back to the home page
    public static void showUserCommand(Scanner sc, Account acc, ArrayList<Account> accounts) {
        while (true) {
            System.out.println("==================User interface======================");
            System.out.println("1,query");
            System.out.println("2,deposit");
            System.out.println("3,withdraw money");
            System.out.println("4,transfer accounts");
            System.out.println("5,Change Password");
            System.out.println("6,sign out");
            System.out.println("7,Cancellation of account");
                System.out.println("Please enter the operation command:");
                int command=sc.nextInt();
                switch (command){
                    case 1:
                        //Query account
                        showAccount(acc);
                        break;
                    case 2:
                        //deposit
                        depositMoney(acc,sc);
                        break;
                    case 3:
                        //withdraw money
                        drawMoney(acc,sc);
                        break;
                    case 4:
                        //transfer accounts
                        transferMoney(acc,accounts,sc);
                        break;
                    case 5:
                        //Change Password
                        updatePassWord(acc,sc);
                        return;
                    case 6:
                        //sign out
                        System.out.println("looking forward to your next visit!!");
                        return;  //Method to end the current operation!
                    case 7:
                        //Cancellation of account
                        //Erase the current account object from the current collection
                        accounts.remove(acc);
                        System.out.println("Account cancellation succeeded!");
                        return;  //Method to end the current operation
                    default:
                        System.out.println("Your command input is incorrect~~~~");
                }
        }
    }

public static void showAccount(Account acc) {
        System.out.println("==================Current account details======================");
        System.out.println("Card No.:"+acc.getCardId());
        System.out.println("full name:"+acc.getUserName());
        System.out.println("Balance:"+acc.getMoney());
        System.out.println("Current cash withdrawal limit:"+acc.getQuotaMoney());

    }


Realization of user deposit function

Deposit analysis

  1. Deposit is to get the current account object
  2. Then let the user enter the amount of deposit
  3. Call the setMoney method of the account object to modify the account balance into the amount after saving
  4. After saving money, you need to query the account information to confirm whether the money has been saved successfully!
/**
     * save money
     * @param acc
     * @param sc
     */
    public static void depositMoney(Account acc,Scanner sc){
        System.out.println("==================Saving operation======================");
        System.out.println("Please enter the deposit amount:");
        double money=sc.nextDouble();

        //Directly modify the amount to the money attribute of the account object
        acc.setMoney(acc.getMoney()+money);
        System.out.println("Deposit completed!");
        showAccount(acc);
    }

Realization of user withdrawal function

Withdrawal analysis

  1. To withdraw money, you need to judge whether the account has money
  2. If you have money, you get your account object
  3. Then let the user enter the withdrawal amount
  4. Judge whether the withdrawal amount exceeds the current limit and whether the balance is sufficient
  5. If the requirements are met, call the setMoney method of the account object to modify the amount
    /**
     * withdraw money
     * @param acc
     * @param sc
     */
    public static void drawMoney(Account acc, Scanner sc) {
        System.out.println("==================Withdrawal operation======================");
        //1. Judge whether its account is enough for 100 yuan
        if(acc.getMoney()>=100){
            while (true) {
                System.out.println("Please enter the withdrawal amount:");
                double money=sc.nextDouble();
                //2. Judge whether the amount exceeds the current limit
                if(money>acc.getQuotaMoney()){
                    System.out.println("If the withdrawal amount exceeds the limit of each time, you should not withdraw so much. You can withdraw at most:"+acc.getQuotaMoney());
                }else{
                    //3. Determine whether the current balance is enough for you to withdraw money
                    if(acc.getMoney()>=money){
                        //Enough money to withdraw
                        acc.setMoney(acc.getMoney()-money);
                        System.out.println("Congratulations, withdraw the money"+money+"succeed! Current account remaining:"+acc.getMoney());
                        return; //Kill the withdrawal method after withdrawing the money
                    }else{
                        System.out.println("Insufficient balance!");
                    }
                }
            }
        }else{
            System.out.println("Don't withdraw if your balance doesn't exceed 100 yuan~~~");
        }

    }



User transfer function

analysis

  1. The transfer function needs to judge whether there are two or more account objects in the system
  2. At the same time, you should also judge whether your account has money
  3. Next, you need to enter the opposite card number to judge whether the opposite account exists
  4. If the opposite account exists, you also need to verify the last name of the opposite head of household
  5. If you meet the requirements, you can modify the amount of your own account object to the opposite account object
    /**
     * Transfer function
     * @param acc
     * @param accounts
     * @param sc
     */
    public static void transferMoney(Account acc, ArrayList<Account> accounts, Scanner sc) {
        //1. Judge whether there are two or more accounts in the system
        if(accounts.size()<2){
            System.out.println("Sorry, there are no other accounts in the system, you can't transfer!");
            return;
        }
        //2. Judge whether there is money in your account object
        if(acc.getMoney()==0){
            System.out.println("Sorry, you don't have any money, so don't make it~~");
            return;
        }

        //3. Start transfer logic
        while (true) {
            System.out.println("Please enter the card number of the other party:");
            String cardId=sc.next();
            Account account=getAccountByCardId(cardId,accounts);
            //Judge whether this account object exists, which indicates that the opposite card number is entered correctly
            if(account!=null){
                //Judge whether this account object is the currently logged in account
                if(account.getCardId()==acc.getCardId()){
                    //I'm transferring money to myself
                    System.out.println("You cannot transfer money for yourself!");
                }else{
                    //Confirm the last name of the other party
                    String name="*"+account.getUserName().substring(1);
                    System.out.println("Please confirm["+name+"]Last name:");
                    String preName=sc.next();
                    //judge
                    if(account.getUserName().startsWith(preName)){
                        //It's really starting to transfer money
                        System.out.println("Please enter the transfer amount:");
                        double money=sc.nextDouble();
                        //Judge whether this amount exceeds your balance
                        if(money>acc.getMoney()){
                            System.out.println("Sorry, you have too much money to transfer. You can transfer at most"+acc.getMoney());
                        }else{
                            //You can really transfer money
                            acc.setMoney(acc.getMoney()-money);
                            account.setMoney(account.getMoney()+money);
                            System.out.println("Congratulations, the transfer has been successful and has been"+account.getUserName()+"transfer accounts"+money);
                            showAccount(acc);
                            return;
                        }
                    }else {
                        System.out.println("Sorry, your authentication information is incorrect~~~");
                    }
                }
            }else{
                System.out.println("Sorry, there is a problem with the transfer card number you entered!");
            }
        }
    }






Modification of user password and Realization of account cancellation function

analysis

  1. Changing the password is to update the password attribute of the current object with the set method
  2. Account cancellation is to delete the current object from the collection object and return to the home page
    /**
     * Change Password
     * @param acc
     * @param sc
     */
    public static void updatePassWord(Account acc, Scanner sc) {
        System.out.println("==================Change Password======================");
        while (true) {
            System.out.println("Please enter the correct password:");
            String okPassWord=sc.next();
            //Determine whether the password is correct
            if(acc.getPassWord().equals(okPassWord)){
                while (true) {
                    //You can enter a new password
                    System.out.println("Please enter a new password:");
                    String newPassWord=sc.next();

                    System.out.println("Please enter your confirmation password:");
                    String okNewPassWord=sc.next();

                    if(newPassWord.equals(okNewPassWord)){
                        //Change the password of the account object to the new password
                        acc.setPassWord(newPassWord);
                        return;  //End it directly!!
                    }else{
                        System.out.println("The passwords you entered twice are inconsistent~~");
                    }
                }
            }else{
                System.out.println("The password currently entered is incorrect~~~");
            }
        }

    }





==========================================================================

All codes

package ATM2;

/**
 * Account class
 */
public class Account {
    private String cardId;   //Card number
    private String userName;  //Customer name
    private String passWord;  //password
    private double money;  //balance
    private double quotaMoney;  // Current cash withdrawal limit

    public Account() {
    }

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

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public double getMoney() {
        return money;
    }

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

    public double getQuotaMoney() {
        return quotaMoney;
    }

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

package ATM2;

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

public class ATMSystem {
    public static void main(String[] args) {
        //1. Prepare container objects required by the system for storing account objects
        ArrayList<Account> accounts = new ArrayList<>();


        //2. Prepare the home page of the system: log in and open an account
        showMain(accounts);

    }

    public static void showMain(ArrayList<Account> accounts) {
        System.out.println("==================Welcome to the home page======================");
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("Please enter the action you want to do:");
            System.out.println("1,Sign in");
            System.out.println("2,Open an account");
            System.out.print("You can enter the command:");
            int command = sc.nextInt();
            switch(command){
                case 1:
                    //Sign in
                    login(accounts,sc);
                    break;
                case 2:
                    //Open an account
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("The operation command you currently entered is not supported!");
            }
        }
    }

    /**
     * Complete user login
     * @param accounts
     * @param sc
     */
    public static void login(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("==================User login function======================");
        //You must have an account in the system to log in
        if(accounts.size()==0){
            //No account
            System.out.println("There is no account in the current system. You need to register first!");
            return;   //Directly end the execution of the method
        }

        //2. Let the user enter the card number on the keyboard and query the account object according to the card number
        while(true){
            System.out.println("Please enter your login card number:");
            String cardId=sc.next();
            //Query account object according to card number
            Account acc=getAccountByCardId(cardId,accounts);

            //3. Judge whether the account object exists, which indicates that the card number is OK
            if(acc!=null){
                while (true){
                    //4. Let the user enter the password
                    System.out.println("Please enter your login password:");
                    String password=sc.next();
                    //5. Judge whether the password is correct
                    if(acc.getPassWord().equals(password)){
                        //The password is correct and the login is successful
                        //Display the operation interface after system login
                        System.out.println("Congratulations,"+acc.getUserName()+"sir/Ms. successfully entered the system,Your card number is:"+acc.getCardId());
                        //Display operation page
                        showUserCommand(sc,acc,accounts);
                        return;  //Continue end login method
                    }else {
                        System.out.println("Your password is incorrect, please confirm!");
                    }
                }
            }else{
                System.out.println("Sorry, there is no account with this card number!");
            }
        }
    }

    public static void showUserCommand(Scanner sc, Account acc, ArrayList<Account> accounts) {
        while (true) {
            System.out.println("==================User interface======================");
            System.out.println("1,query");
            System.out.println("2,deposit");
            System.out.println("3,withdraw money");
            System.out.println("4,transfer accounts");
            System.out.println("5,Change Password");
            System.out.println("6,sign out");
            System.out.println("7,Cancellation of account");
                System.out.println("Please enter the operation command:");
                int command=sc.nextInt();
                switch (command){
                    case 1:
                        //Query account
                        showAccount(acc);
                        break;
                    case 2:
                        //deposit
                        depositMoney(acc,sc);
                        break;
                    case 3:
                        //withdraw money
                        drawMoney(acc,sc);
                        break;
                    case 4:
                        //transfer accounts
                        transferMoney(acc,accounts,sc);
                        break;
                    case 5:
                        //Change Password
                        updatePassWord(acc,sc);
                        return;
                    case 6:
                        //sign out
                        System.out.println("looking forward to your next visit!!");
                        return;  //Method to end the current operation!
                    case 7:
                        //Cancellation of account
                        //Erase the current account object from the current collection
                        accounts.remove(acc);
                        System.out.println("Account cancellation succeeded!");
                        return;  //Method to end the current operation
                    default:
                        System.out.println("Your command input is incorrect~~~~");
                }
        }
    }

    /**
     * Change Password
     * @param acc
     * @param sc
     */
    public static void updatePassWord(Account acc, Scanner sc) {
        System.out.println("==================Change Password======================");
        while (true) {
            System.out.println("Please enter the correct password:");
            String okPassWord=sc.next();
            //Determine whether the password is correct
            if(acc.getPassWord().equals(okPassWord)){
                while (true) {
                    //You can enter a new password
                    System.out.println("Please enter a new password:");
                    String newPassWord=sc.next();

                    System.out.println("Please enter your confirmation password:");
                    String okNewPassWord=sc.next();

                    if(newPassWord.equals(okNewPassWord)){
                        //Change the password of the account object to the new password
                        acc.setPassWord(newPassWord);
                        return;  //End it directly!!
                    }else{
                        System.out.println("The passwords you entered twice are inconsistent~~");
                    }
                }
            }else{
                System.out.println("The password currently entered is incorrect~~~");
            }
        }

    }

    /**
     * Transfer function
     * @param acc
     * @param accounts
     * @param sc
     */
    public static void transferMoney(Account acc, ArrayList<Account> accounts, Scanner sc) {
        //1. Judge whether there are two or more accounts in the system
        if(accounts.size()<2){
            System.out.println("Sorry, there are no other accounts in the system, you can't transfer!");
            return;
        }
        //2. Judge whether there is money in your account object
        if(acc.getMoney()==0){
            System.out.println("Sorry, you don't have any money, so don't make it~~");
            return;
        }

        //3. Start transfer logic
        while (true) {
            System.out.println("Please enter the card number of the other party:");
            String cardId=sc.next();
            Account account=getAccountByCardId(cardId,accounts);
            //Judge whether this account object exists, which indicates that the opposite card number is entered correctly
            if(account!=null){
                //Judge whether this account object is the currently logged in account
                if(account.getCardId()==acc.getCardId()){
                    //I'm transferring money to myself
                    System.out.println("You cannot transfer money for yourself!");
                }else{
                    //Confirm the last name of the other party
                    String name="*"+account.getUserName().substring(1);
                    System.out.println("Please confirm["+name+"]Last name:");
                    String preName=sc.next();
                    //judge
                    if(account.getUserName().startsWith(preName)){
                        //It's really starting to transfer money
                        System.out.println("Please enter the transfer amount:");
                        double money=sc.nextDouble();
                        //Judge whether this amount exceeds your balance
                        if(money>acc.getMoney()){
                            System.out.println("Sorry, you have too much money to transfer. You can transfer at most"+acc.getMoney());
                        }else{
                            //You can really transfer money
                            acc.setMoney(acc.getMoney()-money);
                            account.setMoney(account.getMoney()+money);
                            System.out.println("Congratulations, the transfer has been successful and has been"+account.getUserName()+"transfer accounts"+money);
                            showAccount(acc);
                            return;
                        }
                    }else {
                        System.out.println("Sorry, your authentication information is incorrect~~~");
                    }
                }
            }else{
                System.out.println("Sorry, there is a problem with the transfer card number you entered!");
            }
        }
    }

    /**
     * withdraw money
     * @param acc
     * @param sc
     */
    public static void drawMoney(Account acc, Scanner sc) {
        System.out.println("==================Withdrawal operation======================");
        //1. Judge whether its account is enough for 100 yuan
        if(acc.getMoney()>=100){
            while (true) {
                System.out.println("Please enter the withdrawal amount:");
                double money=sc.nextDouble();
                //2. Judge whether the amount exceeds the current limit
                if(money>acc.getQuotaMoney()){
                    System.out.println("If the withdrawal amount exceeds the limit of each time, you should not withdraw so much. You can withdraw at most:"+acc.getQuotaMoney());
                }else{
                    //3. Determine whether the current balance is enough for you to withdraw money
                    if(acc.getMoney()>=money){
                        //Enough money to withdraw
                        acc.setMoney(acc.getMoney()-money);
                        System.out.println("Congratulations, withdraw the money"+money+"succeed! Current account remaining:"+acc.getMoney());
                        return; //Kill the withdrawal method after withdrawing the money
                    }else{
                        System.out.println("Insufficient balance!");
                    }
                }
            }
        }else{
            System.out.println("Don't withdraw if your balance doesn't exceed 100 yuan~~~");
        }

    }

    /**
     * save money
     * @param acc
     * @param sc
     */
    public static void depositMoney(Account acc,Scanner sc){
        System.out.println("==================Saving operation======================");
        System.out.println("Please enter the deposit amount:");
        double money=sc.nextDouble();

        //Directly modify the amount to the money attribute of the account object
        acc.setMoney(acc.getMoney()+money);
        System.out.println("Deposit completed!");
        showAccount(acc);
    }

    public static void showAccount(Account acc) {
        System.out.println("==================Current account details======================");
        System.out.println("Card No.:"+acc.getCardId());
        System.out.println("full name:"+acc.getUserName());
        System.out.println("Balance:"+acc.getMoney());
        System.out.println("Current cash withdrawal limit:"+acc.getQuotaMoney());

    }

    /**
     * User account opening function
     * @param accounts Collection object of account
     * @param sc
     */
    public static void register(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("==================User account opening function======================");
        //2. Enter the name and password on the keyboard to confirm the password
        System.out.println("Please enter your account name:");
        String name=sc.next();

        String passWord="";
        while (true){
            System.out.println("Please enter your account opening password:");
            passWord=sc.next();
            System.out.println("Please enter your confirmation password:");
            String okPassword=sc.next();
            //Judge whether the passwords entered twice are consistent
            if(okPassword.equals(passWord)){
                break;
            }else{
                System.out.println("The two passwords must be the same~~~~~");
            }
        }
            System.out.println("Please enter the current limit:");
            double quotaMoney=sc.nextDouble();

            //3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers
            String cardId=createCardId(accounts);

            //4. Create an account object to encapsulate the information of the account
            //    public Account(String cardId, String userName, String passWord, double quotaMoney)
            Account account=new Account(cardId,name,passWord,quotaMoney);

            //5. Add the account object to the collection
            accounts.add(account);
            System.out.println("Congratulations on your successful account opening. Your card number is:"+account.getCardId()+",Please keep it properly");


    }

    public static String createCardId(ArrayList<Account> accounts){
        while (true) {
            //Generate 8-digit random number to represent the card number
            String cardId="";
            Random r=new Random();
            for (int i = 0; i < 8; i++) {
                cardId+=r.nextInt(10);
            }

            //Judge whether the card number is repeated
            Account acc=getAccountByCardId(cardId,accounts);
            if(acc==null){
                //Indicates that the current card number is not duplicate
                return cardId;
            }
        }
    }

    public static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
        //Query account object according to card number
        for (int i = 0; i < accounts.size(); i++) {
            Account acc=accounts.get(i);
            if(acc.getCardId().equals(cardId)){
                return acc;
            }
        }
        return null;    //If there is no such account, it means that the card number is not repeated
    }

}

Keywords: Java OOP

Added by elearnindia on Thu, 03 Mar 2022 07:38:22 +0200