标签:
一、银行业务调度系统需求
模拟实现银行业务调度系统逻辑,具体需求如下:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
二、面向对象的分析与设计:
1、有三种对应类型的客户:VIP客户,普通客户,快速客户 ,异步随机生成各种类型的客户,各类型客户在其对应窗口按顺序依次办理业务 。。
> 用类图来分析理解:
一个号码产生器NumberMachine 管理三类号码普通用户、VIP用户、快速用户。而NumberManager 类则用于产生的号码、和获取号码。而ServiceWindow 在NumberManager 获取号码。
三、编写:
NumberManager类
1 package com.itheima.www; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class NumberManager { 7 /*返回上一次返回的号码*/ 8 private int lastNumber = 1 ; 9 /*用于存储所有等待服务的客户号码队列*/ 10 private List<Integer> queueNumber = new ArrayList<Integer>() ; 11 /** 12 产生新号码的方法 13 @return 新产生的号码 14 */ 15 public synchronized Integer generateNewManager(){ 16 queueNumber.add(lastNumber) ; 17 return lastNumber++ ; 18 } 19 20 /** 21 获取马上要为之服务的号码的方法 22 @return 马上要为之服务的号码 23 */ 24 public synchronized Integer fetchServiceNumber() { 25 if (queueNumber.size() > 0) { 26 return queueNumber.remove(0) ; 27 } 28 else { 29 return null ; 30 } 31 } 32 33 }
NumberMachine类
1 package com.itheima.www; 2 3 public class NumberMachine { 4 /*表示普通客户的号码管理器*/ 5 private NumberManager commonManager = new NumberManager() ; 6 /*表示快速客户的号码管理器*/ 7 private NumberManager expressManager = new NumberManager() ; 8 /*表示VIP客户的号码管理器*/ 9 private NumberManager vipManager = new NumberManager() ; 10 11 private NumberMachine() {} 12 13 private static NumberMachine numberMachine = null ; 14 public static NumberMachine getInstance() { 15 if(numberMachine == null) { 16 numberMachine = new NumberMachine() ; 17 } 18 return numberMachine ; 19 } 20 21 public NumberManager getCommonManager() { 22 return commonManager; 23 } 24 public NumberManager getExpressManager() { 25 return expressManager; 26 } 27 public NumberManager getVipManager() { 28 return vipManager; 29 } 30 }
1 package com.itheima.www; 2 3 public enum CustomerType { 4 COMMON,EXPRESS,VIP ; 5 6 public String toString() { 7 switch(this) { 8 case COMMON: 9 return "普通" ; 10 case EXPRESS: 11 return "快速" ; 12 default: 13 return "VIP" ; 14 } 15 } 16 }
1 package com.itheima.www; 2 3 import java.util.Random; 4 import java.util.concurrent.Executors; 5 6 public class ServiceWindow { 7 /*客户的类型*/ 8 private CustomerType type = null ; 9 private int number = 1 ; 10 11 public CustomerType getType() { 12 return type; 13 } 14 15 public void setType(CustomerType type) { 16 this.type = type; 17 } 18 19 public void setNumber(int number) { 20 this.number = number; 21 } 22 23 public void start() { 24 Executors.newSingleThreadExecutor().execute(new Runnable(){ 25 public void run() { 26 switch(type){ 27 case COMMON : 28 while(true){ 29 commonService() ; 30 } 31 case EXPRESS : 32 while(true){ 33 expressService() ; 34 } 35 case VIP: 36 while(true){ 37 vipService() ; 38 } 39 } 40 } 41 }) ; 42 } 43 44 private void commonService(){ 45 /*获取要服务的号码*/ 46 Integer serviceNumber = NumberMachine.getInstance().getCommonManager().fetchServiceNumber() ; 47 String windowName = type + "窗口_第" + number + "号" ; 48 System.out.println(windowName + ":开始获取-普通任务!"); 49 if( serviceNumber != null) { 50 System.out.println(windowName + ":开始为第"+serviceNumber+"号-普通用户-开始服务"); 51 /*普通用户的服务时间在最大值和最小值之间*/ 52 int amongTime = Custants.MAX_SERVICE_TIME - Custants.MIN_SERVICE_TIME ; 53 int serivceTime = new Random().nextInt(amongTime) + 1 + Custants.MIN_SERVICE_TIME ; 54 try { 55 Thread.sleep(serivceTime) ; 56 } catch (InterruptedException e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 } 60 System.out.println(windowName +":完成为第"+serviceNumber+"号-普通客户服务,总耗时" 61 + serivceTime/1000 +"秒"); 62 } 63 else { 64 System.out.println(windowName +":没有取到-普通任务!休息一秒!"); 65 try { 66 Thread.sleep(1000) ; 67 } catch (InterruptedException e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } 71 } 72 } 73 private void expressService() { 74 /*获取要服务的号码*/ 75 Integer serviceNumber = NumberMachine.getInstance().getExpressManager().fetchServiceNumber() ; 76 String windowName = type + "窗口_第" + number + "号" ; 77 System.out.println(windowName + ":开始获取-快速任务!"); 78 if( serviceNumber != null) { 79 System.out.println(windowName + ":开始为第"+serviceNumber+"号-快速用户-开始服务"); 80 /*快速用户的服务时间在最大值和最小值之间*/ 81 int serivceTime = Custants.MIN_SERVICE_TIME ; 82 try { 83 Thread.sleep(serivceTime) ; 84 } catch (InterruptedException e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } 88 System.out.println(windowName +":完成为第"+serviceNumber+"号-快速客户服务,总耗时" 89 + serivceTime/1000 +"秒"); 90 } 91 else { 92 System.out.println(windowName +":没有取到-快速任务!"); 93 commonService() ; 94 } 95 } 96 private void vipService(){ 97 /*获取要服务的号码*/ 98 Integer serviceNumber = NumberMachine.getInstance().getVipManager().fetchServiceNumber() ; 99 String windowName = type + "窗口_第" + number + "号" ; 100 System.out.println(windowName + ":开始获取-普通任务!"); 101 if( serviceNumber != null) { 102 System.out.println(windowName + ":开始为第"+serviceNumber+"号-VIP用户-开始服务"); 103 /*VIP用户的服务时间在最大值和最小值之间*/ 104 int amongTime = Custants.MAX_SERVICE_TIME - Custants.MIN_SERVICE_TIME ; 105 int serivceTime = new Random().nextInt(amongTime) + 1 + Custants.MIN_SERVICE_TIME ; 106 try { 107 Thread.sleep(serivceTime) ; 108 } catch (InterruptedException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 } 112 System.out.println(windowName +":完成为第"+serviceNumber+"号-VIP客户服务,总耗时" 113 + serivceTime/1000 +"秒"); 114 } 115 else { 116 System.out.println(windowName +":没有取到-VIP任务!"); 117 commonService() ; 118 } 119 } 120 }
MainClass类
1 package com.itheima.www; 2 3 import java.util.concurrent.Executors; 4 import java.util.concurrent.TimeUnit; 5 6 public class MainClass { 7 public static void main(String[] args) { 8 //创建4个普通窗口 9 for(int i = 1 ; i < 5 ; ++i ){ 10 ServiceWindow window = new ServiceWindow() ; 11 window.setNumber(i) ; 12 window.setType(CustomerType.COMMON) ; 13 window.start() ; 14 } 15 //创建1个快速窗口 16 ServiceWindow expressWindow = new ServiceWindow() ; 17 expressWindow.setType(CustomerType.EXPRESS); 18 expressWindow.start() ; 19 20 //穿件一个VIP窗口 21 ServiceWindow vipWindow = new ServiceWindow() ; 22 vipWindow.setType(CustomerType.VIP); 23 vipWindow.start() ; 24 25 //分别定时创建的普通客户端号码 26 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){ 27 public void run() { 28 Integer serviceNumber = NumberMachine.getInstance().getCommonManager().generateNewManager() ; 29 System.out.println("客户:第" + serviceNumber + "号-普通客户-正在等待服务!"); 30 } 31 } 32 , 0 33 , Custants.COMMON_CUSTOMER_INTERVAL_TIME 34 , TimeUnit.SECONDS); 35 //分别定时创建的快速客户端号码 36 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){ 37 public void run() { 38 Integer serviceNumber = NumberMachine.getInstance().getExpressManager().generateNewManager() ; 39 System.out.println("客户:第" + serviceNumber + "号-快速客户-正在等待服务!"); 40 } 41 } 42 , 0 43 , Custants.COMMON_CUSTOMER_INTERVAL_TIME * 3 44 , TimeUnit.SECONDS); 45 //分别定时创建的VIP客户端号码 46 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){ 47 public void run() { 48 Integer serviceNumber = NumberMachine.getInstance().getVipManager().generateNewManager() ; 49 System.out.println("客户:第" + serviceNumber + "号-VIP客户-正在等待服务!"); 50 } 51 } 52 , 0 53 , Custants.COMMON_CUSTOMER_INTERVAL_TIME * 6 54 , TimeUnit.SECONDS); 55 } 56 }
1 package com.itheima.www; 2 3 public class Custants { 4 public static int MIN_SERVICE_TIME = 1000 ; 5 public static int MAX_SERVICE_TIME = 10000 ; 6 7 public static int COMMON_CUSTOMER_INTERVAL_TIME = 1 ; 8 }
总结:在写做这种模拟型项目只有对业务流程足够了解才能完成编写,也只有在对业务流程足够了解,才能在面向对象设计和分析的时候更准确的定义出项目中的各种对象,以及对象拥有的方法、对象与对象之间的联系。加油!
系转载
标签:
原文地址:http://www.cnblogs.com/jym-sunshine/p/4689446.html