Java implements a simple bank management system (the most basic function / easy to use)

Posted by bruckerrlb on Tue, 25 Jan 2022 07:22:15 +0100


It realizes a most basic bank management system (based on classes and objects), has no complex functions, and is easy for beginners to operate. After the basic framework is implemented, it can continue to enrich its functions, realize more powerful management functions, or realize graphical interface, input and output stream saving, database connection and so on.

1, Introduction

  1. Problem description
    Implement a simple bank savings system to complete the deposit and withdrawal business of current users.

  2. Basic Requirements
    1. Implement the class that describes the bank and records the existing depositors in the system (hint: it can be implemented with an object array, but pay attention to judge whether the array is out of bounds). This class requires three methods:
    (1) Method of generating new depositors;
    (2) Delete the depositor according to the account;
    (3) According to the method of account query of depositors, the query results are required to be displayed.
    2. Define the depositor category. Its properties include account number, depositor's name, ID number, address, and deposit balance. Its operations include deposit, withdrawal and displaying depositor information.
    3. Write test classes to test the functions required above.
    Design a simple menu interface for function selection.

  3. Functions implemented
    1. Add account information
    2. Deposit (withdrawal) operation
    3. Delete account information (search by card number / name)
    4. Read account information (search by card number / name)

2, Detailed design

The storage structure of the user of this program is an object array
1) Desktop class

package BankSystem;
public class Depositor{
	private int number;      /*Card number*/
	private String name;        /*full name*/
	private String personId;          /*ID number*/
	private String address;     /*e-mail address*/
	private String password;        /*password*/
	private float balance;        /*balance*/

	public Depositor(){         /*Constructor without formal parameters, used to initialize*/
        number=0;
        name="0";
        personId="0";
        address="0";
        password="123456";
        balance=0;
    }

    public int shownumber() {return number;}
    public String showname() {return name;}
    public String showid() {return personId;}
    public String showaddress() {return address;}
    public String showkey() {return password;}
    public float showbalance() {return balance;}
    
    public void inputnumber(int number){this.number=number;}
    public void inputname(String name){this.name=name;}
    public void inputid(String id){this.personId=id;}
    public void inputaddress(String address){this.address=address;}
    public void inputkey(String password){this.password=password;}
    public void inputbalance(float balance){this.balance=balance;}

    public void inputbalance(int money){
    	this.balance +=money;
    	System.out.println("You have deposited" + money + "element,The account balance is" + balance );
    }
    
    public void deposit(int money) {
    	this.balance -= money;
    	System.out.println("You have removed" + money + "element,The account balance is" + balance );
    }
    
    public void show() {
    	System.out.println("******************************");
    	System.out.println("*Account created successfully!Your account information is as follows:");
    	System.out.println("*Bank card No.:"+ number );
    	System.out.println("*Password is:"+ password);
    	System.out.println("*Name:"+ name);
    	System.out.println("*The ID number is:"+ personId);
    	System.out.println("*Email address:"+ address);
    	System.out.println("*The account balance is:" + balance);	
    	System.out.println("******************************");
    }
    public void show1() {
    	System.out.println("******************************");
    	System.out.println("*The account information is as follows:");
    	System.out.println("*Bank card No.:"+ number );
    	//System.out.println("* password is:" + password);
    	System.out.println("*Name:"+ name);
    	System.out.println("*The ID number is:"+ personId);
    	System.out.println("*Email address:"+ address);
    	System.out.println("*The account balance is:" + balance);	
    	System.out.println("******************************");
    }

};

2) Add account information

3) Deposit and withdrawal operation

4) Query account information
Step 1 enter the administrator password. Execute step 2 correctly, otherwise execute step 7.
Step2 :
a) Select search by card number and execute step 3,
b) Select name retrieval and execute step 4.
Step 3 output all account card numbers, enter the card number, select an account, and execute step 5.
Step4 output all account names, enter names and select an account.
Step 5 outputs the account information.
Step 6 enter the modification information.
Step7 returns to the main menu.

5) Delete account information
The administrator password and retrieval method are the same as 4), which will not be repeated here.
Step 1 enter the account to be deleted and give a deletion warning. If "yes", execute step 2, and if "no", execute step 5.
Step 2 if the account is the last of the existing object array, execute step 3; otherwise, execute step 4.
Step 3 at the same time, reduce the current COUNT by one and execute step.
Step 4 assigns the information of the last array to the one to be deleted, and reduces the existing COUNT by one.
Step5 returns to the main menu.

6) Data reading and saving (not implemented)
OutStreamWriter and InputStreamReader are used to save the data in txt file, and the data is separated by spaces to realize data reading and saving.

3, Code


1. See the above for the repository class

2.Bank type
The Bank administrator is not implemented by a separate class, but by the manage() method in the Bank class

package BankSystem;
import java.util.Scanner;

public class Bank {
	int i;
	private Depositor[] depositor = new Depositor[100];
	private int accountNum = 0; 
	private String password1;
	int number = 100000;
	int j=1;
	private String manage_password = "@123456";
	Scanner sc = new Scanner(System.in);
	int insert;
	
	public Bank() {
    }
 
    // main interface
    public void mainView() {
        System.out.println("******Welcome to the bank management system********");
        System.out.println("******Please select business***************");
        System.out.println("******1,Create account**************");
        System.out.println("******2,Login account**************");
        System.out.println("******3,Administrator login**************");
        
    }
    
    public void select() {
        int select = sc.nextInt();
        switch(select) {
        case 1 : this.openAccount();
        break;
        case 2 : this.enter();
        break;
        case 3: this.manage();
        break;
      }
    }
    
    
    public void manage() {
    	boolean flag = false;    	
    	System.out.println("Please enter your password");
        String password = sc.next();
        
        if(password.equals(manage_password)) {
        	flag = true;
        }
        
    	if(flag == false) {
    		System.out.println("Login failed!!!");
    	}
    	
    	if(flag == true) {
    		System.out.println("Login succeeded!!!");
    		do {
                System.out.println("Please select an action:");
                System.out.println("******1,Check and modify account information*****************");
                System.out.println("******2,Delete account information*****************");
                System.out.println("******3,press any key to exit*****************");
                
                insert = sc.nextInt();
                switch(insert) {
                case 1 :     //1. Query account information
                	System.out.println("Please select an action:");
                    System.out.println("******1,Search by card number*****************");
                    System.out.println("******2,Search by name*****************");
                    insert = sc.nextInt();
                    
                    switch(insert) {
                    case 1://By card number
                    	for(int i=0;i<accountNum;i++) {
                    		System.out.println("user"+ (++i) +":" + depositor[--i].shownumber());                   		
                    	}
                    	                                      	
                    	System.out.println("Please select a user:");
                    	insert = sc.nextInt();
                    	if(insert-1<accountNum)
                    		depositor[insert-1].show1();
                    	else
                    		System.out.println("No user!");
                    	break;
                    	
                    case 2://By name
                    	for(int i=0;i<accountNum;i++) {
                    		System.out.println("user"+ (++i) +":" + depositor[--i].showname());                   		
                    	}
                    	System.out.println("Please select a user:");
                    	insert = sc.nextInt();
                    	if(insert-1<accountNum)
                    		depositor[insert-1].show1();
                    	else
                    		System.out.println("No user!");
                    	break;
                    }
                                            
                    break;
 
                case 2://  2. Delete account information    
                	
                	System.out.println("Please select an action:");
                    System.out.println("******1,Search by card number*****************");
                    System.out.println("******2,Search by name*****************");
                    insert = sc.nextInt();
                    
                    switch(insert) {
                    case 1://By card number
                    	for(int i=0;i<accountNum;i++) {
                    		System.out.println("user"+ (++i) +":" + depositor[--i].shownumber());                   		
                    	}                                      	
                    	System.out.println("Please select a user:");
                    	insert = sc.nextInt();
                    	if(insert == accountNum){
                    		accountNum--;
                    		
                    		System.out.println("Remaining users:");
                    		for(int i=0;i<accountNum;i++) {
                        		System.out.println("user"+ (++i) +":" + depositor[--i].shownumber());                   		
                        	}
                    		System.out.println("Deleted successfully!");
                    	}	
                    	else {
                    		depositor[insert-1].inputname(depositor[accountNum-1].showname()); 
                    		depositor[insert-1].inputnumber(depositor[accountNum-1].shownumber()); 
                    		depositor[insert-1].inputid(depositor[accountNum-1].showid()); 
                    		depositor[insert-1].inputkey(depositor[accountNum-1].showkey()); 
                    		depositor[insert-1].inputaddress(depositor[accountNum-1].showaddress()); 
                    		depositor[insert-1].inputbalance(depositor[accountNum-1].showbalance()); 
                    		accountNum--;
                    		System.out.println("Remaining users:");
                    		for(int i=0;i<accountNum;i++) {
                        		System.out.println("user"+ (++i) +":" + depositor[--i].shownumber());                   		
                        	}
                    		System.out.println("Deleted successfully!");
                    	}
                    		
                    	//depositor[insert-1].show1();
                    	break;
                    	
                    case 2://By name
                    	for(int i=0;i<accountNum;i++) {
                    		System.out.println("user"+ (++i) +":" + depositor[--i].showname());                   		
                    	}
                    	System.out.println("Please select a user:");
                    	insert = sc.nextInt();
                    	
                    	if(insert == accountNum){
                    		accountNum--;
                    		System.out.println("Remaining users:");
                    		for(int i=0;i<accountNum;i++) {
                        		System.out.println("user"+ (++i) +":" + depositor[--i].showname());                   		
                        	}
                    		System.out.println("Deleted successfully!");
                    		
                    	}	
                    	else {
                    		depositor[insert-1].inputname(depositor[accountNum-1].showname()); 
                    		depositor[insert-1].inputnumber(depositor[accountNum-1].shownumber()); 
                    		depositor[insert-1].inputid(depositor[accountNum-1].showid()); 
                    		depositor[insert-1].inputkey(depositor[accountNum-1].showkey()); 
                    		depositor[insert-1].inputaddress(depositor[accountNum-1].showaddress()); 
                    		depositor[insert-1].inputbalance(depositor[accountNum-1].showbalance()); 
                    		accountNum--;
                    		System.out.println("Remaining users:");
                    		for(int i=0;i<accountNum;i++) {
                        		System.out.println("user"+ (++i) +":" + depositor[--i].showname());                   		
                        	}
                    		System.out.println("Deleted successfully!");
                    	}
                    	break;
                    }
                                            
                    break;
                	
 
                }
            } while(insert == 1 || insert == 2 );
    	}
    	
    }
    
    public Depositor openAccount(){
    	System.out.println("Please enter your name");
        String name = sc.next();
        
        System.out.println("Please enter your password");
        String password = sc.next();
 
        System.out.println("Please confirm your password again");
        String password1 = sc.next();
        
        while (!password1.equals(password)) {
            System.out.println("Sorry, the two passwords are inconsistent, please re-enter");
            System.out.println("Please re-enter your password");
            password = sc.next();
            System.out.println("Please confirm your password again");
            password1 = sc.next();
        }
        
        System.out.println("Please input your ID number.");
        String personId = sc.next();
 
        System.out.println("Please enter your email address");
        String email = sc.next();
        
        depositor[accountNum] = new Depositor();
        depositor[accountNum].inputnumber(100000 + j);
        depositor[accountNum].inputname(name);
        depositor[accountNum].inputkey(password);
        depositor[accountNum].inputid(personId);
        depositor[accountNum].inputaddress(email);
        depositor[accountNum].show();  //Display account information
        j++;
        accountNum++;
        return depositor[accountNum];
    }
    
    public Depositor enter() {
    	System.out.println("Please enter your bank card number");
        int id = sc.nextInt();
        System.out.println("Please enter your password");
        String password = sc.next();
        
        if (accountNum == 0) {
            System.out.println("Unregistered account, please register first!");
            this.openAccount();
            this.mainView();
            this.select();
        }  
        
        boolean flag = false;
        for (i = 0; i < accountNum; i++) {
            if (id == depositor[i].shownumber() && password.equals(depositor[i].showkey())) {//Judge whether the Id is consistent with the entered account password
                flag = true;
                break;
            }
        }
        
        if (!flag) {
            System.out.println("Login failed!!!");
        }
        
        if (flag) {
            System.out.println("Login successful!!!!");
            do {
                System.out.println("Please select business");
                System.out.println("******1,deposit*****************");
                System.out.println("******2,withdraw money*****************");
                System.out.println("******3,press any key to exit*************");
                insert = sc.nextInt();
                switch(insert) {
                case 1 :     
                    System.out.println("******Please enter the deposit amount*****************");
                    int money = sc.nextInt();
                    depositor[i].inputbalance(money);                                
                    break;
 
                case 2:      
                	System.out.println("******Please enter the withdrawal amount*****************");
                	money = sc.nextInt();
                	depositor[i].deposit(money);//Call withdrawal method
                	break;
 
                }
            } while(insert == 1 || insert == 2 );
        }    
        return depositor[i];
    }
}

3. Test class

package BankSystem;
import java.util.Scanner;
public class Test {
	 public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 Bank b = new Bank();
		 while (true) {
	            b.mainView();//Call interface function
	            int select = sc.nextInt();
	            switch(select) {
	            case 1: b.openAccount();//Create account
	                    break;
	            case 2: b.enter();//Login
	                    break; 
	            case 3: b.manage();
	            		break;
	            default: System.out.println("Select business exception, please re select");
	            break;
	            }
	            
	            System.out.println("Continue to select other businesses");
	            System.out.println("To exit, press 0");
	            System.out.println("To continue selecting other businesses, press 1");
	            
	            select = sc.nextInt();
	            if (select == 0) {
	                break;
	            }
	        }
	 }

}

4, Test results

1) Main menu window

2) Add information

3) Deposit and withdrawal

4) Search by name

5) Delete

5, Summary

The program does not realize the function of modifying user information and the processing operation of file stream. Friends in need can improve themselves. On the whole, the code is cumbersome, some functions are not encapsulated in place, and the code is repeated more. In addition, many details are not handled in place, such as exception handling.

Topics: Java Back-end