Black Horse Programmer __ Payment service dispatching system

Source: Internet
Author: User

Payment Service Scheduling System:

The simulation realizes the logic of the payment service scheduling system, the specific requirements are as follows:

There are 6 business windows in the Bank, window 1-4 is normal window, window 5th is Quick window, window 6th 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 processing the business can be simulated by thread sleep).

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

When the VIP (6th) window and the Fast Business (5th) window do not have customers waiting for business, these two windows can handle the business of ordinary customers, and once the corresponding customers waiting 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.

--------------------------------------------------------------------------------------------------------------- ------------

Analysis and Design:

Three types of customers are: VIP customers, ordinary customers, fast customers. Asynchronous random generation of various types of customers, all types of customers in their corresponding window in order to transact business sequentially.

Each type of customer in the order of queuing, by the window, queue service.

Numbermanager class:

1. Define a member variable to store the previous customer number and a collection of queues to store all the customer numbers waiting for the service.


2, define a method of generating a new number and get the number to be served immediately, these two methods are different threads to manipulate the same data, so, to synchronize.


public class Numbermanager {private int lastnumber = 1;//Last number, initial value is 1private list<integer> queuenumber = new Arrayli St<integer> ();//number list in line/** * Queueing method * @return */public synchronized Integer Generatenewmanager () {Queuenumber.add ( Lastnumber);//Add the last number to the queue return lastnumber++;//increment the ordinal 1}/** * Window number method * @return */public synchronized Integer Fetchservicenumber () {Integer number = null, if (Queuenumber.size () >0) # = Queuenumber.remove (0);// First put in the first take out return number;//return this number}}

---

Numbermachine class:

Define three member variables to point to three Numbermanager objects, respectively, the number manager for normal, fast, and VIP customers, and define three corresponding methods to return the three Numbermanager objects.

The Numbermachine class can be designed as a single case.

public class Numbermachine {//Singleton mode create Numbermachineprivate Numbermachine () {}private static numbermachine instance = new Nu Mbermachine ();p ublic static Numbermachine getinstance () {return instance;}   Various user number Manager private Numbermanager Commonmanager = new Numbermanager ();  Ordinary user private Numbermanager Expressmanager = new Numbermanager ();      Fast User private Numbermanager Vipmanager = new Numbermanager (); VIP User Public Numbermanager Getcommonmanager () {return commonmanager;} Public Numbermanager Getexpressmanager () {return expressmanager;} Public Numbermanager Getvipmanager () {return vipmanager;}}

--

CustomerType Enum class:

There are three types of customers in the system, so define an enumeration class that defines three members representing three types of customers, respectively. Overrides the ToString method, which returns the Chinese name of the type. This is reconstructed at the back of the code and is not considered at first.

public enum CustomerType {common,express,vip; @Overridepublic String toString () {switch (this) {case Common:return "normal"; Case Express:return "Fast"; case Vip:return name ();} return null;}}

----

Servicewindow class:

Defines a start method that internally initiates a thread that loops through three different methods, respectively, according to the category of the service window. Define three methods to service three kinds of customers, in order to observe the effect of the operation, should print out the details of the detailed information

Public class servicewindow {private customertype type = customertype.common ;  //window Type private int windowid = 1;//window number Public void settype (CustomerType  type)  { this.type = type;} Public void setwindowid (Int windowid)  {this.windowid = windowid;} Public void start () {executors.newsinglethreadexecutor (). Execute (new runnable () {@Overridepublic  void run ()  {while (True)  {switch (type)  {case common:commonservice ();// General User Service Break;case express:expressservice ();//Fast User Service Break;case vip:vipservice ();//VIP User Service Break;}}} Private void commonservice ()  {String windowName =  "section"  + windowId  +  "number"  + type +  "window"  ; System.out.println ("Getting Tasks"); Integer number = numbermachine.getinstance ()       //gets the number of the user being queued. Getcommonmanager (). FetchservicENumber (); if (number != null) {int begintime =  (int)  system.currenttimemillis ();// Time of service start Int maxrand = constants.max_service_time-constants.min_service_time;long servetime  = new random (). Nextint (Maxrand)  + 1 + constants.min_service_time;// The time Try {thread.sleep (servetime) used by the service, and//the time spent in simulating a service with thread sleep} catch  (interruptedexception e)  {e.printstacktrace ();} int endtime =  (int)  system.currenttimemillis ();//Time to end the service int costtime =  Endtime-begintime; System.out.println (windowname +  "for the first"  + number +  " + type +"   "Customer complete service, time consuming"  +  (costtime/1000)  +  "seconds");}  else {system.out.println (windowname +  "did not take the service task! Try {thread.sleep (1000);//If you do not get the number of the queued user, wait 1 seconds to continue getting} catch  (interruptedexception e)  {e.printstacktrace ();}}} Private void expressservice ()  {string windowname =  "Number"  + windowId +  "No."  + type +  "window"  ; System.out.println ("Getting Tasks"); Integer number = numbermachine.getinstance ()       //gets the number of the user being queued. Getexpressmanager (). Fetchservicenumber (); if (number != null) {int  begintime =  (int)  system.currenttimemillis ();   //Service start Time Try {thread.sleep ( Constants.min_service_time);  } catch  (interruptedexception e)  { E.printstacktrace ();} int endtime =  (int)  system.currenttimemillis ();   //time to end the service Int costtime  = endTime-beginTime; System.out.println (windowname +  "for the first"  + number +  " + type +"   "Customer complete service, time consuming"  +  (costtime/1000)  +  "seconds");}  else {system.out.println (windowname +  "did not take the service task! "); Commonservice ();}} Private void vipservice ()  {string windowname =  "No."  + windowId +  "number"  + type +  "window"  ; System.out.println ("Getting Tasks"); Integer number = numbermachine.getinstance ()                  //gets the number of the user being queued. GetVipManager (). Fetchservicenumber (); if (number != null) {int begintime =  (int)   System.currenttimemillis ();//service start time Int maxrand = constants.max_service_time-constants.min_ Service_time;long servetime = new random (). Nextint (Maxrand)  + 1 +  Constants.min_service_time;try {thread.sleep (servetime);}  catch  (interruptedexception e)  {e.printstacktrace ();} int endtime =  (int)  system.currenttimemillis ();//Time to end the service int costtime =  Endtime-begintime; System.out.println (windowname +  "for the first"  + number +  " + type +"   "Customer complete service, time consuming"  +  (costtime/1000)  +  "SEC");}  else {system.out.println (windowname +  "did not take the service task! "); Commonservice ();}});}}

----

MainClass class:

Create 4 normal windows with a for loop, then create 1 quick windows and a VIP window. Then create three timers to create a new regular customer number, a new fast customer number, a new VIP customer number, respectively.

Public class mainclass {public static void main (String[] args)  {// Create 4 normal windows for (int i=1;i<5;i++)  {  ServiceWindow commonWindow = new  Servicewindow (); Commonwindow.setwindowid (i); Commonwindow.start ();} Create VIP window Servicewindow vipwindow = new servicewindow (); Vipwindow.settype (CustomerType.VIP) ; Vipwindow.start ();//Create Quick Window Servicewindow expresswindow = new servicewindow (); Expresswindow.settype (customertype.express); Expresswindow.start (); Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New runnable () {@Overridepublic  void run ()  {integer number = numbermachine.getinstance (). Getcommonmanager (). GenerateNewManager (); /create normal user timer System.out.println (number +  "General Customer wait Service");}}, 0, constants.common_customer_ Interval_time, timeunit.seconds); Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New runnable () {@OverridePublic void run ()  {integer number = numbermachine.getinstance (). GetVipManager (). Generatenewmanager ();//create VIP User Timer System.out.println (number +  "VIP Customer Waiting Service");}}, 0,  Constants.common_customer_interval_time * 6, timeunit.seconds); Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New runnable () {@Overridepublic  void run ()  {integer number = numbermachine.getinstance (). Getexpressmanager (). GenerateNewManager () ;//create Fast User timer System.out.println (number +  "Fast Customer wait Service");}}, 0, constants.common_customer_ interval_time * 2, timeunit.seconds);}}


Black Horse Programmer __ Payment service dispatching system

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.