银行业务调度系统
模拟实现银行业务调度系统逻辑,具体需求如下:
Ø 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
Ø 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
Ø 异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
Ø 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
Ø 各类型客户在其对应窗口按顺序依次办理业务。
Ø 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
Ø 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
Ø 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
需要一个号码管理、一个取号机器、一个窗口服务,所以建立 NumberManager类、NumberMachine类、ServiceWindow类,关系如下:
一个取号机器上要包含3个取号机制,既普通客户的取号、快速客户的取号、VIP客户的取号
根据需要,必须将NumberMachine设定为单例模式类。
import java.util.List;
import java.util.ArrayList;
public class NumberManager {
private int lastNumber = 1;
private List queueNumber = new ArrayList();
public synchronized Integer generateNewManager(){
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized Integer fetchServiceNumber(){
if(queueNumber.size()>0)
return queueNumber.remove(0);
return null;
}
}
public class NumberMachine {
private NumberManager commonManager = new NumberManager();
private NumberManager expressManager = new NumberManager();
private NumberManager vipManager = new NumberManager();
public NumberManager getCommonManager() {
return commonManager;
}
public NumberManager getExpressManager() {
return expressManager;
}
public NumberManager getVipManager() {
return vipManager;
}
private NumberMachine(){}
private static NumberMachine instance = new NumberMachine();
public static NumberMachine getInstance(){
return instance;
}
}
import java.util.Random;
import java.util.concurrent.Executors;
public class ServiceWindow {
private CustomerType type = CustomerType.COMMON;
private int windowId = 1;
public void setType(CustomerType type) {
this.type = type;
}
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void start(){
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
switch (type) {
case COMMON:
CommonService();
break;
case EXPRESS:
ExpressService();
break;
case VIP:
VipService();
break;
}
}
}
});
}
public void CommonService() {
String windowName = "第"+windowId+"个"+type+"窗口";
Integer num = NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
System.out.println(windowName+"正在获取服务");
if(num!=null){
System.out.println(windowName+"为第"+num+"个"+"普通"+"客户服务");
long beginTime = System.currentTimeMillis();
int maxRandTime = ConsStants.MAX_SERVICE_TIME - ConsStants.MIN_SERVICE_TIME;
long serverTime = new Random().nextInt(maxRandTime)+1+ConsStants.MIN_SERVICE_TIME;
try {
Thread.sleep(serverTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis()-beginTime;
System.out.println(windowName+"为第"+num+"个"+"普通"+"客户完成服务,耗时:"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到到服务,先休息一秒钟");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void ExpressService() {
String windowName = "第"+windowId+"个"+type+"窗口";
Integer num = NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName+"正在获取服务");
if(num!=null){
System.out.println(windowName+"为第"+num+"个"+type+"客户服务");
long beginTime = System.currentTimeMillis();
//int maxRandTime = ConsStants.MAX_SERVICE_TIME - ConsStants.MIN_SERVICE_TIME;
//long serverTime = new Random().nextInt(maxRandTime)+1+ConsStants.MIN_SERVICE_TIME;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis()-beginTime;
System.out.println(windowName+"为第"+num+"个"+type+"客户完成服务,耗时:"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到到服务,先休息一秒钟");
CommonService();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void VipService() {
String windowName = "第"+windowId+"个"+type+"窗口";
Integer num = NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName+"正在获取服务");
if(num!=null){
System.out.println(windowName+"为第"+num+"个"+type+"客户服务");
long beginTime = System.currentTimeMillis();
int maxRandTime = ConsStants.MAX_SERVICE_TIME - ConsStants.MIN_SERVICE_TIME;
long serverTime = new Random().nextInt(maxRandTime)+1+ConsStants.MIN_SERVICE_TIME;
try {
Thread.sleep(serverTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis()-beginTime;
System.out.println(windowName+"为第"+num+"个"+type+"客户完成服务,耗时:"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到到服务");
CommonService();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public enum CustomerType {
COMMON,EXPRESS,VIP;
@Override
public String toString() {
// TODO Auto-generated method stub
switch (this) {
case COMMON:
return "普通";
case EXPRESS:
return "快速";
case VIP:
return name();
}
return null;
}
}
public class ConsStants {
public static int MAX_SERVICE_TIME = 10000;
public static int MIN_SERVICE_TIME = 1000;
}
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
for (int i = 1; i <= 5; i++) {
ServiceWindow commonwindow = new ServiceWindow();
commonwindow.setWindowId(i);
commonwindow.start();
}
ServiceWindow vipwindow = new ServiceWindow();
vipwindow.setWindowId(7);
vipwindow.setType(CustomerType.VIP);
vipwindow.start();
ServiceWindow expresswindow = new ServiceWindow();
expresswindow.setWindowId(6);
expresswindow.setType(CustomerType.EXPRESS);
expresswindow.start();
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Integer num = NumberMachine.getInstance().getCommonManager().generateNewManager();
System.out.println("第"+num+"号普通客户等待服务");
}
},
0,
1,
TimeUnit.SECONDS
);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Integer num = NumberMachine.getInstance().getVipManager().generateNewManager();
System.out.println("第"+num+"号VIP客户等待服务");
}
},
0,
6,
TimeUnit.SECONDS
);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Integer num = NumberMachine.getInstance().getExpressManager().generateNewManager();
System.out.println("第"+num+"号快速客户等待服务");
}
},
0,
3,
TimeUnit.SECONDS
);
}
}