----------------------
Android training and Java training. We look forward to communicating with you!
----------------------
The Banking Business scheduling system has the following questions:
1. There are 6 business windows, 1-4 are normal windows, 5 are quick windows, and 6 are VIP windows, which correspond to three types of customers respectively:
Ordinary customers, fast customers, VIP customers
2. randomly generate various types of customers,
The probability ratio of each customer type is normal: fast: VIP = 6: 3: 1
3. the maximum and minimum service time for the customer to process the business. The service time required by the VIP and common customers is set randomly within the permitted range, and the quick customer is set to the minimum value.
4. The specific service logic is the same as that in real life,
For example, when VIP and quick window do not have customers, they can serve normal customers.
Object-Oriented Analysis and Design
There are three types of customers: VIP customers, General customers, and fast customers. Various types of customers are randomly generated asynchronously, and each type of customers process their businesses in sequence in their corresponding windows.
Each customer is represented by a number generator of a bank generating a number. Then, we need to have a number manager object so that this object continuously generates numbers, which means that the customer is randomly generated.
Because there are three types of customers, and each type of customer's number orchestration is completely independent, this system generates three number manager objects, each of which manages the queuing numbers of a class of users. However, a bank's number management system can only have one, so it needs to be designed as a singleton mode.
Various customers handle their businesses in sequence in their respective windows and convert them into object-oriented design. We can think that each window is called in sequence. That is, the number to be served is obtained through the number manager each time.
Number receiving machine →
Get normal number (); get VIP number (); get quick number (); get object ();
Renewal Service Window
Start ();
Ordinary customers (); fast customers (); VIP customers ();
Bytes
Number manager →
Customer appearance frequency (); Number acquisition ();
Implementation Code:
First, you need to implement the number MANAGER: numbermanager, which is used to generate numbers and obtain numbers. Because the two numbers must be operated by different threads and the customer who obtains the numbers must perform services first, the two threads must be synchronized.
Numbermanager. Java
Public class numbermanager {
Private int lastnumber = 1;
Private list <integer> queuenumber = new arraylist <integer> (); // queue number
Public synchronized integer generagenewmanager (){//
Generate a number
Queuenumber. Add (lastnumber );
Return lastnumber ++;
}
Public synchronized integer fetchservicenumber () {// get the number
Return queuenumber. Remove (0 );
}}
// -------------------------------- Machine number
Numbermechine. Java
Public class numbermachine {// Number Machine
// Return three managers
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;
}
Change to singleton
Private numbermachine () {}// first, the constructor is private.
Public static numbermachine getinstance () {// get the instance
Return instance;
}
Private Static numbermachine instance = new numbermachine (); // then instantiate
}
Import java. util. Random;
Import java. util. Concurrent. executors;
Public void start () {// start the call, using the thread pool
Executors.newsinglethreadscheduledexecutor(cmd.exe cute (New runnable () {// find Idle threads in the thread pool
Public void run (){
While (true) {// keeps getting the number
Switch (type) {// switch can also recognize enumeration types
Case common: // a common user obtains a common number.
Commonservice ();
Break;
Case Express: // quick start to get a normal number
Expressservice ();
Break;
Case VIP: // The VIP user obtains the normal number.
Vipservice ();
Break;
}
}
}
});
}
//----------------------------------------------------------
// Customertype. Java User Type
Public Enum customertype {// three customer types, common, fast, VIP
Common, express, VIP;
// Rewrite the tostring Method for ease of understanding
Public String tostring (){
Switch (this) {// current object
Case common:
Return "normal ";
Case Express:
Return "quick ";
Case VIP:
Return name ();
}
Return NULL;
}}
// Servicewindow. Java service window
1. Define a start method, start a thread internally, and call three different methods cyclically according to the service window type.
2. Define three methods to serve three customers respectively. In order to observe the running effect, details should be printed out in detail.
Common service
Private void commonservice (){
String windowname = "no." + windowid + "+ Type +" window ";
System. Out. println (windowname + "acquiring task ");
Integer Number = numbermachine. getinstance (). getcommonmanager (). fetchnumber ();
If (number! = NULL ){
System. Out. println (windowname + "indicates" + number + "+" common "+" Customer Service ");
Long begintime = system. currenttimemillis ();
// Maximum random Value
Int maxrand = constants. MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME; // 9000
Long servetime = new random (). nextint (maxrand) + 1 + constants. min_service_time; // 1001
-- 10000 milliseconds
Try {
Thread. Sleep (servetime );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
Long costtime = system. currenttimemillis ()-begintime; // time spent
System. Out. println (windowname + "indicates the" + number + "" + "common" + "customer to complete the service, which consumes
"+ Costtime/1000 +" seconds ");
System. Out. println ();
} Else {
System. Out. println (windowname + "failed to get the task, first rest for 1 second ");
Try {
Thread. Sleep (1000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
// Quick service
Private void expressservice (){
String windowname = "no." + windowid + "+ Type +" window ";
System. Out. println (windowname + "acquiring task ");
Integer Number = numbermachine. getinstance (). getexpressmanager (). fetchnumber ();
If (number! = NULL ){
System. Out. println (windowname + "indicates" + number + "+ Type +" Customer Service ");
Long begintime = system. currenttimemillis ();
Long servetime = constants. min_service_time; // It can only be the minimum value
Try {
Thread. Sleep (servetime );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
Long costtime = system. currenttimemillis ()-begintime; // time spent
System. Out. println (windowname + "is the" + number + "" + Type + "customer to complete the service, which consumes
"+ Costtime/1000 +" seconds ");
System. Out. println ();
} Else {
System. Out. println (windowname + "no task retrieved ");
Commonservice ();
}
}
// VIP service
Private void vipservice (){
String windowname = "no." + windowid + "+ Type +" window ";
System. Out. println (windowname + "acquiring task ");
Integer Number = numbermachine. getinstance (). getvipmanager (). fetchnumber ();
If (number! = NULL ){
System. Out. println (windowname + "indicates" + number + "+ Type +" Customer Service ");
Long begintime = system. currenttimemillis ();
// Maximum random Value
Int maxrand = constants. MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME; // 9000
Long servetime = new random (). nextint (maxrand) + 1 + constants. min_service_time; // 1001
-- 10000 milliseconds
Try {
Thread. Sleep (servetime );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
Long costtime = system. currenttimemillis ()-begintime; // time spent
System. Out. println (windowname + "is the" + number + "" + Type + "customer to complete the service, which consumes
"+ Costtime/1000 +" seconds ");
System. Out. println ();
} Else {
System. Out. println (windowname + "no task retrieved ");
Commonservice ();
}
}
----------------------
Android training and Java training. We look forward to communicating with you!
----------------------
---------------------- For details, please refer to: http://edu.csdn.net/heima ----------------------