Bank Business Dispatch system
First, the need to simulate the implementation of banking business scheduling system logic, the specific requirements are as follows:
1. There are 6 business windows in the Bank, 1-4 window is the normal window, window 5th is the Quick window, and the 6th window is the VIP window.
2. There are three types of customers: VIP customers, ordinary customers, fast customers (handling such as electricity and water bills, such as customer service charges).
3. Asynchronous random generation of various types of customers, the probability of generating various types of users is: VIP Customers: Ordinary customers: Fast customer = 1:6: 3.
4. Customers need the maximum and minimum time to transact business, and in this range randomly set each VIP customer and ordinary customers to deal with the business of the time required, rapid customer processing industry
The time required for the service is minimal (hint: the process of handling the business can be simulated by thread sleep mode).
5. Each type of customer in their corresponding window in order to transact business.
6. When the VIP (6th) window and Fast Service (5th) window does not have customers waiting for business, these two windows can handle the business of ordinary customers, and once there is a corresponding
When the customer is waiting for the business, the customer's business is treated as a priority.
7. Randomly generate customer time interval and business processing time maximum and minimum custom, you can set.
8. Do not require the implementation of the GUI, only consider the system logic implementation, log mode can show the results of the program run.
Second, object-oriented analysis and Design
1. There are three types of customers: VIP customers, ordinary customers, fast customers, asynchronous random generation of various types of customers, all types of customers in their corresponding windows in order to transact business.
①. First of all, often in the banking business people more conducive to understanding the system, I know that each customer is actually a bank of the number of a machine to generate the number of the way to express. So, I think to have a number manager object, let this object constantly generate numbers, is equal to randomly generated customers .
②. Because there are three types of customers, each type of customer number arrangement is completely independent, so, I think this system will produce a total of three number manager object, each management of a class of
The number of the user's queue. These three number manager objects are uniformly managed by a number machine, which can always have one in the entire system, so it is
Be designed as a single example.
2. Each type of customer in their corresponding window in order to transact business, exactly, should be the window in turn.
How does every window know which number to call? It must be asked the corresponding number manager, that is, the service window each time to find the number manager to get the number currently to be serviced.
Third, we need to use the class
1, Numbermanager class
Define a member variable to store the last customer number and a collection of queues to store all the customer numbers waiting for the service. Define a method for generating a new number and a way to get the number you want to service immediately, and the two methods are manipulated by different threads to the same data, so synchronize.
2, Numbermachine class
The definition of three member variables points to three Numbermanager objects, representing common, fast, and VIP customer number managers, and defines three corresponding methods to return the three Numbermanager objects. The Numbermachine class is designed as a single example.
3. CustomerType Enumeration Class
There are three types of customers in the system, so define an enumeration class that defines three members representing three types of customers. Overrides the ToString method, which returns the Chinese name of the type. This was reconstructed at the back of the code, and was not considered at first.
4, Servicewindow class
Define a Start method that starts a thread internally and loops through three different methods, depending on the category of the service window.
Define three methods to service three kinds of customers, in order to observe the operation effect, should print out the details of the detailed information.
5, MainClass class
Create 4 normal windows with a for loop, and then create 1 quick windows and a VIP window.
Then create three timers, respectively, to create a new regular customer number, a new quick customer number, a new VIP customer number.
6, Constants Class
Define three constants: Max_service_time, Min_service_time, Common_customer_interval_time
The code is as follows:
Package Com.itcast.interview.bank;
public class Constants {
public static int max_service_time = 10000;
public static int min_service_time = 1000;
public static int common_customer_interval_time = 1;
}
Package Com.isoftstone.interview.bank;
public enum CustomerType {
COMMON,EXPRESS,VIP;
Public String toString () {
String name = NULL;
Switch (this) {
Case COMMON:
name = "normal";
Break
Case EXPRESS:
Name = "Fast";
Break
Case VIP:
Name = name ();
Break
}
return name;
}
}
Package Com.isoftstone.interview.bank;
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 normal Windows
for (int i=1;i<5;i++) {
Servicewindow window = new Servicewindow ();
Window.setnumber (i);
Window.start ();
}
1 Quick windows
Servicewindow Expresswindow = new Servicewindow ();
Expresswindow.settype (customertype.express);
Expresswindow.start ();
1 VIP Windows
Servicewindow Vipwindow = new Servicewindow ();
Vipwindow.settype (CUSTOMERTYPE.VIP);
Vipwindow.start ();
Ordinary customer Get number
Executors.newscheduledthreadpool (1). Scheduleatfixedrate (
New Runnable () {
public void Run () {
Integer Servicenumber = Numbermachine.getinstance (). Getcommonmanager (). Generatenewnumber ();
System.out.println ("No." + Servicenumber +) The ordinary customer is waiting for the service. ");
}
},
0,
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 ("No." + Servicenumber +) Fast customer is waiting for service. ");
}
},
0,
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 ("No." + Servicenumber +) VIP customer is waiting for service. ");
}
},
0,
Constants.common_customer_interval_time * 6,
Timeunit.seconds);
}
}
Package Com.isoftstone.interview.bank;
public class numbermachine {
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;
&NBSP}
public numbermanager getvipmanager () {
return vipmanager;
&NBSP}
private numbermachine () {}
public static NumberMachine GetInstance () {
return instance;
}
private static Numbermachine instance = new numbermachine ();
}
Package Com.isoftstone.interview.bank;
Import java.util.ArrayList;
Import java.util.List;
public class Numbermanager {
private int lastnumber = 1;
Private list<integer> Queuenumber = new arraylist<integer> ();
public synchronized int Generatenewmanager () {
Queuenumber.add (Lastnumber);
return lastnumber++;
}
public synchronized int Fetchservicenumber () {
Return Queuenumber.remove (0);
}
}
Package Com.isoftstone.interview.bank;
Import Java.util.Random;
Import java.util.concurrent.Executors;
Import Java.util.logging.Logger;
public class Servicewindow {
private static Logger Logger = Logger.getlogger ("Cn.itcast.bankqueue");
Private CustomerType type = Customertype.common;
private int number = 1;
Public CustomerType GetType () {
return type;
}
public void SetType (CustomerType type) {
This.type = type;
}
public void Setnumber (int number) {
This.number = number;
}
public void Start () {
Executors.newsinglethreadexecutor (). Execute (
New Runnable () {
public void Run () {
while (true) {
Switch (type) {
Case COMMON:
Commonservice ();
Break
Case EXPRESS:
Expressservice ();
Break
Case VIP:
Vipservice ();
Break
}
}
}
}
);
}
private void Commonservice () {
String windowname = "First" + number + "No." + Type + "window";
System.out.println (Windowname + "start getting common tasks!");
Integer Servicenumber = Numbermachine.getinstance (). Getcommonmanager (). Fetchnumber ();
if (servicenumber!= null) {
System.out.println (Windowname + "Start for the first" + Servicenumber + "General customer Service");
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 + "complete for the first" + Servicenumber + "number of ordinary customer service, total time consuming" + servicetime/1000 + "seconds");
}else{
System.out.println (Windowname + "does not take the ordinary task, is idle one second");
try {
Thread.Sleep (1000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
private void Expressservice () {
Integer Servicenumber = Numbermachine.getinstance (). Getexpressmanager (). Fetchnumber ();
String windowname = "First" + number + "No." + Type + "window";
System.out.println (Windowname + "Start getting quick task!");
if (Servicenumber!=null) {
System.out.println (Windowname + "Start for the first" + Servicenumber + "fast customer service");
int servicetime = Constants.min_service_time;
try {
Thread.Sleep (Servicetime);
catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println (Windowname + "complete for the first" + Servicenumber + "Fast customer service, total time consuming" + servicetime/1000 + "seconds");
}else{
System.out.println (Windowname +) did not get a quick task. ");
Commonservice ();
}
}
private void Vipservice () {
Integer Servicenumber = Numbermachine.getinstance (). Getvipmanager (). Fetchnumber ();
String windowname = "First" + number + "No." + Type + "window";
System.out.println (Windowname + "start to get VIP task!");
if (Servicenumber!=null) {
System.out.println (Windowname + "beginning for the first" + Servicenumber + "VIP customer Service");
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 + "complete for the first" + Servicenumber + "VIP customer service, total time consuming" + servicetime/1000 + "seconds");
}else{
System.out.println (Windowname + ") did not take the VIP task. ");
Commonservice ();
}
}
}