Project requirements:
Object-oriented design and analysis:
There are three corresponding 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 bank to deal with business people more conducive to understanding the system, for example, I often accompany my wife run bank, the bank of this business is more familiar with, I know that every 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 three number manager object, each manage a type of user's queue number. These three number manager objects are uniformly managed by a number machine, which can always have one in the entire system, so it is designed to be a single example.
Various types of customers in their corresponding window in order to transact business, to be precise, should be the window in turn called.
How does each 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.
Import java.util.ArrayList;
Import java.util.List;
/**
* For different users, there is a special number manager
* Store customer queuing number, and all waiting for service customer number collection
* @author ETHAN
*
* * * * Public
class Numbermanager {
//last returned number
private int lastnumber = 1;
Private list<integer> Queuenumber = new arraylist<integer> ();
Sync public
synchronized Integer Generatenewnumber () {
queuenumber.add (lastnumber);
return lastnumber++;
}
Prevent null, NULL pointer exception, unboxing----int public
synchronized Integer Fetchservicenumber () {
if (queuenumber.size () >0) {
return queuenumber.remove (0);//returns the first
} else {returned
null
;
}
}}
/**
* A machine, single case mode
* for different users, there is a special number manager
* @author ETHAN * * */Public
class Numbermachine {
// Three Type number manager
private Numbermanager Commonmanager = new Numbermanager ();
Private Numbermanager Expressmanager = new Numbermanager ();
Private Numbermanager Vipmanager = new Numbermanager ();
private static Numbermachine Numbermachine = new Numbermachine ();
Private Numbermachine () {} public
static Numbermachine getinstance () {return
numbermachine;
}
Public Numbermanager Getcommonmanager () {return
commonmanager;
}
public void Setcommonmanager (Numbermanager commonmanager) {
This.commonmanager = Commonmanager;
}
Public Numbermanager Getexpressmanager () {return
expressmanager;
}
public void Setexpressmanager (Numbermanager expressmanager) {
This.expressmanager = Expressmanager;
}
Public Numbermanager Getvipmanager () {return
vipmanager;
}
public void Setvipmanager (Numbermanager vipmanager) {
Vipmanager = Vipmanager;
}
}
public enum CustomerType {
common,express,vip;
Public String toString () {
switch (this) {case
COMMON: Return
"normal";
Case EXPRESS: Return
"Fast";
Case VIP: Return
name ();
}
return null;
}
}
public class Constants {
//10 seconds, service time public
static final int max_service_time = 10000;
1 sec public
static final int min_service_time = 1000;
Normal user interval 1 seconds public
static final int common_customer_interval_time = 1;
}
service Window business logic:
Import Java.util.Random;
Import java.util.concurrent.Executors; public class Servicewindow {//Window type private CustomerType type = customertype.common;//normal window max//window number private int window
Id = 1; window, a thread starts public void start () {Executors.newsinglethreadexecutor (). Execute (new Runnable () {@Override Publ
IC void Run () {switch (type) {case Common:while (true) {Commonservice ();
Case Express:while (True) {Expressservice ();
Case Vip:while (True) {Vipservice ();
}
}
}
});
private void Commonservice () {String windowname = ' +windowid+ ' +type+ window ';
Integer number = Numbermachine.getinstance (). Getcommonmanager (). Fetchservicenumber ();
System.out.println (windowname+ "Getting the task"); if (number!=null) {//Bottom is the normal User Service, Commonservice (), type or EXPRESS,VIP System.out.println (windowname+ "is in the first" +number+
"A" + "ordinary" + "customer Service");
Long begintime = System.currenttimemillis (); 9000 int MaXrand = Constants.max_service_time-constants.min_service_time; [0,9000) ===[0,8999]---------->[1,9000] plus 1-----[1,10000]//formula: [Min,max] = = = New Random (). Nextint (max-min+1) +mi
N
Long servertime = new Random (). Nextint (Maxrand) +1+constants.min_service_time;
try {thread.sleep (servertime);
catch (Interruptedexception e) {e.printstacktrace ();
Long costtime = System.currenttimemillis ()-begintime;
System.out.println (windowname+ "for the first" +number+ "+type+" Customer completion service, time consuming "+costtime/1000+"); else {System.out.println (windowname+) did not get the service task, no users.
Let's take a second. "
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace ();
}} private void Expressservice () {String windowname = "+windowid+", "+type+ window";
Integer number = Numbermachine.getinstance (). Getexpressmanager (). Fetchservicenumber ();
System.out.println (windowname+ "Getting the task"); if (number!=null) {Long begintime = System.currenttimemillis ();
try {//Fast user, processing time minimum value thread.sleep (constants.min_service_time);
catch (Interruptedexception e) {e.printstacktrace ();
Long costtime = System.currenttimemillis ()-begintime;
System.out.println (windowname+ "for the first" +number+ "+type+" Customer completion service, time consuming "+costtime/1000+"); else {System.out.println (windowname+) did not get the service task, no users.
To serve a common user ");
Commonservice ();
} private void Vipservice () {String windowname = ' +windowid+ ' +type+ window ';
Integer number = Numbermachine.getinstance (). Getvipmanager (). Fetchservicenumber ();
System.out.println (windowname+ "Getting the task");
if (number!=null) {Long begintime = System.currenttimemillis ();
9000 int maxrand = Constants.max_service_time-constants.min_service_time; [0,9000) ===[0,8999]---------->[1,9000] plus 1-----[1,10000]//formula: [Min,max] = = = New Random (). Nextint (max-min+1) +mi
N
Long servertime = new Random (). Nextint (Maxrand) +1+constants.min_service_time; try {thread.sleep (sErvertime);
catch (Interruptedexception e) {e.printstacktrace ();
Long costtime = System.currenttimemillis ()-begintime;
System.out.println (windowname+ "for the first" +number+ "+type+" Customer completion service, time consuming "+costtime/1000+"); else {System.out.println (windowname+) did not get the service task, no users.
To serve a common user ");
Commonservice ();
} public CustomerType GetType () {return type;
The public void SetType (CustomerType type) {this.type = type;
public int Getwindowid () {return windowid;
The public void Setwindowid (int windowid) {this.windowid = Windowid;
}
}
Test class:
Import java.util.concurrent.Executors;
Import Java.util.concurrent.TimeUnit;
public class MainClass {/** * @param args */public static void main (string[] args) {//default to common//generate four normal windows
for (int i=1;i<5;i++) {Servicewindow Commonwindow = new Servicewindow ();
Commonwindow.setwindowid (i);
Commonwindow.start ();
}//A quick window Servicewindow Expresswindow = new Servicewindow ();
Expresswindow.settype (customertype.express);
Expresswindow.start ();
A VIP window Servicewindow Vipwindow = new Servicewindow ();
Vipwindow.settype (CUSTOMERTYPE.VIP);
Vipwindow.start (); Three timers, timed to produce three kinds of users//analog user arrival, scheduling thread pool, timing//user ratio: 1:6:3, Interval time 1:2:6 executors.newscheduledthreadpool (1). scheduleatfixedrate (New Runnable () {@Override public void run () {Integer number = numbermachine.getinstance (). GE
Tcommonmanager (). Generatenewnumber (); System.out.println (number+ "") ordinary users waiting for service.
"); }, 0, Constants.common_customer_interval_Time, Timeunit.seconds); Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {@Override public void run ()
{Integer number = Numbermachine.getinstance (). Getexpressmanager (). Generatenewnumber (); System.out.println (number+ "") ordinary users waiting for service.
");
}, 0, constants.common_customer_interval_time*2, timeunit.seconds); Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {@Override public void run ()
{Integer number = Numbermachine.getinstance (). Getvipmanager (). Generatenewnumber (); System.out.println (number+ "") ordinary users waiting for service.
");
}, 0, constants.common_customer_interval_time*6, timeunit.seconds);
}
}