Implementation of bank management system with java code

Time:2021-2-22

This example for you to share the Java bank management system specific code, for your reference, the specific content is as follows

Bank business scheduling

1、 System requirements

1. There are six business windows in the bank, No.1-4 are ordinary windows, No.5 are fast windows, and No.6 are VIP windows.

2. There are three corresponding types of customers: VIP customers, ordinary customers and fast customers (customers who handle such businesses as paying water and electricity charges and telephone charges).

3. Asynchronous random generation of various types of customers
VIP customers: ordinary customers: fast customers = 1:6:3.

4. There are maximum and minimum time required for customers to handle business. Within this range, the time required for each VIP customer and ordinary customer to handle business is set randomly, and the time required for fast customers to handle business is the minimum (hint: the process of handling business can be simulated by thread sleep).

5. All types of customers handle business in order in their corresponding windows.

6. When there are no customers waiting for business in VIP (No.6) window and fast business (No.5) window, these two windows can handle the business of ordinary customers. Once there are corresponding customers waiting for business, priority will be given to the business of corresponding customers.

7. Randomly generate the customer time interval and the maximum and minimum value of business processing time, which can be set.

8. The GUI is not required, only the system logic is considered, and the program running results can be displayed by log.

2、 A brief analysis of the system

1. There are three corresponding types of customers: VIP customers, ordinary customers, fast customers, asynchronous random generation of various types of customers, each type of customers in its corresponding window in order to handle business.

(1) I know that each customer is actually informed to handle the relevant business by the way of a number generated by a number retrieval machine of the bank. Therefore, to define a number manager object and let the object continuously generate numbers is to generate customers randomly.
(2) Because there are three types of customers, and the number arrangement of each type of customer is completely independent, I think that this system will generate three number manager objects to manage the queuing numbers of a type of users. These three number manager objects are managed by a number machine, which can only have one number machine in the whole system. Therefore, it should be designed as a singleton.

2. All types of customers in the corresponding window in order to deal with business, it can be said that each window in turn called.

(1) How do windows know which number to call? It must ask the corresponding number manager, that is, every time the service window looks for the number manager to get the number to be served.

3、 System analysis

1. Numbermanager class analysis:

(1) Define a member variable used to store the last customer number and a queue set used to store all customer numbers waiting for service.
(2) , define a method to generate a new number and a method to obtain the number to be served immediately. These two methods are operated by different threads with the same data, so they need to be synchronized.

2. Numbermachine class analysis:

(1) Three member variables are defined to point to three numbermanager objects, representing the number managers of ordinary, fast and VIP customers respectively. Three corresponding methods are defined to return the three numbermanager objects.
(2) Design the numbermachine class as a singleton.  

3. Customertype enumeration class analysis:

(1) There are three types of customers in the system, so define an enumeration class with three members to represent three types of customers.
(2) , override the toString method to return the Chinese name of the type. This is reconstructed in the later coding, which need not be considered at the beginning.

4. Servicewindow class analysis:

(1) , define a start method, start a thread internally, and call three different methods according to the category of service window.
(2) In order to observe the operation effect, the detailed information should be printed out in detail.

5. Mainclass class analysis:

(1) , create four common windows with for loop, then create one quick window and one VIP window.
(2) And then create three timers to create new ordinary customer number, new fast customer number and new VIP customer number.

6. Constant class analysis:

Define three constants: max_ SERVICE_ TIME、MIN_ SERVICE_ TIME、COMMON_ CUSTOMER_ INTERVAL_ TIME

7. Program diagram:

4、 System details

1、 NumberManager.java File creation:

import java.util.ArrayList;
import java.util.List;
 
public class NumberManager {
 private int lastNumber = 0;
 Private list queuenumbers = new arraylist(); // define ArrayList set
 
 /*
 *1. Define a member variable used to store the last customer number and a queue set used to store all customer numbers waiting for service.
 */
 Public synchronized integer generatenewnumber() {//
 queueNumbers.add (+ + lastnumber); // add to collection
 Return lastnumber; // returns the last value
 }
 
 /*
 *2. Define a method to generate a new number and a method to obtain the number to be served immediately. These two methods are operated by different threads with the same data, so they need to be synchronized.
 */
 public synchronized Integer fetchNumber() {
 if ( queueNumbers.size () > 0) {// if there are elements in the set queuenumbers
 return (Integer)  queueNumbers.remove (0); // returns the integer type of the first element removed
 } else {
 Return null; // return null
 }
 }
}

2、 NumberMachine.java File creation:

Public class numbermachine {// singleton design pattern
 private NumberMachine() {
 }//Privatization approach
 
 private static NumberMachine instance = new NumberMachine();
 
 public static NumberMachine getInstance() {
 Return instance; // example instance
 }
 
 /*
 *Three member variables are defined to point to three numbermanager objects, representing the number managers of ordinary, fast and VIP customers respectively. Three corresponding methods are defined to return the three numbermanager objects
 * 。
 */
 private NumberManager commonManager = new NumberManager();
 private NumberManager expressManager = new NumberManager();
 private NumberManager vipManager = new NumberManager();
 
 public NumberManager getCommonManager() {
 Return commonmanager; // as below, return
 }
 
 public NumberManager getExpressManager() {
 return expressManager;
 }
 
 public NumberManager getVipManager() {
 return vipManager;
 }
 
}

3、 CustomerType.java File creation:

Public enum customertype {// enumerating class customertype
 COMMON, EXPRESS, VIP;
 public String toString() {
 String name = null;
 switch (this) {
 case COMMON:
 Name = "normal"; // if the word "common" appears, name is the normal user
 break;
 case EXPRESS:
 Name = "fast"; // if express appears, it is a fast user
 break;
 case VIP:
 Name = name(); // if VIP appears, it is a VIP
 break;
 }
 return name;
 }
}

4、 ServiceWindow.java File creation:

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
/**
 *There is no subclass of VIP window and fast window, because the ordinary window in the actual business can be set as VIP window and fast window at any time.
 * */
public class ServiceWindow {
 private static Logger logger =  Logger.getLogger (" cn.itcast.bankqueue "); // creating a system log
 private CustomerType type =  CustomerType.COMMON ; // the default value of user type calling "enumeration class customertype" is "normal user"
 private int number = 1;
 
 public CustomerType getType() {
 return type;
 }
 
 Public void setType (customertype) {// set has parameters
 this.type = type;
 }
 
 public void setNumber(int number){
 this.number = number;
 }
 
 The public void start() {// start method is used to
 Executors.newSingleThreadExecutor().execute(
 new Runnable(){
  Public void run() {// duplicate run method
  //The operation efficiency of the following writing method is low. It is better to put while under case
  while(true){
  switch(type){
  Case common: // ordinary users
   Commonservice(); // execute normal user methods
   break;
  Case express: // fast user
   Expressservice(); // execute fast user methods
   break;
  case  VIP://vip user
   Vipservice(); // execute the VIP user method
   break;
  }
  }
  }
 }
 );
 }
 Private void commonservice() {// normal user method
 String windowsname = "+ number +" number "+ type +" window "; 
 System.out.println (windowsname + "start serving ordinary users...);
 Integer serviceNumber = NumberMachine.getInstance().getCommonManager().fetchNumber(); 
 If (servicenumber! = null) {// if
 System.out.println (windowname + "start to serve" + servicenumber + "ordinary customer"); 
 int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
 int serviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME;
 
 try {
 Thread.sleep(serviceTime);
 } catch (InterruptedException e) {
 e.printStackTrace();
 } 
 System.out.println (windowname + "is the general customer service of" + servicenumber + ", which takes" + servicetime / 1000 + "seconds in total.); 
 }else{
 System.out.println (windowname + "no normal task, idle for one second"); 
 try {
 Thread.sleep (1000); // thread sleep one second
 } catch (InterruptedException e) {
 e.printStackTrace();
 } 
 }
 }
 
 Private void expressservice() {// fast customer method
 Integer serviceNumber = NumberMachine.getInstance().getExpressManager().fetchNumber();
 String windowsname = "+ number +" number "+ type +" window "; 
 System.out.println (windowsname + "start getting fast tasks!"); 
 if(serviceNumber !=null){
 System.out.println (windowname + "start to serve" + servicenumber + "fast customer"); 
 int serviceTime = Constants.MIN_SERVICE_TIME;
 try {
 Thread.sleep(serviceTime);
 } catch (InterruptedException e) {
 e.printStackTrace();
 } 
 System.out.println (windowname + "is the fast customer service of" + servicenumber + ", which takes" + servicetime / 1000 + "seconds in total.); 
 }else{
 System.out.println (windowsname + "no fast task! "); 
 commonService();
 }
 }
 
 Private void vipservice() {// VIP user method
 Integer serviceNumber = NumberMachine.getInstance().getVipManager().fetchNumber();
 String windowsname = "+ number +" number "+ type +" window "; 
 System.out.println (windowsname + "start getting VIP tasks!"); 
 if(serviceNumber !=null){
 System.out.println (windowsname + "start to serve VIP customer No." + servicenumber + "); 
 int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
 int serviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME;
 try {
 Thread.sleep(serviceTime);
 } catch (InterruptedException e) {
 e.printStackTrace();
 } 
 System.out.println (windowsname + "completes VIP customer service for" + servicenumber + ", total time consuming" + servicetime / 1000 + "seconds"); 
 }else{
 System.out.println (windowsname + "no VIP task! "); 
 commonService();
 } 
 }
}

5、 MainClass.java File creation:

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
 
public class MainClass {
 
 private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
 public static void main(String[] args) {
 //Generate 4 common windows
 for(int i=1;i<5;i++){
 ServiceWindow window = new ServiceWindow();
 window.setNumber(i);
 window.start();
 }
 
 //Generate a quick window
 ServiceWindow expressWindow = new ServiceWindow();
 expressWindow.setType(CustomerType.EXPRESS);
 expressWindow.start();
 
 //Generate a VIP window 
 ServiceWindow vipWindow = new ServiceWindow();
 vipWindow.setType(CustomerType.VIP);
 vipWindow.start(); 
 
 //Ordinary customers get number
 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
 new Runnable(){
  public void run(){
  Integer serviceNumber = NumberMachine.getInstance().getCommonManager().generateNewNumber();
  /**
  *Using the logger mode, we can't see the intuitive running effect, because logger.log The method does not print out the content directly,
  *Instead, it's left to an internal thread to process, so the printed results look chaotic in chronological order.
  */
  // logger.info (ordinary customer No. + servicenumber + is waiting for service! ");
  System.out.println (ordinary customer No. + servicenumber + is waiting for service! ");  
  }
 },
 0,
 Constants.COMMON_CUSTOMER_INTERVAL_TIME, 
 TimeUnit.SECONDS);
 
 //Quick customer number collection
 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
 new Runnable(){
  public void run(){
  Integer serviceNumber = NumberMachine.getInstance().getExpressManager().generateNewNumber();
  System.out.println ("No." + servicenumber + "fast customer is waiting for service! ");
  }
 },
 0,
 Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2, 
 TimeUnit.SECONDS);
 
 //VIP customers get number
 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
 new Runnable(){
  public void run(){
  Integer serviceNumber = NumberMachine.getInstance().getVipManager().generateNewNumber();
  System.out.println (VIP customer No. + servicenumber + "is waiting for service! ");
  }
 },
 0,
 Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, 
 TimeUnit.SECONDS);
 }
 
}

6、 Constants.java File creation:

public class Constants {
 public static int MAX_ SERVICE_ Time = 10000; // 10 seconds!
 public static int MIN_ SERVICE_ Time = 1000; // 1 second!
 
 /*The average time for each ordinary window to serve a customer is 5 seconds. There are four such windows, that is to say, all the ordinary windows of the bank are combined
 *An average of 1.25 seconds can serve an ordinary customer, plus the quick window and VIP window can also serve ordinary customers, so,
 *It's reasonable to generate an ordinary customer in one second*/ 
 public static int COMMON_CUSTOMER_INTERVAL_TIME = 1; 
}

7. Conclusion:

There are many similarities between the banking system and the traffic light management system. For example, “enumeration” and “reflection” are used. If this is a fixed programming mode, you can refer to it well. After all, Mr. Zhang regards these two projects as 7K interview questions, which shows that they are of great value. After these two projects come down, his thinking has been improved to a new level, and he hopes to have a better future I can better understand the essence of “Object-Oriented”!

For more learning materials, please pay attention to the topic management system development.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.