Implementation of bank management system with java code

This example shares the specific code of Java bank management system for your reference. The specific contents are as follows

Banking scheduling

1、 System requirements

1. There are 6 business windows in the bank. Windows 1 - 4 are ordinary windows, window 5 is fast window and window 6 is VIP window.

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

3. Various types of customers are generated asynchronously and randomly. The probability ratio of generating various types of users is: VIP customers: ordinary customers: fast customers = 1:6:3.

4. The time required for customers to handle business has maximum and minimum values. Within this range, the time required for each VIP customer and ordinary customers to handle business is randomly set, and the time required for fast customers to handle business is the minimum value (prompt: the process of handling business can be simulated through thread sleep).

5. Each type of customer handles business in order in its corresponding window.

6. When there are no customers waiting for business in VIP (No. 6) window and express service (No. 5) window, these two windows can handle the business of ordinary customers. Once there are corresponding customers waiting for business, the business of corresponding customers will be processed first.

7. The maximum and minimum values of randomly generated customer time interval and business processing time are self-determined and can be set.

8. GUI is not required, only the system logic implementation is considered, and the program operation results can be displayed in log mode.

2、 System analysis

1. There are three corresponding types of customers: VIP customers, ordinary customers and fast customers. Various types of customers are generated asynchronously and randomly. Each type of customer handles business in order in its corresponding window.

(1) I know that each customer is actually notified to handle relevant business by a number retrieval machine of the bank. Therefore, defining a number manager object and allowing this object to continuously generate numbers is equivalent to randomly generating customers. (2) Because there are three types of customers, and the number arrangement of each type of customer is completely independent, I think that the system will generate three number manager objects to manage the queuing numbers of one type of users. These three number manager objects are uniformly managed by a number machine. There can only be one number machine in the whole system, so it needs to be controlled Designed as a single example.

2. Each type of customer handles business in order in the corresponding window, or call the number in order in each window.

(1) How can each window know which number to call? It must ask the corresponding number manager, that is, each time the service window looks for the number manager to obtain 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 operate on the same data by different threads, so they need to be synchronized.

2. Numbermachine class analysis:

(1) Define three member variables to point to three numbermanager objects respectively, which represent the number managers of ordinary, fast and VIP customers. Define three corresponding methods to return the three numbermanager objects. (2) design the numbermachine class as a single example.

3. Customertype enumeration class analysis:

(1) There are three types of customers in the system, so we define an enumeration class, in which three members are defined to represent three types of customers respectively. (2) Rewrite the toString method to return the Chinese name of the type. This is reconstructed in the later coding, so it doesn't need to be considered at the beginning.

4. Servicewindow class analysis:

(1) Define a start method, start a thread internally, and call three different methods circularly according to the category of the service window. (2) define three methods to serve three kinds of customers respectively. In order to observe the operation effect, print out the details in detail.

5. Mainclass analysis:

(1) Use the for loop to create four ordinary windows, and then create one quick window and one VIP window. (2) then create three timers to create new ordinary customer number, new quick customer number and new VIP customer number regularly.

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();// 定义ArrayList集合

 /*
 * 1、定义一个用于存储上一个客户号码的成员变量和用于存储所有等待服务的客户号码的队列集合。
 */
 public synchronized Integer generateNewNumber() {// 上锁
 queueNumbers.add(++lastNumber); // 添加到集合中
 return lastNumber;// 返回最后一个数值
 }

 /*
 * 2、定义一个产生新号码的方法和获取马上要为之服务的号码的方法,这两个方法被不同的线程操作了相同的数据,所以,要进行同步。
 */
 public synchronized Integer fetchNumber() {
 if (queueNumbers.size() > 0) {// 如果集合queueNumbers中有元素
  return (Integer) queueNumbers.remove(0);// 返回移除了第一个元素Integer类型
 } else {
  return null;// 返回空
 }
 }
}

2、NumberMachine. Java file creation:

public class NumberMachine {// 单例设计模式
 private NumberMachine() {
 }// 私有化方法

 private static NumberMachine instance = new NumberMachine();

 public static NumberMachine getInstance() {
 return instance;// 例子instance
 }

 /*
 * 定义三个成员变量分别指向三个NumberManager对象,分别表示普通、快速和VIP客户的号码管理器,定义三个对应的方法来返回这三个NumberManager对象
 * 。
 */
 private NumberManager commonManager = new NumberManager();
 private NumberManager expressManager = new NumberManager();
 private NumberManager vipManager = new NumberManager();

 public NumberManager getCommonManager() {
 return commonManager;// 和下面的一样,返回一下
 }

 public NumberManager getExpressManager() {
 return expressManager;
 }

 public NumberManager getVipManager() {
 return vipManager;
 }

}

3、CustomerType. Java file creation:

public enum CustomerType {// 枚举类CustomerType
 COMMON,EXPRESS,VIP;
 public String toString() {
 String name = null;
 switch (this) {
 case COMMON:
  name = "普通";//如果出现COMMON字样,就是name就是普通用户
  break;
 case EXPRESS:
  name = "快速";//如果出现EXPRESS,就是快速用户
  break;
 case VIP:
  name = name();//如果出现vip,就是贵宾了
  break;
 }
 return name;
 }
}

4、ServiceWindow. Java file creation:

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
/**
 * 没有把VIP窗口和快速窗口做成子类,是因为实际业务中的普通窗口可以随时被设置为VIP窗口和快速窗口。
 * */
public class ServiceWindow {
 private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");//一个系统日志的创建
 private CustomerType type = CustomerType.COMMON;//用户类型调用“枚举类CustomerType”默认值是“普通用户”
 private int number = 1;

 public CustomerType getType() {
 return type;
 }

 public void setType(CustomerType type) {//set有参数
 this.type = type;
 }

 public void setNumber(int number){
 this.number = number;
 }

 public void start(){//start方法用来
 Executors.newSingleThreadExecutor().execute(
  new Runnable(){
   public void run(){//复写run方法
   //下面这种写法的运行效率低,最好是把while放在case下面
   while(true){
    switch(type){
    case COMMON://普通用户
     commonService();//执行普通用户方法
     break;
    case EXPRESS://快速用户
     expressService();//执行快速用户方法
     break;
    case VIP://vip用户
     vipService();//执行vip用户方法
     break;
    }
   }
   }
  }
 );
 }
 private void commonService(){//普通用户方法
 String windowName = "第" + number + "号" + type + "窗口";
 System.out.println(windowName + "开始服务普通用户...");
 Integer serviceNumber = NumberMachine.getInstance().getCommonManager().fetchNumber();
 if(serviceNumber != null ){//如果
  System.out.println(windowName + "开始为第" + 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(windowName + "完成为第" + serviceNumber + "号普通客户服务,总共耗时" + serviceTime/1000 + "秒");
 }else{
  System.out.println(windowName + "没有取到普通任务,正在空闲一秒");
  try {
  Thread.sleep(1000);//线程sleep一秒钟
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
 }
 }

 private void expressService(){//快速客户方法
 Integer serviceNumber = NumberMachine.getInstance().getExpressManager().fetchNumber();
 String windowName = "第" + number + "号" + type + "窗口";
 System.out.println(windowName + "开始获取快速任务!");
 if(serviceNumber !=null){
  System.out.println(windowName + "开始为第" + serviceNumber + "号快速客户服务");
  int serviceTime = Constants.MIN_SERVICE_TIME;
  try {
  Thread.sleep(serviceTime);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  System.out.println(windowName + "完成为第" + serviceNumber + "号快速客户服务,总共耗时" + serviceTime/1000 + "秒");
 }else{
  System.out.println(windowName + "没有取到快速任务!");
  commonService();
 }
 }

 private void vipService(){//vip用户方法
 Integer serviceNumber = NumberMachine.getInstance().getVipManager().fetchNumber();
 String windowName = "第" + number + "号" + type + "窗口";
 System.out.println(windowName + "开始获取VIP任务!");
 if(serviceNumber !=null){
  System.out.println(windowName + "开始为第" + serviceNumber + "号VIP客户服务");
  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 + "完成为第" + serviceNumber + "号VIP客户服务,总共耗时" + serviceTime/1000 + "秒");
 }else{
  System.out.println(windowName + "没有取到VIP任务!");
  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) {
 //产生4个普通窗口
 for(int i=1;i<5;i++){
  ServiceWindow window = new ServiceWindow();
  window.setNumber(i);
  window.start();
 }

 //产生1个快速窗口
 ServiceWindow expressWindow = new ServiceWindow();
 expressWindow.setType(CustomerType.EXPRESS);
 expressWindow.start();

 //产生1个VIP窗口
 ServiceWindow vipWindow = new ServiceWindow();
 vipWindow.setType(CustomerType.VIP);
 vipWindow.start(); 

 //普通客户拿号
 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
  new Runnable(){
   public void run(){
   Integer serviceNumber = NumberMachine.getInstance().getCommonManager().generateNewNumber();
   /**
    * 采用logger方式,无法看到直观的运行效果,因为logger.log方法内部并不是直接把内容打印出出来,
    * 而是交给内部的一个线程去处理,所以,打印出来的结果在时间顺序上看起来很混乱。
    */
   //logger.info("第" + serviceNumber + "号普通客户正在等待服务!");
   System.out.println("第" + serviceNumber + "号普通客户正在等待服务!");
   }
  },Constants.COMMON_CUSTOMER_INTERVAL_TIME,TimeUnit.SECONDS);

 //快速客户拿号
 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
  new Runnable(){
   public void run(){
   Integer serviceNumber = NumberMachine.getInstance().getExpressManager().generateNewNumber();
   System.out.println("第" + serviceNumber + "号快速客户正在等待服务!");
   }
  },Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2,TimeUnit.SECONDS);

 //VIP客户拿号
 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
  new Runnable(){
   public void run(){
   Integer serviceNumber = NumberMachine.getInstance().getVipManager().generateNewNumber();
   System.out.println("第" + serviceNumber + "号VIP客户正在等待服务!");
   }
  },Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6,TimeUnit.SECONDS);
 }

}

6、Constants. Java file creation:

public class Constants {
 public static int MAX_SERVICE_TIME = 10000; //10秒!
 public static int MIN_SERVICE_TIME = 1000; //1秒!

 /*每个普通窗口服务一个客户的平均时间为5秒,一共有4个这样的窗口,也就是说银行的所有普通窗口合起来
 * 平均1.25秒内可以服务完一个普通客户,再加上快速窗口和VIP窗口也可以服务普通客户,所以,
 * 1秒钟产生一个普通客户比较合理,*/
 public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}

7. Summary:

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. After all, Mr. Zhang takes these two projects as 7K interview topics, which shows that they have high value. After these two projects, his thinking has been improved to a new level, I hope I can better understand the essence of "Object-Oriented" in the future!

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>