Dark Horse programmer-7 k interview questions (2) ----- banking business Scheduling System

Source: Internet
Author: User

Requirements:

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 ).

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.

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.

Business Process and analysis:

    Process: currently, banks have a high degree of Informatization. Every time we go to a bank to handle business operations, we need to get a number first, wait in the queue for a call, and distinguish between various special types, the process follows, but we know that there are many types of business users, such as large transactions and financial services, there are also remittance and settlement services, as well as various payment services. Some are also called company business, personal business, VIP business, and so on. We follow the project requirements. There are three types of customers, VIP customers, common customers, and fast customers. There are six service windows: Window 1-4 is a normal window, window 5 is a quick window, and window 6 is a VIP window. However, the service window needs to provide services to the customer. The only credential you need is that you have obtained the number, so there is a number machine, and there are three types of objects in this machine: VIP customers, ordinary customers, quick customer. Each object in the number receiving machine must provide a number for the customer who handles the business. At the same time, it also provides a service number for the service window. This is also a number manager object. Therefore, it has the number retrieval and call functions, and the service window has the business processing function. Therefore, the object-oriented design must first design these objects, such as number machines (), number managers (), service windows (), and randomly generated personnel.

Class diagram:

    

Import java. util. ArrayList;
Import java. util. List;

/*
* This is the number machine */
Public class NumbeiManager {
Private int lastNumber = 1; // record the number of the number to be generated
Private List <Integer> queueNumber = new ArrayList <Integer> (); // API-oriented and parent-class programming. This is a professional List instead of an ArrayList.
Public synchronized Integer generateNewManager () {// you can generate a number Integer to avoid null if no number exists. null cannot be converted to int, and a null pointer is abnormal.
QueueNumber. add (lastNumber );
Return lastNumber ++;
}
Public synchronized Integer fetchServiceNumber () // The original number returned by the customer is int. Integer can avoid null if no number is available. null cannot be converted to int, and a null pointer is abnormal.
{
Integer number = null;
If (queueNumber. size ()> 0)
Number = queueNumber. remove (0 );
Return number;
}
}

 

Package Bank;
/*
* Machines with three numbers
**/
Public class NumberMachine {
Private NumbeiManager commonManager = new NumbeiManager (); // generate a common customer number
Private NumbeiManager expressManager = new NumbeiManager (); // generate a Quick customer number
Private NumbeiManager vipManager = new NumbeiManager (); // generate the vip Customer Number
Public NumbeiManager getCommonManager (){
Return commonManager;
}
Public NumbeiManager getExpressManager (){
Return expressManager;
}
Public NumbeiManager getVipManager (){
Return vipManager;
}
// Singleton design: design the machine as a singleton and create an object of its own, and provide an interface to external users.
Private NumberMachine (){}
Private static NumberMachine instance = new NumberMachine (); // you must provide an object.
Public static NumberMachine getInstance () // provides an external method for each instance.
{
Return instance;
}
}

Package Bank;

Import java. util. Random;
Import java. util. concurrent. Executors;

Public class ServiceWindow {
Private CustemerType = CustemerType. COMMON;
Private int windowId = 1;
Public void setType (CustemerType ){
This. type = type;
}
Public void setWindowId (int windowId ){
This. windowId = windowId;
}
 
Public void start ()
{
Executors.newsinglethreadexecutor(cmd.exe cute (new Runnable (){

@ Override
Public void run (){
While (true)
{
Switch (type)
{
Case COMMON:
CommonService ();
Break;
Case EXPRESS:
ExpressService ();
Break;
Case VIP:
VipService ();
Break;

}
}
}
});
}

Private void commonService (){
String windowName = "no." + windowId + "+ type +" window ";
System. out. println (windowName + "acquiring task ");
Integer number = NumberMachine. getInstance (). getCommonManager (). fetchServiceNumber ();

If (number! = Null)
{
System. out. println (windowName + "indicates" + number + "+ type +" Customer Service ");
Long beginTime = System. currentTimeMillis ();
Int maxRand = Constants. MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
Long serverTime = new Random (). nextInt (maxRand) + 1 + Constants. MIN_SERVICE_TIME;
Try {
Thread. sleep (serverTime );
} Catch (Exception e ){
E. printStackTrace ();
}
Long costTime = System. currentTimeMillis ()-beginTime;
System. out. println (windowName + "indicates" + number + "" + "common customer service, time consumed" + costTime/1000 );
}
Else
{
System. out. println (windowName + "no service task is obtained. Take a rest for 1 second ");
Try {
Thread. sleep (1000 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Private void expressService (){
String windowName = "no." + windowId + "+ type +" window ";
System. out. println (windowName + "acquiring task ");
Integer number = NumberMachine. getInstance (). getExpressManager (). fetchServiceNumber ();
If (number! = Null)
{
System. out. println (windowName + "indicates" + number + "+ type +" Customer Service ");
Long beginTime = System. currentTimeMillis ();
// Int maxRand = Constants. MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
// Long serverTime = new Random (). nextInt (maxRand) + 1 + Constants. MIN_SERVICE_TIME;
Try {
Thread. sleep (Constants. MIN_SERVICE_TIME );
} Catch (Exception e ){
E. printStackTrace ();
}
Long costTime = System. currentTimeMillis ()-beginTime;
System. out. println (windowName + "indicates" + number + "+ type +" customer service, time consumed "+ costTime/1000 );
}
Else
{
System. out. println (windowName + "no service task retrieved ");
CommonService ();
Try {
Thread. sleep (1000 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Private void vipService (){
String windowName = "no." + windowId + "+ type +" window ";
System. out. println (windowName + "acquiring task ");
Integer number = NumberMachine. getInstance (). getVipManager (). fetchServiceNumber ();
If (number! = Null)
{
System. out. println (windowName + "indicates" + number + "+ type +" Customer Service ");
Long beginTime = System. currentTimeMillis ();
Int maxRand = Constants. MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
Long serverTime = new Random (). nextInt (maxRand) + 1 + Constants. MIN_SERVICE_TIME;
Try {
Thread. sleep (serverTime );
} Catch (Exception e ){
E. printStackTrace ();
}
Long costTime = System. currentTimeMillis ()-beginTime;
System. out. println (windowName + "indicates" + number + "+ type +" customer service, time consumed "+ costTime/1000 );
}
Else
{
System. out. println (windowName + "no service task retrieved ");
CommonService ();
Try {
Thread. sleep (1000 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}

Package Bank;

Public enum CustemerType {
COMMON, EXPRESS, VIP;
Public String toString ()
{
Switch (this)
{
Case COMMON:
Return "normal ";
Case EXPRESS:
Return "quick ";
Case VIP:
Return "VIP ";
}
Return null;
}
}

Package Bank;

Public class Constants {
Public static int MAX_SERVICE_TIME = 10000; // maximum time
Public static int MIN_SERVICE_TIME = 1000; // minimum time
Public static int COMMON_CUSTOMER_INTERVAL_TIME = 1; // interval between common customers
}

Package Bank;

Import java. util. concurrent. Executors;
Import java. util. concurrent. TimeUnit;

Public class MainClass {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Five normal Windows
For (int x = 1; x <5; x ++)
{
ServiceWindow commonWindow = new ServiceWindow ();
CommonWindow. setWindowId (x );
CommonWindow. start ();
}
// A vip window
ServiceWindow VipWindow = new ServiceWindow ();
VipWindow. setType (CustemerType. VIP );
VipWindow. start ();
// An expree window
ServiceWindow ExpressWindow = new ServiceWindow ();
ExpressWindow. setType (CustemerType. EXPRESS );
ExpressWindow. start ();

Executors. newScheduledThreadPool (1). scheduleAtFixedRate (
New Runnable (){

@ Override
Public void run (){
Integer number = NumberMachine. getInstance (). getCommonManager (). generateNewManager ();
System. out. println (number + "regular customer waiting for service ");
}
},
0,
Constants. COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit. SECONDS );
Executors. newScheduledThreadPool (1). scheduleAtFixedRate (
New Runnable (){

@ Override
Public void run (){
Integer number = NumberMachine. getInstance (). getExpressManager (). generateNewManager ();
System. out. println (number + "No. Fast customer waiting for service ");
}
},
0,
Constants. COMMON_CUSTOMER_INTERVAL_TIME * 2,
TimeUnit. SECONDS );
Executors. newScheduledThreadPool (1). scheduleAtFixedRate (
New Runnable (){

@ Override
Public void run (){
Integer number = NumberMachine. getInstance (). getVipManager (). generateNewManager ();
System. out. println (number + "VIP customer waiting for service ");
}
},
0,
Constants. COMMON_CUSTOMER_INTERVAL_TIME * 6,
TimeUnit. SECONDS );
}

}

 

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.