Java interview questions-Bank Business Scheduling

Source: Internet
Author: User

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.

After reading the requirements, you can start with your own ideas:

Import Java. text. dateformat; import Java. text. simpledateformat; import Java. util. arraylist; import Java. util. date; import Java. util. enumeration; import Java. util. using list; import Java. util. random; import Java. util. vector; import Java. util. concurrent. timeunit; public class test {dateformat date; // This is the limit probability. Static variables are not allowed for a type of customer // internal classes, the static int defaultcount = 0; static int fastcount = 0; static int vipco Unt = 0; public test () {date = new simpledateformat ("HH: mm: SS") ;}// customer class, representing various customers public class customer {private string name; // customer type 2, 1, 3 sub-table representatives, fast, VIP, common customer private int type; private partition list <customer> window; Public void setwindow (partition list <customer> window) {This. window = Window;} public window list <customer> getwindow () {return window;} // time required for processing the business, private int time in seconds; Public int GetType () {return type ;} pub LIC int gettime () {return time;} public void setname (string name) {This. name = Name;} Public String getname () {return name;} public customer (INT type) {This. type = type; random r = new random (); // 2 is a fast customer, 1 is a VIP customer, and 3 is a common customer, the three numbers are the base number // random time for processing the business, and the time required is calculated based on the customer type. The time sequence of the // principle is normal> VIP> fast customer time = (this. type % 2 + this. type) * R. nextint (4) + 5 ;}/// window class, representing each window waiting for the container public class window implements custlinstener {// each window Port Container private window list <customer> vip6; private window list <customer> fast5; // This is a common window container private arraylist <custom list <customer> defaults124; private window list <customer> default1; private comment list <customer> default2; private comment list <customer> default3; private comment list <customer> default4; public comment list <customer> getvip6 () {return vip6 ;} public writable list <customer> getfast5 () {return fast5;} public Response List <customer> getdefault1 () {return default1;} public response list <customer> getdefault2 () {return default2;} public response list <customer> getdefault3 () {return default3 ;} public window list <customer> getdefault4 () {return default4;} public window () {// initialize each window default1 = new window list <customer> (); default2 = new consumer list <customer> (); default3 = new consumer list <customer> (); default4 = new consumer list <Customer> (); defaults124 = new arraylist <shortlist <customer> (); defaults124.add ult1; defaults124.add (default2); defaults124.add (default3); defaults124.add (default4 ); fast5 = new consumer list <customer> (); vip6 = new consumer list <customer> () ;}// Add a public void addcustomer (customer Cu) {If (Cu. getType () = 2) {Cu. setwindow (fast5); fast5.add (Cu); system. out. println (date. format (new date () + "-- [" + cu. g Etname () + "]: the customer is added to the [quick window] queue and is waiting for [quick business]");} else if (Cu. getType () = 1) {Cu. setwindow (vip6); vip6.add (Cu); system. out. println (date. format (new date () + "-- [" + cu. the customer getname () + "] is added to the [VIP window] queue and waits for [VIP service]");} else {// for common users, in principle, the int size = 0; For (INT I = 1; I <4; I ++) {If (defaults124.get (I ). size () <defaults124.get (size ). size () size = I;} defaults124.get (size ). add (Cu); system. out. println (Date. Format (new date () + "-- [" + cu. getname () + "]: the customer is added to the [common window] queue and waits for [common business]");} // the customer's business has been completed, delete a customer public void removecustomer (empty list <customer> window) {// if it is a quick window service and there is no one in the quick window, adjust it from the normal window. If (window = fast5 & fast5.size () = 0) {// first, judge whether all common windows have Boolean usingall = true; for (partition list <customer> W: defaults124) {If (W. size () = 0) usingall = false;} // if all are in use, adjust if (usingall) {// first obtain which common window person is the most in T size = 0; // The precondition is that they all have customers for (INT I = 1; I <5; I ++) {If (defaults124.get (I ). size ()> defaults124.get (size ). size () size = I;} // the customer who receives the business from the next window with the largest number of users has been sent to the quick window customer temp = defaults124.get (size ). get (1); defaults124.get (size ). remove (1); fast5.addlast (temp);} else {customer C = Window. peek (); system. out. println (date. format (new date () + "[" + C. getname () + "] Customer leaving"); window. poll () ;}} else {customer C = W Indow. peek (); system. out. println (date. format (new date () + "[" + C. getname () + "] Customer leaving"); window. poll () ;}@ overridepublic void custevent (custevent e) {// todo auto-generated method stubcustomer Cu = new customer (E. getType (); Cu. setname (E. getname (); addcustomer (Cu) ;}// clerk, who is responsible for public class officer {private window windows; private window list <customer> default1; private region list <customer> defau Lt2; private comment list <customer> default3; private comment list <customer> default4; private comment list <customer> fast5; private comment list <customer> vip6; Public Officer (window W) {windows = W; default1 = W. getdefault1 (); default2 = W. getdefault2 (); default3 = W. getdefault3 (); default4 = W. getdefault4 (); fast5 = W. getfast5 (); vip6 = W. getvip6 () ;}// six salesmen start to work public void startwork () {// clerk 1 job thread T1 = new t Hread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {While (! Thread. interrupted () {If (default1.size ()> 1) {doing (default1, "normal window ①") ;}} catch (interruptedexception E) {// todo auto-generated catch blocke. printstacktrace () ;}}); // worker thread t2 = new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {While (! Thread. interrupted () {If (default2.size ()> 1) {doing (default2, "normal window ②") ;}} catch (interruptedexception E) {// todo auto-generated catch blocke. printstacktrace () ;}}); // clerk 3 job thread T3 = new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {While (! Thread. interrupted () {If (default3.size ()> 1) {doing (default3, "normal window ③") ;}} catch (interruptedexception E) {// todo auto-generated catch blocke. printstacktrace () ;}}); // clerk 4 job thread t4 = new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {While (! Thread. interrupted () {If (default4.size ()> 1) {doing (default4, "normal window ④") ;}} catch (interruptedexception E) {// todo auto-generated catch blocke. printstacktrace () ;}}); // working thread T5 = new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {While (! Thread. interrupted () {If (fast5.size ()> 1) {doing (fast5, "Quick window ⑤") ;}} catch (interruptedexception E) {// todo auto-generated catch blocke. printstacktrace () ;}}); // number 6 VIP window clerk work thread T6 = new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {While (! Thread. interrupted () {If (vip6.size ()> 1) {doing (vip6, "VIP window 6") ;}} catch (interruptedexception E) {// todo auto-generated catch blocke. printstacktrace () ;}}); t1.setpriority (thread. norm_priority); t2.setpriority (thread. norm_priority); t3.setpriority (thread. norm_priority); t4.setpriority (thread. norm_priority); t5.setpriority (thread. norm_priority); t6.setpriority (thread. norm_priority); t1.start (); t2.sta RT (); t3.start (); t4.start (); t5.start (); t6.start ();} private void doing (empty list <customer> window, string wn) throws interruptedexception {customer C = Window. peek (); system. out. println (date. format (new date () + "[" + C. getname () + "]: the customer handles the business at [" + Wn + "]. The estimated time is [" + C. gettime () + "] seconds"); thread. yield (); timeunit. seconds. sleep (C. gettime (); windows. removecustomer (window) ;}// this class randomly generates the customer's public class godking im Plements runnable {// event custadaper CA; // indicates the customer's private integer number; // random number seed private random; // window // Private window windows; Public godking (window W) {number = 100; random = new random (); // windows = W; CA = new custadaper () ;}// start generating the customer @ overridepublic void run () {// todo auto-generated method stubtry {thread. currentthread (). setpriority (thread. min_priority); While (! Thread. interrupted () {// here to achieve VIP customers: ordinary customers: Quick customers = ratio of 1: 6: 3 int dtype = random. nextint (3) + 1; // The ratio of the quick business customer to that of the common customer is not reached, but the VIP is reached, avoid vipif (vipcount = 1 & fastcount <3 & defaultcount <6) {do {dtype = random. nextint (3) + 1 ;}while (dtype = 1); // The ratio of the fast client to the VIP client is already specified, normal not yet} else if (vipcount = 1 & fastcount = 3 & defaultcount <6) {do {dtype = random. nextint (3) + 1;} while (dtype = 1 | dtype = 2);} else {// The counter has been set to vipcount = 0; fastcount = 0; defaultcount = 0;} switch (dtype) {Case 1: vipcount ++; Case 2: fastcount ++; Case 3: defaultcount ++ ;} // customer C = new customer (dtype); // C. setname (number. tostring (); // windows. addcustomer (c); Number ++; custevent E = new custevent (); E. setname (number. tostring (); E. settype (dtype); CA. notifycustevent (E); // a maximum of 10 seconds for one thread. yield (); timeunit. seconds. sleep (random. nextint (10);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public void addcusteventlistener (custlinstener Cu) {ca. addcustlistener (Cu) ;}} public static void main (string [] ARGs) {test T = new test (); window windows = T. new window (); officer officers = T. new officer (Windows); godking God = T. new godking (Windows); God. addcusteventlistener (Windows); officers. startwork (); God. run () ;}// new customer event public class custevent {private int type; private string name; Public int GetType () {return type;} public void settype (INT type) {This. type = type;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}} public interface custlinstener {public void custevent (custevent E);} public class custadaper {private vector <custlinstener> events = new vector <custlinstener> (); custlinstener Cu; Public void addcustlistener (custlinstener WC) {events. addelement (WC);} public void policycustevent (custevent e) {enumeration <custlinstener> listener = events. elements (); While (listener. hasmoreelements () {Cu = listener. nextelement (); Cu. custevent (e );}}}}

These hundreds of lines actually caused Stack Overflow during JS coloring and parsing. IE6 is not good...

I like to write a single-class single file for something I want to learn and test. This facilitates copying, pasting, and directly compiling. There are many internal classes.

After writing, I found that the business implementation logic of the first five to the first six simulation customers was always abnormal. I spent more than half a day studying it as a magic horse. At first, I was not planning to use events, howeverLockThe problem is, think about whether the event mechanism will solve the problem. The result is still not solved, but the development mode has become nondescribable. How can we change the thread priority level? In the highest mode, the computer crashed.

Think about whether there are too many threads. It is still a problem to change it to two separate threads. Finally, let's take a closer look at thinking in Java. Concurrency is really hard to say, in addition, how can several threads ensure synchronization when the time slice is originally allocated ....

Finally, I read instructor Zhang's code ,..... It's different from my path. I think it's too detailed. As a result, I made so many orders for myself. Let's take a look at this first, and then study concurrency in another day.

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.