JavaSE operation: automatic teller machine, source code

1. The topic requirements of the simple automatic teller machine:

  1. Create a user class User (including card number, name, password, balance and other attributes), the name and password entered when the user opens the card (a card number is automatically assigned, and the initial amount is set to 0).
  2. Use ArrayList or LinkedList to store all registered users,
    such as:ArrayList<User> userList = new ArrayList<User>();
  3. Realization of functions
    (1) Deposit
    (2) Withdrawal (if the balance is insufficient, please prompt)
    (3) Check the balance (only check the balance of the current card number)
  4. Technical requirements, use exceptions to handle user's wrong input (that is, program protection fault tolerance).

2. Thinking process

The teacher said that when writing a program, the most important thing is thinking . After thinking, everything will become more specific. This is a necessary process. When we think about things, we can better think about the logical relationship between things by writing them down.
So when we think about some simple problems, we should also think about how these are actually achieved. How does each step of this come about? This is very important.

Homework brain map

Three, code implementation

IUserManager interface: used to write some abstract methods/user collections

public interface IUserManager {
    
    
    ArrayList<UserBean> ulist = new ArrayList<UserBean>();

}

UserBean class: user standard class

ID, Name, password, money
toString: provide a method to view the current state of the class
userBean construction method: need to provide the user name and the money to be saved now

package com.hzyc.hw1209;

public class UserBean {
    
    
    private int ID;
    private String Name;
    private String Password;
    private int money;

    public UserBean(int ID, String Name, int money) {
    
    
        this.setID(ID);
        this.setName(Name);
        this.setMoney(money);
    }

    public String toString() {
    
    
        return "卡号为[" + this.ID + "]的用户\n" +
                this.Name + "的账户余额为:¥" + this.money;
    }

    public int getID() {
    
    
        return ID;
    }

    public void setID(int ID) {
    
    
        this.ID = ID;
    }

    public String getName() {
    
    
        return Name;
    }

    public void setName(String name) {
    
    
        Name = name;
    }

    public String getPassword() {
    
    
        return Password;
    }

    public void setPassword(String password) {
    
    
        Password = password;
    }

    public int getMoney() {
    
    
        return money;
    }

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

UserManager: User management class

NewUser card opening method (including a newCountMoney: provides a method for deducting card opening fees)
saveMoney: provides a deposit method
getMoney: provides a method for withdrawing money, if the amount of money withdrawn exceeds, it cannot be withdrawn.
checkMoney: Provides a money method for viewing [current account]
findUser: Provides a login method, finds your account from ulist according to your own password.
checkpsw: Provides a method to check whether the password is correct, and is used to verify the correctness of the password.

package com.hzyc.hw1209;

import com.sun.xml.internal.bind.v2.model.core.ID;

import java.util.Scanner;

public class UserManager implements IUserManager {
    
    
    Scanner sc = new Scanner(System.in);
    int flag = 2000;

    public int findUser(int ID) {
    
    
        try {
    
    
            if (ID > flag) {
    
    
                System.out.println("输入有误,请重新输入用户ID");
                findUser(sc.nextInt());
            }
            //核验密码成功,则返回正确的index
            if (checkpsw(ID)) {
    
    
                int tub = 0;
                for (int i = 0; i < ulist.size(); i++) {
    
    
                    if (ulist.get(i).getID() == ID) {
    
    
                        tub = i;
                    }
                }
                return tub;
            }
        } catch (Exception e) {
    
    
        }

        return -1;
    }

    public boolean checkpsw(int ID) {
    
    
        System.out.println("请输入密码以核验身份");
        String str = sc.next();
        int index = 0;
        //遍历所有用户,找到对应的ID
        for (int i = 0; i < ulist.size(); i++) {
    
    
            if (ulist.get(i).getID() == ID) {
    
    
                index = i;
            }
        }
        return ulist.get(index).getPassword().equals(str);
    }

    public void newUser() {
    
    
        System.out.println("现在正在为您开卡,请输入姓名和储蓄:【姓名-金钱-密码】");
        try {
    
    
            String str = sc.next();
            String[] newStr = str.split("-");
            flag += 4;
            UserBean user = new UserBean(flag, newStr[0], Integer.parseInt(newStr[1]));
            //为用户设置密码
            user.setPassword(newStr[2]);
            //开户
            newCountMoney(user);
            ulist.add(user);
            System.out.println("开卡成功,现在" + user);

        } catch (Exception e) {
    
    
            System.out.println("输入有误,请重新输入");
        }

    }

    public void newCountMoney(UserBean u) {
    
    
        u.setMoney(u.getMoney() - 5);
        System.out.println("扣除开卡费:¥5,成功!");
    }

    public void saveMoney(int index) {
    
    
        System.out.println("请输入您想要存储多少钱");
        try {
    
    
            ulist.get(index).setMoney(ulist.get(index).getMoney() + sc.nextInt());
            System.out.println("存钱成功!");
            System.out.println(ulist.get(index));
            System.out.println("-----------------------------");

        } catch (Exception e) {
    
    
            System.out.println("您的存钱有误,请存入整数的钱");
        }
    }

    public void getMoney(int index) {
    
    
        System.out.println("请输入您想要取出多少钱");
        int moneybe = ulist.get(index).getMoney() - sc.nextInt();
        if (moneybe < 0) {
    
    
            System.out.println("对不起,您的存款不够");
        } else {
    
    
            ulist.get(index).setMoney(moneybe);
        }
    }

    public void checkMoney(int index) {
    
    
        try {
    
    
            System.out.println("您现在的账户状态为:");
            System.out.println(ulist.get(index));
        } catch (Exception e) {
    
    
            System.out.println("密码错误!!正在返回系统主页面");
        }
    }
}

Workflow process class

Star as the outer menu choice: 1. Account opening process; 2. Login process;
loginFlow is the process of [password verification].
To log in == verify that your account information is
correct, you can enter your account for the next step, and return if the password is incorrect Main menu (star method)
loggedFlow successfully logged in to enter this menu.
After logging in, the user’s menu:
[Process]: 1. Deposit; 2. Withdraw; 3. View; 4. Exit;

import java.util.Scanner;

public class Workflow {
    
    
    Scanner sc = new Scanner(System.in);
    UserManager mgr = new UserManager();
    int index = 0;

    void star() {
    
    
        System.out.println("\n\n*************欢迎使用本系统****************\n" +
                "请开始您的操作:【1】开户【2】登录");
        int choose1 = sc.nextInt();
        switch (choose1) {
    
    
            case 1:
                mgr.newUser();
                star();
                break;
            case 2:
                //核对密码,密码错误则返回主菜单
                loginFlow();
                break;
        }

        loggedFlow();

    }

    void loginFlow() {
    
    
        System.out.println("请输入您的卡号:");
        Scanner sc = new Scanner(System.in);
        index = mgr.findUser(sc.nextInt());
        System.out.println("您的账户状态为:");
        mgr.checkMoney(index);
        if (index == -1){
    
    
            star();
        }
    }

    void loggedFlow() {
    
    
        System.out.println("请进行您的操作:");
        outhere:
        while (true) {
    
    
            System.out.println("\n\n*************欢迎多存钱****************\n" +
                    "请选择操作:【1】存款【2】取款【3】查询余额【4】退出系统");
            try {
    
    
                int choose = sc.nextInt();
                switch (choose) {
    
    
                    case 1:
                        mgr.saveMoney(index);
                        break;
                    case 2:
                        mgr.getMoney(index);
                        break;
                    case 3:
                        mgr.checkMoney(index);
                        break;
                    case 4:
                        break outhere;
                }
            } catch (Exception e) {
    
    
                System.out.println("您的输入有误,请重新输入。");
            }
        }
        star();
    }
}

Mediocre test

public class tst {
    
    
    public static void main(String[] args) {
    
    
        Workflow w =new Workflow();
        w.star();
    }
}

四、have a try!

Insert picture description here

5. Summary:

1. Procedure

IUserManager
	userManager
		newUser开卡方法
		newCountMoney\n提供扣除开卡费方法
		saveMoney\n提供存钱方法
		getMoney\n提供取钱方法,如果取钱数目超过,则不可以被取出。
		checkMoney\n提供查看【当前账户】的钱财方法
		findUser\n提供登录方法,根据自己的密码从ulist里面找到自己的账户
		checkpsw\n提供一个查看密码是否正确的方法,用于核验密码的正确性。
	userBean
		ID、Name、password、money
		标准用户类
		toString\n提供一个查看当前类状态的方法
		userBean构造方法:\n需要提供用户名称和现在要存的钱
	Workflow
		star作为外层菜单\n选择:\n1.开户流程;\n2.登录流程;
		loginFlow是核验密码的流程\n需要登录==核对自己的账户信息\n正确则可以进入自己的账户进行下一步操作,密码错误则返回主菜单(star方法)
		loggedFlow成功登录进入该菜单,登录后用户的菜单\n【流程】:\n1.存款;\n2.取款;\n3.查看;\n4.退出;

2. Understanding of trycatch

In this program, I added a lot of try/catch statements, and deepened my understanding of trycatch: The
try-catch statement marks the statement block to be tried, and specifies a response that is thrown when an exception occurs.

Personally think the role of trycatch:
(1) It prevents the program from exiting suddenly in normal use and can strengthen the robustness of the program.
(2) It allows you to correct errors.

We can assume a scenario, if we are using Taobao, while using the computer, the program will not pop up a window telling us, "Your password is entered incorrectly, please re-enter", but it will crash all at once. , The entire page is gone, one can imagine the importance of trycatch.

3.Tips:

Throwable class is the parent class of Error and Exception
Try

Guess you like

Origin blog.csdn.net/weixin_43801418/article/details/111043103