-------
模拟实现银行业务调度系统逻辑,具体需求如下:
银行内有6个业务窗口,1- 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
各类型客户在其对应窗口按顺序依次办理业务。
当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
分析对象:
客户类包含3种客户VIP客户,普通客户,快速客户
号码类(银行叫号机)异步随机生成以上各种类型的客户3种客户各自有不同的号码顺序
服务类有包含3种服务VIP服务,普通服务,快速服务
银行类开启各种服务窗口 启动号码类(生成各类客户)窗口按顺序叫号办理各种业务
客户类实现有3个客户采用枚举方式:
public enumCustomerType {
GENERAL, VIP, RAPIDBUSINESS;
public long spendTime;
@Override
public String toString() {
switch (this) {
case GENERAL:
return "普通";
case RAPIDBUSINESS:
return "快速";
case VIP:
return "VIP";
}
return null;
}
}
号码管理器(由号码机类来调用)编写:
public classNumberManager{
private int lastNumber = 1;
private List<Integer> queueNumber = new ArrayList<Integer>();
public synchronized int generateNewManager() {
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized Integer getServiceNumber() {
if (!queueNumber.isEmpty()) {
return queueNumber.remove(0);
}
return null;
}
}
号码机类编写(一个银行只有一个号码机,采用单例设计模式)编写:
public classNumberMachine{
private static NumberMachine instance=new NumberMachine();
private NumberManager generalManager = new NumberManager();
private NumberManager rapidManager = new NumberManager();
private NumberManager vipManager = new NumberManager();
public NumberManager getGeneralManager() {
return generalManager;
}
public NumberManager getRapidBusinessManager() {
return rapidManager;
}
public NumberManager getVipManager() {
return vipManager;
}
private NumberMachine(){}
//构造函数私有别人不能new新的对象
//采用静态函数取得本类实例
public static NumberMachine getInstance(){
return instance;
}
}
建一个常量类存放数据:
public classConstants{
public staticintMAX_SERVICE_TIME=10000;//最大服务时间毫秒
public staticintMIN_SERVICE_TIME=1000;//最小服务时间毫秒
public staticintGENERAL_CUSTOMER_INTERVAL_TIME=1;//生成普通客户间隔秒
public staticintVIP_CUSTOMER_INTERVAL_TIME=6;//生成VIP客户间隔秒
public staticintRAPIDBUSINESS_CUSTOMER_INTERVAL_TIME=3;//生成快速业务客户间隔秒
}
银行窗口服务类编写:
import java.util.Random;
import java.util.concurrent.Executors;
public classServiceWindow{
private CustomerType windowType=CustomerType.GENERAL;
private CustomerType customerType=CustomerType.GENERAL;
private int windowId;
public voidsetWindowType(CustomerType windowType) {
this.windowType = windowType;
}
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void start() {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
switch (windowType) {
case GENERAL:
generalServer();
break;
case RAPIDBUSINESS:
rapidBusinessServer();
break;
case VIP:
vipServer();
break;
}
}
}
//普通客户服务函数
public void generalServer() {
String windowName = getWindowName();
System.out.println(windowName + "正在获取服务");
Integer number = NumberMachine.getInstance()
.getGeneralManager().getServiceNumber();
customerType=CustomerType.GENERAL;
if (number != null) {
System.out.println(windowName + "正在为" + number
+"号普通客户服务...");
// 服务时间
spendTime(windowName,number,customerType);
}else{
System.out.println(windowName + "没有取到服务,休息1秒");
try {
Thread.sleep(1000);
}catch(InterruptedExceptione) {
System.out.println("出错了" + e);
}
}
}
//随机生成客户服务时间
public void spendTime(String windowName, Integer number,CustomerType customerType) {
long serviceTime = 0;
long beginTime = System.currentTimeMillis();
if (customerType ==CustomerType.RAPIDBUSINESS) {
serviceTime= Constants.MIN_SERVICE_TIME;
}else{
int random = new Random()
.nextInt(Constants.MAX_SERVICE_TIME) + 1;
serviceTime= (random < Constants.MIN_SERVICE_TIME? Constants.MIN_SERVICE_TIME
:random);
}
try {
Thread.sleep(serviceTime);
}catch(InterruptedExceptione) {
System.out.println("出错了" + e);
}
long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为" + number +customerType+"号客户完成服务,耗时:"
+(costTime / 1000) + "秒。。。。。。。");
}
//快速业务客户服务函数
public void rapidBusinessServer() {
String windowName = getWindowName();
Integer number = NumberMachine.getInstance()
.getRapidBusinessManager().getServiceNumber();
customerType=CustomerType.RAPIDBUSINESS;
if (number != null) {
System.out.println(windowName + "正在为" + number
+"号快速业务客户服务...");
spendTime(windowName,number,customerType);
}else{
System.out.println("没有快速业务客户,获取普通客户业务");
generalServer();
}
}
//VIP客户服务函数
public void vipServer() {
String windowName = getWindowName();
Integer number = NumberMachine.getInstance().getVipManager()
.getServiceNumber();
customerType=CustomerType.VIP;
if (number != null) {
System.out.println(windowName + "正在为" + number
+"号vip客户服务...");
spendTime(windowName,number,customerType);
}else{
System.out.println("没有vip客户,获取普通客户业务");
generalServer();
}
}
public String getWindowName() {
String windowName = "第" + windowId+ "号" + windowType + "窗口";
return windowName;
}
});
}
}
银行类在main中调用各类编写如下:
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public classBanker {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("银行开始营业了!");
//开启4个普通窗口
for (int i = 1; i <= 4; i++){
ServiceWindow generalWindow = new ServiceWindow();
generalWindow.setWindowId(i);
generalWindow.start();
}
//开启一个快速业务窗口
ServiceWindow rapidBusinessWindow = new ServiceWindow();
rapidBusinessWindow.setWindowType(CustomerType.RAPIDBUSINESS);
rapidBusinessWindow.setWindowId(5);
rapidBusinessWindow.start();
//开启一个VIP窗口
ServiceWindow vipWindow = new ServiceWindow();
vipWindow.setWindowType(CustomerType.VIP);
vipWindow.setWindowId(6);
vipWindow.start();
//随机生成普通客户线程
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
// command,
new Runnable() {
@Override
public void run() {
Integer number = NumberMachine.getInstance()
.getGeneralManager().generateNewManager();
System.out.println(number + "号普通客户等待服务");
}
},0, Constants.GENERAL_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
//随机生成快速业务客户线程
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
// command,
new Runnable() {
@Override
public void run() {
Integer number = NumberMachine.getInstance()
.getRapidBusinessManager().generateNewManager();
System.out.println(number + "号快速客户等待服务");
}
},0, Constants.RAPIDBUSINESS_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
//随机生成VIP客户线程
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
// command,
new Runnable() {
@Override
public void run() {
Integer number = NumberMachine.getInstance()
.getVipManager().generateNewManager();
System.out.println(number + "号vip客户等待服务");
}
},0, Constants.VIP_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
}
}
运行结果部分截图如下: