-------------------------- android培训、java培训、期待与您交流! --------------------------
银行调度系统
1,简单的需求分析
三种窗口对应三类客户,其中普通窗口四个,快速与VIP各一个,客户都是随机产生的,客户数的比例是VIP:普通客户:快速客户=1:6:3,业务办理时间有范围,在该范围内随机设定每个VIP以及普通客户的办理业务时间,快速客户就设置为最小值。其中VIP和快速窗口空闲时可以办理普通客户业务,一旦有对应的业务时就优先处理他们的业务。
2、 设计类
设计号码管理器类 ,定义一个用于存储上一个客户号码的成员变量和用于存储所有等待服务的客户号码的队列集合。定义一个产生新号码的方法和获取马上要为之服务的号码的方法,这两个方法被不同的线程操作了相同的数据,所以,要进行同步。
设计号码取号机类,定义三个成员变量分别指向三个号码管理器对象,分别表示普通、快速和VIP客户的号码管理器,定义三个对应的方法来返回这三个号码管理器对象。因为这个机器只有一个,所以设计成为单例模式。
设计三种客户类,可以设计成为枚举,因为只有三种选择。
设计窗口类,定义一个start方法,内部启动一个线程,根据服务窗口的类别分别循环调用三个不同的方法。
部分代码解析
//③服务窗口
public class SevriceWindow {
//窗口的叫号方法,需要用到多线程
private CustomerType type = CustomerType.COMMON;//这里初始化为任意的类型客户都行
private int WindowId = 1;
public void setType(CustomerType type) {//这里的setType方法是为了设置Start线程中的客户类型已经所调用的客户方法
this.type = type;
}
public CustomerType getType() {
return type;
}
public void setWindowId(int windowId) {
WindowId = windowId;
}
public void start(){
Executors.newSingleThreadExecutor().execute(
new Runnable(){
public void run(){
while(true){//忘记加循环了
switch(type){//这里要获得什么类型的窗口所以我们要定义一个客户类型的枚举
case COMMON:
commonService();
break;
case EXPRESS:
excpressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
private void commonService() {
String windowName = "第"+WindowId+"号"+type+"窗口";
Integer number = NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
System.out.println("第"+WindowId+"号"+type+"窗口获取普通服务");
if(number!=null)
{
System.out.println(windowName + "开始为第" + number + "号普通客户服务");
//long startTime = System.currentTimeMillis();
int maxRandom =TimeConstants.MAX_SEVRICE_TIME - TimeConstants.MIN_SEVRICE_TIME;
int serviceTime = new Random().nextInt(maxRandom)+1+TimeConstants.MIN_SEVRICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
//long endTime = System.currentTimeMillis()-startTime;//服务时间
System.out.println(windowName+"为"+number+"号"+type+"客户服务,用时"+serviceTime/1000+"秒");
}
else{
System.out.println(windowName+"没有客户,休息中...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void excpressService() {
String windowName = "第"+WindowId+"号"+type+"窗口";
Integer number = NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println("第"+WindowId+"号"+type+"窗口获取快速服务");
if(number!=null)
{
System.out.println(windowName + "开始为第" + number + "号快速客户服务");
//long startTime = System.currentTimeMillis();
int maxRandom =TimeConstants.MAX_SEVRICE_TIME - TimeConstants.MIN_SEVRICE_TIME;
int serviceTime = new Random().nextInt(maxRandom)+1+TimeConstants.MIN_SEVRICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
//long endTime = System.currentTimeMillis()-startTime;//服务时间
System.out.println(windowName+"为"+number+"号"+type+"客户服务,用时"+serviceTime/1000+"秒");
}
else{
System.out.println(windowName+"没有快速客户,去为普通客户服务");
commonService();
}
}
private void vipService() {
String windowName = "第"+WindowId+"号"+type+"窗口";
Integer number = NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println("第"+WindowId+"号"+type+"窗口获取VIP服务");
if(number!=null)
{
System.out.println(windowName + "开始为第" + number + "号VIP客户服务");
//long startTime = System.currentTimeMillis();
int maxRandom =TimeConstants.MAX_SEVRICE_TIME - TimeConstants.MIN_SEVRICE_TIME;
int serviceTime = new Random().nextInt(maxRandom)+1+TimeConstants.MIN_SEVRICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
//long endTime = System.currentTimeMillis()-startTime;//服务时间
System.out.println(windowName+"为"+number+"号"+type+"客户服务,用时"+serviceTime/1000+"秒");
}
else{
System.out.println(windowName+"没有VIP客户,去为普通客户服务");
commonService();
}
}
}
main代码
package bankQueue;
//⑥主函数,测试
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MainTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建4个普通窗口,一个快速窗口,一个VIP窗口
for(int i =1;i<5;i++){
SevriceWindow commonWindow = new SevriceWindow();
commonWindow.setWindowId(i);
commonWindow.start();
}
SevriceWindow expressWindow = new SevriceWindow();//new一个SevriceWindow对象
expressWindow.setWindowId(5);
expressWindow.setType(CustomerType.EXPRESS);//通过new的对象设置快速客户的类型
expressWindow.start();//运行快速客户类型的服务方法
SevriceWindow vipWindow = new SevriceWindow();//new一个SevriceWindow对象
vipWindow.setWindowId(6);
vipWindow.setType(CustomerType.VIP);//通过new的对象设置快速客户的类型
vipWindow.start();//运行快速客户类型的服务方法
//定时来普通客户
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number = NumberMachine.getInstance().getCommonManager().generteNewNumber();
System.out.println(number+"号普通客户等待服务!");
}
},
0,
TimeConstants.COMMON_COME_TIME,
TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number = NumberMachine.getInstance().getExpressManager().generteNewNumber();
System.out.println(number+"号快速客户等待服务!");
}
},
0,
TimeConstants.COMMON_COME_TIME*3,
TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number = NumberMachine.getInstance().getVipManager().generteNewNumber();
System.out.println(number+"号VIP客户等待服务!");
}
},
0,
TimeConstants.COMMON_COME_TIME*6,
TimeUnit.SECONDS);
}
}
-------------------------- android培训、java培训、期待与您交流! --------------------------