Black Horse programmer-java-Bank Business Dispatch system "11"

Source: Internet
Author: User

-- Java training, Android training, iOS training,. NET training , look forward to communicating with you! --

1. understand the work flow and principle of the Bank business Dispatch system

There are three kinds of windows in the bank, one is the ordinary window, one is the VIP window, one is the Quick window, all the customers are queued, and when the VIP window or Quick window is idle, it can serve the normal window.

2. Project requirements for the banking dispatch system

· There are 6 Business Windowsin the Bank,1-4 window is normal window,window5 is Quick window,window6 is VIP window.

· There are three types of customers:VIP customers, ordinary customers, fast customers (for example, to pay utilities, such as electricity charges, such as business customers).

· Asynchronously randomly generates various types of customers, and the probability ratio for generating types of users is:

VIP Customer: Ordinary customer: Fast customer = 1 :6 :3.

· The time required for the customer to transact the business is the maximum and minimum, in which the time required for each VIP Customer and the ordinary customer to transact the business is randomly set, and the time required for the fast customer to transact the business is the minimum (hint: the process of handling the business can be done by thread Sleep The way it is modeled).

· Various types of customers in their corresponding window in order to transact business sequentially.

· When the VIP(No.6 ) window and the Express business (5 ) window do not have customers waiting for business, these two windows can handle the business of ordinary customers, And once the corresponding customer waits for the business, the priority is to deal with the customer's business.

· Randomly generated customer time interval and business processing time maximum and minimum value custom, can be set.

· Does not require the implementation of the GUI, only consider the implementation of the system logic, Log mode can show the program running results.

3. The object oriented idea realizes the Bank service dispatch system

Service window class (Servicewindow): Encapsulates the "normal service", "express service", "VIP service" three methods, by enumerating the values of CustomerType, and calling these three methods respectively.

Three kinds of service logic: first get "service number" (generated by the number generator), set a certain service time, the service time is completed after the execution.

The implementation is as follows:

package cn.itcast.bankqueue;import java.util.random;import java.util.concurrent.executors; import java.util.logging.logger;/** service window class: *  does not make the VIP window and the Quick window sub-class, because the actual business of ordinary windows can be set at any time as VIP window and Quick window.  * */public class servicewindow {        // Print information Private static logger logger = logger.getlogger ("Cn.itcast.bankqueue");         //customertype enumeration class, distinguish three kinds of services, here default General Service Private customertype type  = CustomerType.COMMON;private int number = 1;public CustomerType  GetType ()  {return type;}         //setting up different services Public void settype (CustomerType type)  {this.type = type;} Public void setnumber (int number) {this.number = number;} Started executing one of the 3 services in Public void start () {executors.newsinglethreadexecutor (). Execute (new  Runnable () {Public void run () {//The following is a less efficient way to do this, preferably by placing the while under case (true) {switch (type) {case  Common:commonservice (); Break;case express:expressservice (); Break;case vip:vipservice (); break;}}}); General Service Private void commonservice () {string windowname =  "section"  + number +   "No."  + type +  "window"; System.out.println (windowname +  "Start getting common tasks!"); Nteger servicenumber = numbermachine.getinstance (). Getcommonmanager (). FetchNumber (); if ( servicenumber != null ) {System.out.println (windowname +  "Start for section"  +  servicenumber +  "General 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 for the"  + serviceNumber +  "general customer service, total time-consuming"  + servicetime/ 1000 +  "seconds");} Else{system.out.println (windowname +  "does not take the normal task, is idle for a second"); Try {thread.sleep (1000);}  catch  (interruptedexception e)  {e.printstacktrace ();}} Fast service Private void expressservice () {integer servicenumber = numbermachine.getinstance (). Getexpressmanager (). Fetchnumber (); string windowname =  ""  + number +  "No."  + type +  "window"; System.out.println (windowname +  "start getting quick quests!"); if (servicenumber !=null) {System.out.println (windowname +  "starts with"  + serviceNumber  +  "Fast customer Service"); Int servicetime = constants.min_service_time;try {thread.sleep ( ServiceTime);}  catch  (interruptedexception e)  {e.printstacktrace ();} System.out.println (windowname +  "completed as first"  + servicenumber + " Fast customer service, total time spent " + serviceTime/1000 +  seconds");} Else{system.out.println (windowname +  "not get to fast task! "); Commonservice ();}} VIP Service Private void vipservice () {integer servicenumber = numbermachine.getinstance (). Getvipmanager (). Fetchnumber (); string windowname =  ""  + number +  "No."  + type +  "window"; System.out.println (windowname +  "Get VIP Mission!"); if (servicenumber !=null) {System.out.println (windowname +  "starts with"  + 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 for the first"  + serviceNumber +  "VIP customer service, total time-consuming"  + servicetime/1000 +  "seconds");} Else{system.out.println (windowname +  "did not take the VIP mission! "); Commonservice ();}}}

Customer Type enumeration class (CustomerType): Differentiate three types of customers by enumerating classes

The implementation is as follows:

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 = "Fast"; break;case Vip:name = name (); break;} return name;}}

Number Generator class (numbermachine): Built-in three number generation manager corresponds to three kinds of services.

The implementation is as follows:

package  cn.itcast.bankqueue;//number generator: Designed as a singleton mode to achieve multi-threaded operation of a data purpose public class numbermachine {private  numbermachine () {}private static numbermachine instance = new numbermachine ();p ublic static numbermachine getinstance () {return instance;} Three number generation manager Private numbermanager commonmanager = new numbermanager ();p rivate  Numbermanager expressmanager = new numbermanager ();p rivate numbermanager  Vipmanager = new numbermanager ();//Get a number Build manager Public numbermanager getcommonmanager ()  {return commonmanager;} Public numbermanager getexpressmanager ()  {return expressmanager;} Public numbermanager getvipmanager ()  {return vipmanager;}} 

  Number Generation Manager class (Numbermanager): Use the synchronization mechanism to achieve number generation and storage.

The implementation is as follows:

Package Cn.itcast.bankqueue;import Java.util.arraylist;import java.util.list;//number generation Manager: Use ArrayList to store generated numbers, And only starting from the first number to obtain, to achieve first come first get number effect public class Numbermanager {private int lastnumber = 0;private List queuenumbers = new ArrayList () ;p ublic synchronized Integer generatenewnumber () {queuenumbers.add (++lastnumber); return lastnumber;} Public synchronized Integer Fetchnumber () {if (Queuenumbers.size () >0) {return (Integer) queuenumbers.remove (0);} Else{return null;}}}

  constant Value class (Constants): Storing information for some constants

The implementation is as follows:

Package Cn.itcast.bankqueue;public class Constants {public static int max_service_time = 10000;//10 seconds! public static int min_service_time = 1000; 1 seconds! /* Each ordinary window service a customer's average time is 5 seconds, a total of 4 such windows, that is, the Bank of all ordinary windows together * Average 1.25 seconds can serve a regular customer, plus Quick window and VIP window can also serve ordinary customers, so, * 1 seconds to produce a common customer reasonable, */public static int common_customer_interval_time = 1; }

  Start the program (main):

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 = logger.getlogger ("Cn.itcast.bankqueue");p ublic  Static void main (String[] args)  {//generates 4 normal windows for (int i=1;i<5;i++) {ServiceWindow  window =  new servicewindow (); Window.setnumber (i); Window.start ();} Generate 1 Quick Windows Servicewindow expresswindow =  new servicewindow (); Expresswindow.settype ( customertype.express); Expresswindow.start ();//Generate 1 VIP Windows Servicewindow vipwindow =  new  servicewindow (); Vipwindow.settype (CUSTOMERTYPE.VIP); Vipwindow.start ();// Ordinary customer take number Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New runnable () {Public void run () { Integer servicenumber = numbermachine.getinstance (). Getcommonmanager (). GenerateNewNumber ();/** *  uses the logger way, unable to see the intuitive running effect, because the Logger.log method is not directly inside the content to print out, *  but to the inside of a thread to deal with, so, The printed results look chaotic in chronological order.  *///logger.info (" + serviceNumber + " number of ordinary customers are waiting for service! "); System.out.println (" + serviceNumber + " number of ordinary customers are waiting for service! ");}},0,constants.common_customer_interval_time, timeunit.seconds);// Fast Customer pick Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New runnable () {Public void run () { Integer servicenumber = numbermachine.getinstance (). Getexpressmanager (). GenerateNewNumber (); System.out.println ("No.  + serviceNumber + " Fast customer is waiting for service!) ");}},0,constants.common_customer_interval_time * 2, timeunit.seconds);// VIP customer number Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New runnable () {Public void run () {integer servicenumber = numbermachine.getinstance (). Getvipmanager (). GenerateNewNumber (); SYSTEM.OUT.PRINTLN ("section"  + servicenumber +  "VIP customers are waiting for service! ");}},0,constants.common_customer_interval_time * 6, timeunit.seconds);}}

Summary :

1. First understand the needs of the project

2. Understand the business logic of each requirement module (important)

3. Consider object-oriented thinking: What classes are involved, and the relationships between classes and classes.

4. Start implementing the function, and later refactor and optimize the code.


Black Horse programmer-java-Bank Business Dispatch system "11"

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.