Java-Bank Business Dispatch system "11"

Source: Internet
Author: User

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 in the normal window can be set to the VIP window and Quick window at any time. * */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 Cus Tomertype GetType () {return type;} Set different service public void SetType (CustomerType type) {this.type = type;} public void Setnumber (int number) {this.number = number;} Started execution of a public void start () {Executors.newsinglethreadexecutor () in 3 services. Execute (new Runnable () {public void run () {// The following writing is inefficient, preferably by placing the while in case (true) {switch (type) {case common:commonservice (); Break;case EXPRESS: Expressservice (); break;case Vip:vipservice (); break;}}}); Normal service private void Commonservice () {String windowname = "First" + number + "sign" + 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 the first" + Servicenumber + "number of ordinary customer service"); int maxrandom = Constants.max_service_time-constant S.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 + "number of ordinary customer service, total time-consuming" + servicetime/1000 + "seconds");} Else{system.out.println (Windowname + "not taken to normal task, idle for one second"), try {thread.sleep (+);} catch (Interruptedexception e) { E.printstacktrace ();}}} Fast service private void Expressservice () {Integer servicenumber = Numbermachine.getinstance (). Getexpressmanager (). Fetchnumber (); String windowname = "First" + number + "sign" + type + "window"; System.out.println (Windowname + "start getting quick quests!"); if (Servicenumber!=null) {System.out.println (Windowname + "Start for first" + Servicenumber + "fast customer service"); int serviceTime = Constants . min_service_time;try {thread.sleep (serviceTime);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Windowname + "completed for the first" + Servicenumber + "Number of fast customer service, total time-consuming" + servicetime/1000 + "seconds");} Else{system.out.println (Windowname + "not get to fast task! "); Commonservice ();}} VIP Service private void Vipservice () {IntegerServicenumber = Numbermachine.getinstance (). Getvipmanager (). Fetchnumber (); String windowname = "First" + number + "sign" + type + "window"; System.out.println (Windowname + "start getting VIP quests!"); if (Servicenumber!=null) {System.out.println (Windowname + "start for" + 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 + "not taken to 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 The purpose of a data 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; }

Startup 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) {//produces 4 ordinary windows for (int i=1;i<5;i++) {servicewindow window = new Servicewindow (); w Indow.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 ();//General Customer Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {public void run () {Integer servicenumber = Numbermachine.getinstance (). Getcommonmanager (). Generatenewnumber ();/** * Using logger mode, It is not possible to see the intuitive running effect, because the Logger.log method does not directly print out the content, but instead gives the internal thread to handle it, so the printed result looks chaotic in chronological order. *///logger.info ("the" + Servicenumber + "number of ordinary customers are waiting for service! "); System.out.println ("No." + ServicenuMber + "Number of ordinary customers are waiting for service! ");}},0,constants.common_customer_interval_time, timeunit.seconds);//fast customer Take number 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 take number Executors.newscheduledthreadpool ( 1). Scheduleatfixedrate (New Runnable () {public void run () {Integer servicenumber = Numbermachine.getinstance (). Getvipmanager (). Generatenewnumber (); System.out.println ("No." + Servicenumber + "VIP customer is 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.

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.