Banking Business Scheduling System

Source: Internet
Author: User

I. Requirements

  1. Simulate and implement the logic of the banking business scheduling system. The specific requirements are as follows:
  2. 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.
  3. There are three types of customers: VIP customers, ordinary customers, and fast customers (customers who handle services such as water, electricity, and telephone fees ).
  4. Asynchronous random generation of various types of customers, the probability ratio of each type of user generation is: VIP customers: ordinary customers: Fast customers = 1: 6: 3.
  5. 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 ).
  6. Each type of customer handles the business in sequence in its corresponding window.
  7. 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.
  8. You can set the time interval of randomly generated customers and the maximum and minimum values of business processing time.
  9. GUI is not required. You only need to implement the system logic. You can use the LOG method to display the program running results.
2. 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 ?"
Iii. Detailed Design of The numbermanager class
  • 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.

Numbermachine class

  • 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.
Customertype enumeration class
  • 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.
Servicewindow class
  • 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.
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.
Constants class
  • Define three constants: max_service_time, min_service_time, and common_customer_interval_time.
Iv. Encoding
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 ;}

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 ();}}}

Import Java. util. arraylist; import Java. util. list; public class numbermanager {private int lastnumber = 0; private list queuenumbers = new arraylist (); Public synchronized integer generatenewnumber () {// generate a new number queuenumbers synchronously. add (++ lastnumber); Return lastnumber;} public synchronized integer fetchnumber () {// synchronously capture the number if (queuenumbers. size ()> 0) {return (integer) queuenumbers. remove (0) ;}else {return NULL ;}}}

Public class numbermachine {// number management class, single-sample design mode 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 ;}}

Public Enum customertype {// customer enumeration class 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 ;}}

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 servicewindow expresswindow = new servi Cewindow (); 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 (). generatenewnumber ();/*** use the logger method, so 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 );}}
V. Summary
  • Singleton mode ensures that this class only generates one instance object in the memory. In the singleton mode design class, the constructor must be private and the class object should be created inside the class, provides external methods to obtain this type of object. In addition, the singleton mode requires multi-threaded access, so pay attention to synchronization.
  • Switch () can accept int byte short char enumeration in jdk5.0. It supports the string type in jdk7.0.
  • Code that is repeatedly used should be extracted and encapsulated into methods for convenient calling. The functions of the methods should be subdivided as much as possible.

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.