----------- Android training, Java training, and hope to communicate with you! ------------
Banking Business Scheduling System
Simulate and implement the logic of the banking business scheduling system. The specific requirements are as follows:
1. There are 6 business windows in the bank, 1-4 are normal windows, 5 are quick windows, and 6 are VIP windows.
2. There are three types of customers: VIP customers, ordinary customers, and fast customers (customers who handle services such as water, electricity, and telephone fees ).
3,Users of various types are randomly generated asynchronously. The probability proportions of users of various types are as follows:
VIP customers: common customers: Fast customers=1: 6: 3.
4. the maximum and minimum time required for the customer to process the business. In this range, the time required for each VIP customer and ordinary customer to process the business is set at random. The minimum time required for the quick customer to process the business is
(Tip: You can simulate the process of processing the business through thread sleep ).
5. Each type of customer handles the business in sequence in its corresponding window.
6. When the VIP (6) window and quick business (5) window do not have customers waiting to handle the business, these two windows can process the business of ordinary customers, once a corresponding customer is waiting for business processing,
The customer's business is prioritized.
7. You can set the random generation of customer intervals and the maximum and minimum values of business processing time.
8. You do not need to implement the GUI. You only need to implement the system logic. You can use the LOG method to display the program running results.
Object-Oriented Analysis:
CATEGORY search:
1. Number receiving machine: The first thing a bank customer goes to a bank is to get the number and queue.
2. Number MANAGER: used to inform the customer who can handle the business.
3. service window: three types of customer services are available.
4. Customer: three types, with few arrays and fixed values. You can select Enumeration type.
5. constants: fixed numbers of data are found during programming, which are frequently used and can be expressed in the form of constants.
6. Main class, used to start the service window and simulate the customer to get the number.
Conclusion: When writing code, a class may be able to write only a part of the content at the beginning. With the increasing number of system classes, the actual functions of each class become clearer.
Number MANAGER: implementation code
Import Java. util. *; public class numbermanager {private int lastnumber; // The private list queuenumbers = new arraylist () container for loading "business number" data; // generate a business number, to synchronize public synchronized int generatenumber () {queuenumbers. add (++ lastnumber); Return lastnumber;} // get the number. to synchronize the public synchronized int getnumber () {If (queuenumbers. size ()> 0) {return (integer) queuenumbers. remove (0) ;}else {return-1 ;}}}
Number receiving machine: the number receiving system
Public class numbermachine {// Singleton design, the number acquisition system only has one. Private Static numbermachine instance = new numbermachine (); Private numbermachine () {}; public static numbermachine getinstance () {return instance ;} // There are three different number managers in the system: Private numbermanager commonmanager = new numbermanager (); Private numbermanager expressmanager = new numbermanager (); Private numbermanager vipmanager = new numbermanager (); // method for obtaining the manager public numbermanager getcommonmanager () {return commonmanager;} public numbermanager getexpressmanager () {return expressmanager;} public numbermanager getvipmanager () {return vipmanager ;}}
Service window:
Import Java. util. random; import Java. util. concurrent. executors; public class servicewindow {// The default window type is normal customer window private mermertype type = customertype. common; // window No. Private int windowid = 1; // set window type public void settype (customertype type) {This. type = type;} // set the window number public void setwindowid (INT windowid) {This. windowid = windowid;} // window to start the external service public void start () {system. out. println (Type + ":" + windowid +" Starting to serve "); // create executors.newsinglethreadexecutor(cmd.exe cute (New runnable () {public void run () {While (true) {// continuously provide service switch (type) {Case common: commonservice (); // service normal customer break; Case Express: expressservice (); // service Quick customer break; Case VIP: vipservice (); // service VIP Customer break ;}}}) ;}// common Customer Service Public void commonservice () {// window name string windowname = "no." + windowid + "no." + Type + "window"; // calls a common customer whose customer number is number. Int number = nu Mbermachine. getinstance (). getcommonmanager (). getnumber (); system. Out. println (windowname + "acquiring task"); // if a customer has an IF (number! =-1) {system. out. println (windowname + "for" + number + "normal customer service"); int maxrand = constants. max_service_time; // the service time of a common customer ranges from 1 second to 10 seconds. Int servetime = new random (). nextint (maxrand) + constants. min_service_time; try {thread. sleep (servetime); // this time period is in the service} catch (interruptedexception e) {e. printstacktrace ();} system. out. println (windowname + "indicates the" + number + "common customer to complete the service, time consumed" + servetime + "seconds");} // if there is no else {system. out. println (windowname + "no service task obtained, first rest for 1 second"); try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();}}}/ /Quick customer service public void expressservice () {// window name string windowname = "no." + windowid + "no." + Type + "window "; // call the Quick customer int number = numbermachine with the customer number. getinstance (). getexpressmanager (). getnumber (); system. out. println (windowname + "retrieving task"); // if a customer has an IF (number! =-1) {system. out. println (windowname + "" + number + ""); // Service 1 second int servetime = constants. min_service_time; try {thread. sleep (servetime); // this time period is in the service} catch (interruptedexception e) {e. printstacktrace ();} // the actual time consumed by the system. out. println (windowname + "is the" + number + "Number +" Quick customer to complete the service, time consumed "+ servetime +" seconds ");} // if no customer else {system. out. println (windowname + "no service task retrieved"); // serves the common customer commonservice () ;}// VIP Customer Service Public Void vipservice () {// window name string windowname = "" + windowid + "" + Type + "window "; // call the VIP Customer int number = numbermachine with the customer number. getinstance (). getvipmanager (). getnumber (); system. out. println (windowname + "retrieving task"); // if a customer has an IF (number! =-1) {system. out. println (windowname + "indicates" + number + "VIP Customer Service"); int maxrand = constants. max_service_time; // the service time of a common customer ranges from 1 second to 10 seconds. Int servetime = new random (). nextint (maxrand) + constants. min_service_time; try {thread. sleep (servetime); // this time period is in the service} catch (interruptedexception e) {e. printstacktrace ();} system. out. println (windowname + "indicates the" + number + "VIP customer to complete the service, time consumed" + servetime + "seconds");} // if no customer else {system. out. println (windowname + "no service task retrieved"); // serves the common customer commonservice ();}}}
Enumeration customer types used in the middle:
// Enumerate public Enum customertype {common, express, VIP ;}
Constant:
// Constant public class constants {public static int max_service_time = 10; // The maximum service time is 10 seconds. Public static int min_service_time = 1; // The shortest service time is 1 second. Public static int common_service_interval_time = 1; // a common customer can get one in 1 second .}
Main startup class:
Import Java. util. concurrent. executors; import Java. util. concurrent. timeunit; public class mainclass {public static void main (string [] ARGs) {// generates four common customer windows for (INT I = 1; I <5; I ++) {servicewindow commonwindow = new servicewindow (); commonwindow. setwindowid (I); commonwindow. start () ;}// generate a Quick customer window servicewindow expresswindow = new servicewindow (); expresswindow. settype (customertype. express); expresswindow. start (); // Generate a VIP Customer window servicewindow vipwindow = new servicewindow (); vipwindow. settype (customertype. VIP); vipwindow. start (); // executors is required for one common customer every one second. newscheduledthreadpool (1 ). scheduleatfixedrate (New runnable () {public void run () {// number int number = numbermachine. getinstance (). getcommonmanager (). generatenumber (); system. out. println ("------------------------" + number + "regular customer waiting for service") ;}, 0, constants. common_s Ervice_interval_time, timeunit. seconds); // every 2 seconds, the number of one quick client is executors. newscheduledthreadpool (1 ). scheduleatfixedrate (New runnable () {public void run () {// number int number = numbermachine. getinstance (). getexpressmanager (). generatenumber (); system. out. println ("~~~~~~~~~~~~~~~~~~~~~~~~ "+ Number +" no. "Quick customer waiting for service") ;}, 0, constants. common_service_interval_time * 2, timeunit. seconds); // executors is required for one VIP customer every six seconds. newscheduledthreadpool (1 ). scheduleatfixedrate (New runnable () {public void run () {// number int number = numbermachine. getinstance (). getvipmanager (). generatenumber (); system. out. println ("★★★★★★★★★★★★★★★"+ Number +" VIP customer waiting for service ") ;}, 0, constants. common_service_interval_time * 6, timeunit. Seconds );}}
----------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see:Http://edu.csdn.net/heima