银行业务调度系统
/**视频地址http://edu.csdn.net/java/video.shtml**/
模拟实现银行业务调度系统逻辑,具体需求如下:
- 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
- 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
- 异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
- 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
- 各类型客户在其对应窗口按顺序依次办理业务。
- 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
- 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
- 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
1 package com.isoftstone.interview.bank; 2 3 import java.util.Random; 4 import java.util.concurrent.Executors; 5 import java.util.logging.Logger; 6 7 /** 8 * 没有把VIP窗口和快速窗口做成子类,是因为实际业务中的普通窗口可以随时被设置为VIP窗口和快速窗口。 9 * */ 10 public class ServiceWindow { 11 private static Logger logger = Logger.getLogger("Bank"); 12 private CustomerType type = CustomerType.COMMON; 13 private int number = 1; 14 15 public CustomerType getType() { 16 return type; 17 } 18 19 public void setType(CustomerType type) { 20 this.type = type; 21 } 22 23 public void setNumber(int number){ 24 this.number = number; 25 } 26 27 public void start(){ 28 Executors.newSingleThreadExecutor().execute( 29 new Runnable(){ 30 public void run(){ 31 //下面这种写法的运行效率低,最好是把while放在case下面 32 while(true){ 33 switch(type){ 34 case COMMON: 35 commonService(); 36 break; 37 case EXPRESS: 38 expressService(); 39 break; 40 case VIP: 41 vipService(); 42 break; 43 } 44 } 45 } 46 } 47 ); 48 } 49 50 private void commonService(){ 51 String windowName = "第" + number + "号" + type + "窗口"; 52 System.out.println(windowName + "开始获取普通任务!"); 53 Integer serviceNumber = NumberMachine.getInstance().getCommonManager().fetchNumber(); 54 if(serviceNumber != null ){ 55 System.out.println(windowName + "开始为第" + serviceNumber + "号普通客户服务"); 56 int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME; 57 int serviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME; 58 59 try { 60 Thread.sleep(serviceTime); 61 } catch (InterruptedException e) { 62 e.printStackTrace(); 63 } 64 System.out.println(windowName + "完成为第" + serviceNumber + "号普通客户服务,总共耗时" + serviceTime/1000 + "秒"); 65 }else{ 66 System.out.println(windowName + "没有取到普通任务,正在空闲一秒"); 67 try { 68 Thread.sleep(1000); 69 } catch (InterruptedException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 75 private void expressService(){ 76 Integer serviceNumber = NumberMachine.getInstance().getExpressManager().fetchNumber(); 77 String windowName = "第" + number + "号" + type + "窗口"; 78 System.out.println(windowName + "开始获取快速任务!"); 79 if(serviceNumber !=null){ 80 System.out.println(windowName + "开始为第" + serviceNumber + "号快速客户服务"); 81 int serviceTime = Constants.MIN_SERVICE_TIME; 82 try { 83 Thread.sleep(serviceTime); 84 } catch (InterruptedException e) { 85 e.printStackTrace(); 86 } 87 System.out.println(windowName + "完成为第" + serviceNumber + "号快速客户服务,总共耗时" + serviceTime/1000 + "秒"); 88 }else{ 89 System.out.println(windowName + "没有取到快速任务!"); 90 commonService(); 91 } 92 } 93 94 private void vipService(){ 95 96 Integer serviceNumber = NumberMachine.getInstance().getVipManager().fetchNumber(); 97 String windowName = "第" + number + "号" + type + "窗口"; 98 System.out.println(windowName + "开始获取VIP任务!"); 99 if(serviceNumber !=null){ 100 System.out.println(windowName + "开始为第" + serviceNumber + "号VIP客户服务"); 101 int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME; 102 int serviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME; 103 try { 104 Thread.sleep(serviceTime); 105 } catch (InterruptedException e) { 106 e.printStackTrace(); 107 } 108 System.out.println(windowName + "完成为第" + serviceNumber + "号VIP客户服务,总共耗时" + serviceTime/1000 + "秒"); 109 }else{ 110 System.out.println(windowName + "没有取到VIP任务!"); 111 commonService(); 112 } 113 } 114 }
1 package com.isoftstone.interview.bank; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class NumberManager { 7 private int lastNumber = 0; 8 private List queueNumbers = new ArrayList(); 9 10 public synchronized Integer generateNewNumber(){ 11 queueNumbers.add(++lastNumber); 12 return lastNumber; 13 } 14 15 public synchronized Integer fetchNumber(){ 16 if(queueNumbers.size()>0){ 17 return (Integer)queueNumbers.remove(0); 18 }else{ 19 return null; 20 } 21 } 22 }
1 package com.isoftstone.interview.bank; 2 3 public class Constants { 4 public static int MAX_SERVICE_TIME = 10000; //10秒! 5 public static int MIN_SERVICE_TIME = 1000; //1秒! 6 7 /*每个普通窗口服务一个客户的平均时间为5秒,一共有4个这样的窗口,也就是说银行的所有普通窗口合起来 8 * 平均1.25秒内可以服务完一个普通客户,再加上快速窗口和VIP窗口也可以服务普通客户,所以, 9 * 1秒钟产生一个普通客户比较合理,*/ 10 public static int COMMON_CUSTOMER_INTERVAL_TIME = 1; 11 }
1 package com.isoftstone.interview.bank; 2 3 public class NumberMachine { 4 5 private NumberMachine(){} 6 private static NumberMachine instance = new NumberMachine(); 7 public static NumberMachine getInstance(){ 8 return instance; 9 } 10 11 private NumberManager commonManager = new NumberManager(); 12 private NumberManager expressManager = new NumberManager(); 13 private NumberManager vipManager = new NumberManager(); 14 public NumberManager getCommonManager() { 15 return commonManager; 16 } 17 public NumberManager getExpressManager() { 18 return expressManager; 19 } 20 public NumberManager getVipManager() { 21 return vipManager; 22 } 23 24 }
1 package com.isoftstone.interview.bank; 2 3 public enum CustomerType { 4 COMMON,EXPRESS,VIP; 5 6 /** 7 * 用中文显示type的内容 8 */ 9 public String toString(){ 10 switch(this){ 11 case COMMON: 12 return "普通"; 13 case EXPRESS: 14 return "快速"; 15 case VIP: 16 return name(); 17 } 18 return null; 19 } 20 }
1 package com.isoftstone.interview.bank; 2 3 import java.util.concurrent.Executors; 4 import java.util.concurrent.TimeUnit; 5 import java.util.logging.Logger; 6 7 public class MainClass { 8 9 private static Logger logger = Logger.getLogger("cn.itcast.bankqueue"); 10 11 12 public static void main(String[] args) { 13 //产生4个普通窗口 14 for(int i=1;i<5;i++){ 15 ServiceWindow window = new ServiceWindow(); 16 window.setNumber(i); 17 window.start(); 18 } 19 20 //产生1个快速窗口 21 ServiceWindow expressWindow = new ServiceWindow(); 22 expressWindow.setType(CustomerType.EXPRESS); 23 expressWindow.start(); 24 25 //产生1个VIP窗口 26 ServiceWindow vipWindow = new ServiceWindow(); 27 vipWindow.setType(CustomerType.VIP); 28 vipWindow.start(); 29 30 //普通客户拿号 31 Executors.newScheduledThreadPool(1).scheduleAtFixedRate( 32 new Runnable(){ 33 public void run(){ 34 Integer serviceNumber = NumberMachine.getInstance().getCommonManager().generateNewNumber(); 35 /** 36 * 采用logger方式,无法看到直观的运行效果,因为logger.log方法内部并不是直接把内容打印出出来, 37 * 而是交给内部的一个线程去处理,所以,打印出来的结果在时间顺序上看起来很混乱。 38 */ 39 //logger.info("第" + serviceNumber + "号普通客户正在等待服务!"); 40 System.out.println("第" + serviceNumber + "号普通客户正在等待服务!"); 41 } 42 }, 43 0, 44 Constants.COMMON_CUSTOMER_INTERVAL_TIME, 45 TimeUnit.SECONDS); 46 47 //快速客户拿号 48 Executors.newScheduledThreadPool(1).scheduleAtFixedRate( 49 new Runnable(){ 50 public void run(){ 51 Integer serviceNumber = NumberMachine.getInstance().getExpressManager().generateNewNumber(); 52 System.out.println("第" + serviceNumber + "号快速客户正在等待服务!"); 53 } 54 }, 55 0, 56 Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2, 57 TimeUnit.SECONDS); 58 59 //VIP客户拿号 60 Executors.newScheduledThreadPool(1).scheduleAtFixedRate( 61 new Runnable(){ 62 public void run(){ 63 Integer serviceNumber = NumberMachine.getInstance().getVipManager().generateNewNumber(); 64 System.out.println("第" + serviceNumber + "号VIP客户正在等待服务!"); 65 } 66 }, 67 0, 68 Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, 69 TimeUnit.SECONDS); 70 } 71 72 }