Java simple ATM system

Outline
   ATM system 
                  1. Registration (account (the system randomly generates 15) - code (6) - Balance) 
                  2. Log
                  4. Change Password
                  3. balance inquiry
                  4. save - (balance after completion of the operation display) 
                  5 . withdrawals - (displayed after the operation is complete balance)
                  6. exit 
                7. support via card transfer function (unfinished)
                  8. persistent data (unfinished)

public class the user {
        // user class
        private String userId; // account
        private String userPassword; // password
        private double money; // balance
        
        public the User () {
                Super ();
        }

        public String getUserId () {
                return the userId;
        }

        public void setUserId(String userId) {
                this.userId = userId;
        }

        public String getUserPassword() {
                return userPassword;
        }

        public void setUserPassword(String userPassword) {
                this.userPassword = userPassword;
        }

        public double getMoney() {
                return money;
        }

        public void setMoney(double money) {
                this.money = money;
        }
}
//功能类
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.function.DoublePredicate;
import java.util.regex.Pattern;

public class ATMSystem {
        private ArrayList<User> users;
        private Scanner scanner;
        private int loginIndex;

        public ATMSystem() {
                this.users = new ArrayList<>();//分配空间
                this.scanner = new Scanner(System.in);
                stratCaiDan();
        }
        private void stratCaiDan() {
                System.out.println("【1】注册 【2】登录 【3】退出");
                System.out.print(">");
                String result = scanner.nextLine();

                switch (result) {
                case "1":// 注册
                        register();
                        stratCaiDan ();
                        BREAK;
                Case "2": // Sign
                        Login ();
                        BREAK;
                Case ". 3": // Exit
                        BREAK;
                }
        }
        // menu
        Private void bankCaiDan () {
                System.out.println ( "[ 1] [2] check balances save money withdrawals [3] [4] [5] Change password transfers [6] quit ");
                System.out.print ("> ");
                String = scanner.nextLine the Result ();
                
                Switch (Result) {
                Case ". 1": // check balances
                        showMoney ();
                        bankCaiDan ();
                        BREAK;
                Case "2": // save
                        saveMoney ();
                        bankCaiDan ();
                        BREAK;
                Case ". 3": // withdrawals
                        drawMoney ();
                        bankCaiDan ();
                        BREAK;
                Case ". 4": // Change Password
                        updatePassword ();
                        bankCaiDan ();
                        break;
                Case "5": transfer //
                        
                        break;
                Case "6": // quit
                        break;
                }
        }
        
        // check balances
        void showMoney public () {
                System.out.println ( "Balance:" + users.get (loginIndex) .getMoney ());
        }
        
        // save
        public void saveMoney () {
                of System.out.print ( "Please enter the deposit amount: ");
                String = Money scanner.nextLine ();
                users.get (loginIndex) .setMoney (users.get (loginIndex) .getMoney () + Double.parseDouble (Money));
                System.out.println (" deposit success ");
                showMoney ();
        }
        
        // withdrawals
        public void drawMoney () {
                System.out.print (" enter the withdrawal amount: ");
                String = Money scanner.nextLine ();
                if(users.get(loginIndex).getMoney()>=Double.parseDouble(money)) {
                        users.get(loginIndex).setMoney(users.get(loginIndex).getMoney()-                                 Double.parseDouble(money));
                }else {
                        System.out.println("余额不足!");
                        return;
                }
                System.out.println("取款成功");
                showMoney();
        }
        
        //修改密码方法
        public void updatePassword() {
                System.out.print("请输入原密码:");
                String password = scanner.nextLine();
                if(!users.get(loginIndex).getUserPassword().equals(password)) {
                        System.out.println ( "Wrong password!");
                        Return;
                }
                the while (to true) {
                        System.out.print ( "Please enter a new password:"); // set password
                        String newPassword = scanner.nextLine ();
                        IF (! Pattern.matches ( "[0-9]. 6 {}", newPassword)) {
                                System.out.println ( "the password is incorrect, please re-enter");
                                Continue;
                        }
                        of System.out.print ( "Please confirm new password: ");
                        String rePassword = scanner.nextLine ();
                        IF (rePassword.equals (newPassword!)) {
                                System.out.println ( "Your passwords are not the same, please try again");
                                the Continue;
                        }
                        users.get (loginIndex) .setUserPassword (newPassword);
                        System.out.println ( "Change Password successful");
                        BREAK;
                }
        }
        
        
        // login method
        Private void the Login () {
                System.out.print ( "Please enter your card number:");
                String the above mentioned id = scanner.nextLine ();
                int index = getUserIndexById (the above mentioned id);
                IF (index -1 ==) {
                        System.out.println ( "enter your account number does not exist, the check again");
                }
                System.out.print ( "Enter password:");
                ; String password = scanner.nextLine ()
                (. Users.get (index) .getUserPassword () the equals (password)) IF {
                        System.out.println ( "Sign success ");
                        this.loginIndex = index;
// stratCaiDan ();
                        // successful login, jump page function
                        bankCaiDan ();
                } the else {
                        System.out.println (" wrong password, please try again check ");
                        Login ();
                        return;
                }

        }
        
        // get the user subscript method
        private int getUserIndexById (String id) {
                for (int index = 0; index < users.size(); index++) {
                        if (users.get(index).getUserId().equals(id)) {
                                return index;
                        }
                }
                return -1;
        }

        // 注册方法
        private void register() {
                User user = new User();
                Random ran = new Random();
                // 随机生成一个账号
                for (int i = 0; i < 15; i++) {
                        if (i == 0) {
                                user.setUserId(ran.nextInt(9) + 1 + "");
                        } else {
                                user.setUserId (user.getUserId () + ran.nextInt (10));
                        }
                }
                System.out.println ( "Account:" + user.getUserId () + "Keep");
                the while (to true) {
                        the System .out.print ( "enter password:"); // set password
                        String password = scanner.nextLine ();
                        IF (! Pattern.matches ( "[0-9]. 6 {}", password)) {
                                the System. out.println ( "the password is incorrect, please re-enter");
                                Continue;
                        }
                        of System.out.print ( "confirm password:");
                        String rePassword = Scanner.nextLine();
                        (! rePassword.equals (password)) IF {
                                System.out.println ( "Your passwords are not the same, please try again");
                                the Continue;
                        }
                        user.setUserPassword (password);
                        users.add (the User);
                        System.out.println ( "successfully registered account is:." + user.getUserId ());
                        BREAK;
                }
        }
}


// start the test class
public class the test {
        public static void main (String [] args) {
                new new ATMSystem ( );
        }
}

Guess you like

Origin www.cnblogs.com/zhuxiaopijingjing/p/12334690.html