银行业务调度模拟逻辑:
----银行内由6个窗口,其中1-4号为普通窗口,5号为快速窗口,6号为vip窗口。
----由三种对应类型的客户,vip客户,普通客户,快速客户
----异步生成三种类型的随即客户,比率为:普通客户,vip客户,快速客户;6:1:3
----客户办理业务所需要的时间最大值和最小值
----个类型的客户按照顺序在窗口上一次办理业务。
----当vip窗口和快速客户窗口没有客户时也可以办理普通用户的业务。
----随机生成客户时间间隔及业务办理时间最大值和最小值
首先对于这个银行业务管理系统,先对整个系统进行分析,运用面向对象的思想,抽象出几个类,
然后通过线程对各个类进行调度。通过分析,可以抽象出服务窗口和用户排队号这两个类,通过号
的管理类来产生用户的排队号。
具体实现如下:
服务窗口类:
public class serviceWindow {
private CoustomType type = CoustomType.COMMON;
private int windowId = 1;
public void setType(CoustomType type) {
this.type = type;
}
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void start() {
Executors.newSingleThreadExecutor().execute(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 = "第" + windowId + "号" + type + "窗口";
Integer CustomerId = NumberMachine.getInstance()
.getCommonNumberManager().fetchNumber();
System.out.println(windowName + "正在获取普通服务");
if (CustomerId != null) {
System.out.println(windowName + "正在为" + CustomerId + "号普通客户服务");
Random rand = new Random();
int costTime = rand.nextInt(Constant.MAX_TIME) + 1
+ Constant.MIN_TIME;
try {
Thread.sleep(costTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName + "为" + CustomerId + "号普通服务耗费时间"
+ costTime / 1000 + "秒");
} else {
System.out.println(windowName + "没有获取到普通客户,先休息一秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void expressService() {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer CustomerId = NumberMachine.getInstance()
.getCommonNumberManager().fetchNumber();
System.out.println(windowName + "正在获取快速服务");
if (CustomerId != null) {
System.out.println(windowName + "正在为" + CustomerId + "号快速客户服务");
try {
Thread.sleep(Constant.MIN_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName + "为" + CustomerId + "号快速客户服务耗费时间"
+ Constant.MIN_TIME / 1000 + "秒");
} else {
System.out.println(windowName + "没有获取到快速客户");
commonService();
}
}
private void vipService() {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer CustomerId = NumberMachine.getInstance()
.getCommonNumberManager().fetchNumber();
System.out.println(windowName + "正在获取vip服务");
if (CustomerId != null) {
System.out.println(windowName + "正在为" + CustomerId + "号vip客户服务");
try {
Thread.sleep(Constant.MAX_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName + "为" + CustomerId + "号vip客户服务耗费时间"
+ Constant.MAX_TIME / 1000 + "秒");
} else {
System.out.println(windowName + "没有获取到vip客户");
commonService();
}
}
}
排队号产生类:NumberMachine运用了单例模式
public class NumberMachine {
/**
* 单例模式
*/
private NumberManager CommonNumberManager=new NumberManager();
private NumberManager ExpressNumberManager=new NumberManager();
private NumberManager VIPNumberManager=new NumberManager();
public NumberManager getCommonNumberManager()
{
return CommonNumberManager;
}
public NumberManager getExpressNumberManager()
{
return ExpressNumberManager;
}
public NumberManager getVIPNumberManager()
{
return VIPNumberManager;
}
private NumberMachine()
{
}
public static NumberMachine getInstance()
{
return machine;
}
private static NumberMachine machine=new NumberMachine();
}
numberManager类:
public class NumberManager {
private int lastNumber=1;
private List<Integer> numbers=new ArrayList<Integer>();
public synchronized Integer generateNewNumber()
{
numbers.add(lastNumber++);
return lastNumber;
}
public synchronized Integer fetchNumber()
{
if(numbers.size()>0)
{
return numbers.remove(0);
}
return null;
}
}
客户类型枚举:
public enum CoustomType {
COMMON,EXPRESS,VIP;
public String toString()
{
if(this==COMMON)
{
return "普通";
}
if(this==EXPRESS)
{
return "快速";
}
if(this==VIP)
{
return "VIP";
}
return null;
}
}