Java: A New Path-Bank Account Management System

topic

Design a bank account management system

Welcome to use the bank account management system
1. Log in
2. Register
3. Log out
Please enter the operation to be performed


Current user: xxx
1. Check account balance
2. Transfer in
3. Expense
4. Check transaction record
5. Exit
Please enter the operation to be performed

Since it is new in Java, this question only needs to complete the above operations, and it does not need to be too complete. The code provided below realizes the registration of multiple accounts without causing data confusion on the basis of completing the requirements of the above questions. There are many passwords during registration. For six persons, those who need it can add it by themselves. If you need a mixture of numbers and letters , verify whether the user name exists and whether the password matches when logging in. However, if the user name is duplicated during registration, you can add it yourself.

The use of HashMapfunctions in this question is relatively simple.

If there are errors or trouble writing, please discuss in the comment area, thank you.

Code

BankUser.java

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

public class BankUser {
    
    
    ArrayList<Double> record = new ArrayList<Double>();
    Scanner in = new Scanner(System.in);

    private String userName;            //用户名
    private String password;            //密码
    double sum = 0.0;                   //银行账户总额
    double input;                       //转入金额
    double output;                      //支出金额

    public BankUser(String userName, String password) {
    
    
        this.userName = userName;
        this.password = password;
    }

    public void userResearch(String number){
    
                    //用于实现银行的基本业务,即登录后的相关操作
        if(number.equals("1")){
    
                                 //查询账户余额
            System.out.println("账户余额为"+sum);
        }
        else if(number.equals("2")){
    
                             //转入金额
            System.out.print("请输入转入多少金额:");
            input = in.nextDouble();
            sum += input;
            record.add(input);
            System.out.println("交易完成!");
        }
        else if(number.equals("3")){
    
                             //支出金额
            System.out.println("请输入支出多少金额:");
            output = in.nextDouble();
            sum -= output;
            record.add(-output);
            System.out.println("交易完成!");
        }
        else if(number.equals("4")){
    
                              //交易明细
            for(int j=1;j<=record.size();j++){
    
    
                if(record.get(j-1)>=0.0)
                    System.out.println(j+". +"+record.get(j-1));
                else
                    System.out.println(j+". "+record.get(j-1));
            }

        }
        else{
    
                                                     //返回上个界面
            number="5";
        }
    }

    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;
    }
}

Register.java

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

public class Register {
    
    
    ArrayList<BankUser> useRegister = new ArrayList<BankUser>();
    Scanner in = new Scanner(System.in);

    String name;        //用户名
    String word;        //密码

    public void addUser(BankUser user){
    
                 //将注册的信息添加进数组
        useRegister.add(user);
    }

    public void registerUser(){
    
                         //注册
        System.out.print("请输入用户名:");
        name = in.nextLine();
        System.out.print("请输入密码:");
        word=in.nextLine();

        while(true){
    
                                    //密码不少于6位
            if(word.length()<6){
    
    
                System.out.println("密码长度不能小于6位,请重新输入:");
                word=in.nextLine();
            }
            else
                break;
        }

        BankUser customer = new BankUser(name,word);
        addUser(customer);
        System.out.println("注册成功!");
    }
}

Land.java

import java.util.Scanner;

public class Land {
    
    
    Scanner in = new Scanner(System.in);

    String name;                //用户名
    String word;                //密码
    String number;              //操作选项
    Register register;

    public Land (Register register){
    
    
        this.register=register;
    }

    public void userLand(){
    
                     //登录
        System.out.print("用户名:");
        name = in.nextLine();
        System.out.print("密码:");
        word = in.nextLine();

        while (true){
    
    
            int i = 0;          //用于标记是否登录成功,方便退出循环
            int k = 0;
            for(BankUser user:register.useRegister){
    
    
                if(user.getUserName().equals(name)){
    
    
                    while (true){
    
    
                        int n = 0;
                        if(user.getPassword().equals(word)){
    
    
                            System.out.println("登录成功!");
                            i = 1;
                            n = 1;

                            /*--------------登录之后的界面--------------*/
                            System.out.println("当前用户:"+name);
                            System.out.println("1.查询账户余额");
                            System.out.println("2.转入");
                            System.out.println("3.支出");
                            System.out.println("4.查询交易记录");
                            System.out.println("5.退出");
                            System.out.println("请输入要执行的操作:");
                            /*----------------------------------------*/

                            while(true){
    
                           //实现银行职能重复进行
                                number = in.nextLine();
                                user.userResearch(number);
                                if(number.equals("5")){
    
    
                                    k = 0;
                                    break;
                                }
                            }

                        }
                        else{
    
    
                            System.out.print("密码错误,请重新输入:");
                            word = in.nextLine();
                        }
                        if(n==1)break;
                    }
                }
                if(i==1)break;
                k++;
            }

            if(k==register.useRegister.size()){
    
                     //遍历数组,若没相同用户名即不存在
                System.out.println("用户名不存在,请重新输入!");
                System.out.print("用户名:");
                name = in.nextLine();
                System.out.print("密码:");
                word = in.nextLine();
            }

            if(i==1)break;
        }
    }
}

Test.java

import java.util.Scanner;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Register register = new Register();
        Land land = new Land(register);
        Scanner in = new Scanner(System.in);

        String number;

        while(true){
    
    
            
          /*------------------初始界面------------------*/  
            System.out.println("欢迎使用银行账户管理系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.print("请输入要执行的操作:");
            number = in.nextLine();
          /*--------------------------------------------*/

            if(number.equals("2")){
    
                     //注册
                register.registerUser();
            }
            else if(number.equals("1")){
    
                //登录
                land.userLand();
            }
            else if(number.equals("3"))             //退出
                break;
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_46263782/article/details/107708896