模拟实现银行业务调度系统逻辑,具体需求如下:
银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
1.有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)
2.异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
3.客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
4.各类型客户在其对应窗口按顺序依次办理业务。
5.当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
6.随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
7.不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
解题:
一、面向对象分析
有三种对应类型的客户:VIP客户,普通客户,快速客户 ,异步随机生成各种类型的客户,各类型客
在其对应窗按顺序依次办理业务。
1.首先,经常在银行办理业务的人更有利于理解本系统,例如,我经常陪老婆跑银行,对银行的这
业务算是比较熟悉了,我知道每一个客户其实就是由银行的一个取号机器产生号码的方式来表
的。所以,我想到要有一个号码管理器对象,让这个对象不断地产生号码,就等于随机生成了
户。
2.由于有三类客户,每类客户的号码编排都是完全独立的,所以,我想到本系统一共要产生三个号
管理器对象,各自管理一类用户的排队号码。这三个号码管理器对象统一由一个号码机器进行
理,这个号码机器在整个系统中始终只能有一个,所以,它要被设计成单例。
各类型客户在其对应窗口按顺序依次办理业务 ,准确地说,应该是窗口依次叫号。
1.各个窗口怎么知道该叫哪一个号了呢?它一定是问的相应的号码管理器,即服务窗口每次找号码
理器获取当前要被服务的号码。
二、NumberMachine类的编写:package cn.itcast.bank; import java.util.ArrayList; import java.util.List; public class NumberManager { private int lastNumber = 1; private List<Integer> queueNumber = new ArrayList<Integer>(); public synchronized Integer generateNewManager() { queueNumber.add(lastNumber); return lastNumber++; }
public synchronized Integer fetchServiceNumber() { Integer number = null; if (queueNumber.size() > 0) { number = queueNumber.remove(0); } return number; } }
package cn.itcast.bank; 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; } }
四、ServiceWindow类的编写:package cn.itcast.bank; public enum CustomerType { COMMON, EXPRESS, VIP; public String toString() { switch (this) { case COMMON: return "普通"; case EXPRESS: return "快速"; case VIP: return name(); } return null; } }
五、Constants类的编写:package cn.itcast.bank; 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() { while (true) { switch (type) { case COMMON: commonService(); break; case EXPRESS: expressService(); break; case VIP: vipService(); break; } } } public void commonService() { String windowName = "第" + windowId + "号" + type + "窗口"; Integer number = NumberMachine.getInstance().getCommonManager() .fetchServiceNumber(); System.out.println(windowName + "正在获取任务!"); if (number != null) { System.out.println(windowName + "正在为第" + number + "个普通客户服务!"); long beginTime = System.currentTimeMillis(); int maxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME; long serveTime = new Random().nextInt(maxRand) + 1 + Constants.MIN_SERVICE_TIME; try { Thread.sleep(serveTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } long costTime = System.currentTimeMillis() - beginTime; System.out.println(windowName + "为第" + number + "个普通客户服务完毕!耗时:" + (costTime / 1000) + "秒"); } else { System.out.println(windowName + "没有取到任务,先休息1s钟!"); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void expressService() { String windowName = "第" + windowId + "个" + type + "窗口"; Integer number = NumberMachine.getInstance() .getExpressManager().fetchServiceNumber(); System.out.println(windowName + "正在获取快速任务..."); if (number != null) { System.out.println(windowName + "正在为第" + number + "个" + type + "客户服务..."); long beginTime = System.currentTimeMillis(); long serveTime = Constants.MIN_SERVICE_TIME; try { Thread.sleep(serveTime); } catch (InterruptedException e) { e.printStackTrace(); } long costTime = System.currentTimeMillis() - beginTime; System.out.println(windowName + "为第" + number + "个" + type + "客户服务完毕!耗时:" + (costTime / 1000) + "秒"); } else { System.out.println(windowName + "没有取到快速任务,尝试取普通任务!"); commonService(); } } public void vipService() { String windowName = "第" + windowId + "个" + type + "窗口"; Integer number = NumberMachine.getInstance().getVipManager() .fetchServiceNumber(); System.out.println(windowName + "正在获取vip任务"); if (number != null) { System.out.println(windowName + "正在为第" + number + "个" + type + "客户服务。。。"); long beginTime = System.currentTimeMillis(); int MaxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME; long serveTime = new Random().nextInt(MaxRand) + 1 + Constants.MIN_SERVICE_TIME; try { Thread.sleep(serveTime); } catch (InterruptedException e) { e.printStackTrace(); } long costTime = System.currentTimeMillis() - beginTime; System.out.println(windowName + "为第" + number + "个" + type + "客户服务完毕!耗时:" + (costTime / 1000) + "秒"); }else{ System.out.println(windowName + "没有取到Vip任务,尝试获取普通任务"); commonService(); } } }); } }
六、MainClass类的编写:package cn.itcast.bank; public class Constants { public static int MAX_SERVICE_TIME = 10000; public static int MIN_SERVICE_TIME = 1000; public static int COMMON_CUSTOMER_INTERVAL_TIME = 1; }
package cn.itcast.bank; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class MainClass { public static void main(String[] args) { for(int i = 1; i <= 5; i++){ ServiceWindow commonServiceWindow = new ServiceWindow(); commonServiceWindow.setWindowId(i); commonServiceWindow.start(); } ServiceWindow expressServiceWindow = new ServiceWindow(); expressServiceWindow.setType(CustomerType.EXPRESS); expressServiceWindow.start(); ServiceWindow vipServiceWindow = new ServiceWindow(); vipServiceWindow.setType(CustomerType.VIP); vipServiceWindow.start(); Executors.newScheduledThreadPool(1).scheduleAtFixedRate( new Runnable() { @Override public void run() { Integer number = NumberMachine.getInstance().getCommonManager().generateNewManager(); System.out.println(number + "号普通客户等到服务"); } }, 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 + "号快速客户等待服务"); } }, 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客户正在等待服务"); } }, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, TimeUnit.SECONDS ); } }