Dark Horse programmer: Banking Business Scheduling System

Source: Internet
Author: User

---------- Android training, Java training, and hope to communicate with you!
----------

Business Requirements  

Simulate and implement the logic of the banking business scheduling system. The specific requirements are as follows:

There are 6 business windows in the bank. Window 1-4 is a normal window, window 5 is a quick window, and window 6 is a VIP window.

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

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.

The maximum and minimum time required for the customer to process the business is set. In this range, the time required for each VIP customer and ordinary customer to process the business is set to the minimum time required for the quick customer to process the business (tip: the business process can be simulated by thread sleep ).

Each type of customer handles the business in sequence in its corresponding window.

When the VIP (6) window and quick business (5) window do not have customers waiting for business processing, these two windows can process the business of ordinary customers, once a corresponding customer is waiting for processing the business, the corresponding customer's business is prioritized.

You can set the time interval of randomly generated customers and the maximum and minimum values of business processing time.

GUI is not required. You only need to implement the system logic. You can use the LOG method to display the program running results.

Business analysis 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.First of all, people who often handle business at the bank are more conducive to understanding the system. For example, I often accompany my wife to run the bank, and I am familiar with the banking business, I know that every customer is represented by a number generator of a bank. Therefore, I want to have a number manager object, so that this object will generate a number at random. As there are three types of customers, each type of customer's number orchestration is completely independent, so I think the system will generate a total of three number manager objects, each of which manages the queuing numbers of a class of users. The three phone number manager objects are managed by a single phone number machine. This phone number machine can only have one phone number in the entire system. Therefore, it must be designed as a singleton. Each type of customer handles the business in sequence in the corresponding window. To be precise, the number of the window should be called in sequence.How do I know which number should I call each window? It must be the corresponding number manager, that is, the number manager in the service window gets the number to be served each time. If I have not experienced this kind of banking business for many times, and have accumulated a lot of object-oriented application development experience, I do not know if I can easily implement this design, whether or not we can discover the hidden object information is a feeling of accumulation. Is this a Legend: "Can you just say it, can't you say it ?" Class Diagram

Program source code

Numbermanager

Defines a set of member variables used to store the previous customer number and a queue used to store all customer numbers waiting for service. Define a method to generate a new number and a method to obtain the number to be served immediately. These two methods are operated on the same data by different threads, So synchronization is required.
package cn.itcast.bankqueue;import java.util.ArrayList;import java.util.List;public class NumberManager {private int lastNumber = 0;private List queueNumbers = new ArrayList();public synchronized Integer generateNewNumber(){queueNumbers.add(++lastNumber);return lastNumber;}public synchronized Integer fetchNumber(){if(queueNumbers.size()>0){return (Integer)queueNumbers.remove(0);}else{return null;}}}

Numbermachine

Define three member variables pointing to three numbermanager objects, indicating the number manager of normal, fast, and VIP customers respectively, and define three corresponding methods to return these three numbermanager objects. Design the numbermachine class as a singleton.
package cn.itcast.bankqueue;public class NumberMachine {private NumberMachine(){}private static NumberMachine instance = new NumberMachine();public static NumberMachine getInstance(){return instance;}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;}}

Servicewindow

Define a start method, start a thread internally, and call three different methods cyclically according to the service window type.
Define three methods to serve three customers respectively. In order to observe the running effect, the detailed information should be printed out in detail.
Package CN. itcast. bankqueue; import Java. util. random; import Java. util. concurrent. executors; import Java. util. logging. logger;/*** the VIP window and quick window are not subclass, because normal windows in the actual business can be set as VIP windows and quick windows at any time. **/Public class servicewindow {Private Static 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(cmd.exe cute (New Runnable () {public void run () {// The following method is inefficient. It is best to put while under case while (true) {Switch (type) {Case common: commonservice (); break; Case Express: expressservice (); break; Case VIP: vipservice (); break ;}});} private void commonservice () {string windowname = "no." + number + "+ Type +" window "; system. out. println (windowname + "start to get the common task! "); Integer servicenumber = numbermachine. getinstance (). getcommonmanager (). fetchnumber (); If (servicenumber! = NULL) {system. out. println (windowname + "Start with" + servicenumber + "normal 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 + "completed as normal customer service No." + servicenumber + ", total time consumption" + servicetime /1000 + "seconds");} else {system. out. println (windowname + "no common task retrieved, idle for one second"); try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace () ;}} private void expressservice () {INTEGER servicenumber = numbermachine. getinstance (). getexpressmanager (). fetchnumber (); string windowname = "no." + number + "no." + Type + "window"; system. out. println (windowname + "to get the quick task! "); If (servicenumber! = NULL) {system. out. println (windowname + "Start with" + servicenumber + "Quick customer service"); int servicetime = constants. min_service_time; try {thread. sleep (servicetime);} catch (interruptedexception e) {e. printstacktrace ();} system. out. println (windowname + "completed as" + servicenumber + ", fast customer service, total time consumed" + servicetime/1000 + "seconds");} else {system. out. println (windowname + "no quick task found! "); Commonservice () ;}} private void vipservice () {INTEGER servicenumber = numbermachine. getinstance (). getvipmanager (). fetchnumber (); string windowname = "no." + number + "no." + Type + "window"; system. out. println (windowname + "start to get the VIP task! "); If (servicenumber! = NULL) {system. out. println (windowname + "starting from" + 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 + "completed as VIP Customer Service No." + servicenumber + ", total time consumption" + servicetime /1000 + "seconds");} else {system. Out. println (windowname + "didn't get the VIP task! "); Commonservice ();}}}

Customertype

There are three types of customers in the system. Therefore, an enumeration class is defined. Three members are defined to represent three types of customers. Override the tostring method to return the Chinese name of the type. This is reconstructed in later encoding. You don't need to consider it at the beginning.
Package CN. itcast. bankqueue; Public Enum customertype {common, express, VIP; Public String tostring () {string name = NULL; Switch (this) {Case common: Name = "normal"; break; case Express: name = "quick"; break; Case VIP: Name = Name (); break;} return name ;}}

Constants

Define three constants: max_service_time, min_service_time, and common_customer_interval_time.
Package CN. itcast. bankqueue; public class constants {public static int max_service_time = 10000; // 10 seconds! Public static int min_service_time = 1000; // 1 second! /* Each common window serves a customer with an average time of 5 seconds. There are 4 such windows in total, that is to say, all common windows of a bank can be combined * to serve a common customer in an average of 1.25 seconds, and the quick window and VIP window can also serve ordinary customers, * It is reasonable to generate a common customer in one second. */public static int common_customer_interval_time = 1 ;}

Mainclass

Use the for loop to create four normal windows, and then create one quick window and one VIP window. Then, create three timers to regularly create new common customer numbers, new fast customer numbers, and new VIP Customer numbers.
Package CN. itcast. bankqueue; import Java. util. concurrent. executors; import Java. util. concurrent. timeunit; import Java. util. logging. logger; public class mainclass {Private Static logger = logger. getlogger ("CN. itcast. bankqueue "); public static void main (string [] ARGs) {// generates four common windows for (INT I = 1; I <5; I ++) {servicewindow window = new servicewindow (); window. setnumber (I); window. start ();} // generate a quick window servicewind Ow expresswindow = new servicewindow (); expresswindow. settype (customertype. express); expresswindow. start (); // generate one VIP window servicewindow vipwindow = new servicewindow (); vipwindow. settype (customertype. VIP); vipwindow. start (); // executors for common customers. newscheduledthreadpool (1 ). scheduleatfixedrate (New runnable () {public void run () {INTEGER servicenumber = numbermachine. getinstance (). getcommonmanager (). generatenewnumbe R ();/*** using the logger method, you cannot see the intuitive running effect, because logger. the log method does not directly print the content out, * It is handed to an internal thread for processing. Therefore, the printed results seem messy in chronological order. * /// Logger.info ("no." + servicenumber + ") regular customers are waiting for service! "); System. Out. println (" no. "+ servicenumber +" normal customers are waiting for service! ") ;}}, 0, constants. common_customer_interval_time, timeunit. seconds); // The Quick client obtains the number executors. newscheduledthreadpool (1 ). scheduleatfixedrate (New runnable () {public void run () {INTEGER servicenumber = numbermachine. getinstance (). getexpressmanager (). generatenewnumber (); system. out. println ("no." + servicenumber + ", the customer is waiting for service! ") ;}}, 0, constants. common_customer_interval_time * 2, timeunit. seconds); // executors for VIP customers. newscheduledthreadpool (1 ). scheduleatfixedrate (New runnable () {public void run () {INTEGER servicenumber = numbermachine. getinstance (). getvipmanager (). generatenewnumber (); system. out. println ("no." + servicenumber + "VIP customers are waiting for service! ") ;}}, 0, constants. common_customer_interval_time * 6, timeunit. Seconds );}}

 

---------- Android training, Java training, and hope to communicate with you!
----------

See http://edu.csdn.net/heima/ for details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.