银行调度系统-张孝祥

来源:互联网 发布:python exec函数 编辑:程序博客网 时间:2024/06/05 16:08

银行服务:首先客户到银行取号排队,然后在到自己时,到对应的服务窗口,办理业务。

根据上面可得到:一个号码机器(单例)(管理三个号码管理器(号码管理器提供号码生成以及号码减少功能

(现实生活中客户新增以及办理完业务的客户)))对应三种客户类型:common、express、Vip。(客户类型 枚举)

服务窗口:服务窗口有:窗口类型、窗口id;服务窗口提供办理业务的方法。


号码机器类

package cn.duduyu.bank;public class NumberMachine {private NumberManager commonManager=new NumberManager();private NumberManager vipManager=new NumberManager();private NumberManager expressManager=new NumberManager();private NumberMachine(){}public NumberManager getCommonManager() {return commonManager;}public NumberManager getVipManager() {return vipManager;}public NumberManager getExpressManager() {return expressManager;}public static NumberMachine getInstance(){return instance;}private static NumberMachine instance=new NumberMachine();}


号码管理器类
package cn.duduyu.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 createNewNumber(){queueNumber.add(lastNumber);return lastNumber++;}/* * 返回值为Integer,为了防止当没有娶到号时,转为int报空指针异常 */public synchronized Integer fetchNumber(){Integer number=null;if(queueNumber.size()>0){number=queueNumber.remove(0);}return number;}}

客户类型类

package cn.duduyu.bank;public enum CustomerType {COMMON,EXPRESS,VIP;public String toString(){switch(this){case COMMON:return "普通";case EXPRESS:return "快速";case VIP:return name();}return null;}}

服务窗口类

package cn.duduyu.bank;import java.util.Random;import java.util.concurrent.Executors;/** * 服务窗口 * @author DuanTing * @description <br> * @time 2014-10-8  下午01:43:18 * @type cn.duduyu.bank.ServiceWindow.java */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() {@Overridepublic void run() {while(true){switch(type){case COMMON:commonService();break;case EXPRESS:expressService();break;case VIP:vipService();break;}}}});}public void expressService(){String windowName="第"+windowId+"号"+type+"窗口";Integer number=NumberMachine.getInstance().getExpressManager().fetchNumber();System.out.println(windowName+"开始获取"+type+"任务");if(number!=null){System.out.println(windowName+"正在为第"+number+"号快速客户服务");long startTime=System.currentTimeMillis();/*int serviceRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;long serviceTime=new Random().nextInt(serviceRandom)+1+Constants.MIN_SERVICE_TIME;*/try {Thread.sleep(Constants.MIN_SERVICE_TIME);long costTime=System.currentTimeMillis()-startTime;System.out.println(windowName+"为"+number+"号"+type+"客户服务了"+costTime/1000+"秒时间");} catch (InterruptedException e) {e.printStackTrace();}}else{System.out.println(windowName+"没有获取到"+type+"任务");commonService();}}public void vipService(){String windowName="第"+windowId+"号"+type+"窗口";Integer number=NumberMachine.getInstance().getVipManager().fetchNumber();System.out.println(windowName+"开始获取"+type+"任务");if(number!=null){System.out.println(windowName+"正在为第"+number+"号VIP客户服务");long startTime=System.currentTimeMillis();int serviceRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;long serviceTime=new Random().nextInt(serviceRandom)+1+Constants.MIN_SERVICE_TIME;try {Thread.sleep(serviceTime);long costTime=System.currentTimeMillis()-startTime;System.out.println(windowName+"为"+number+"号"+type+"客户服务了"+costTime/1000+"秒时间");} catch (InterruptedException e) {e.printStackTrace();}}else{System.out.println(windowName+"没有获取到"+type+"任务");commonService();}}public void commonService() {String windowName="第"+windowId+"号"+type+"窗口";Integer number=NumberMachine.getInstance().getCommonManager().fetchNumber();System.out.println(windowName+"开始获取"+"普通任务");if(number!=null){System.out.println(windowName+"正在为第"+number+"号普通客户服务");long startTime=System.currentTimeMillis();int serviceRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;long serviceTime=new Random().nextInt(serviceRandom)+1+Constants.MIN_SERVICE_TIME;try {Thread.sleep(serviceTime);long costTime=System.currentTimeMillis()-startTime;System.out.println(windowName+"为"+number+"号"+"普通客户服务了"+costTime/1000+"秒时间");} catch (InterruptedException e) {e.printStackTrace();}}else{System.out.println(windowName+"没有获取到"+"普通任务,先休息1秒钟");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}

常量类

package cn.duduyu.bank;public class Constants {public static int MIN_SERVICE_TIME=1000;public static int MAX_SERVICE_TIME=10000;public static int COMMON_CUSTOMER_INTERVAL=1;public static int VIP_CUSTOMER_INTERVAL=6;public static int EXPRESS_CUSTOMER_INTERVAL=2;}


客户端调用

package cn.duduyu.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 commonWindow=new ServiceWindow();commonWindow.setWindowId(i);commonWindow.start();}ServiceWindow vipWindow=new ServiceWindow();vipWindow.setType(CustomerType.VIP);vipWindow.start();ServiceWindow expressWindow=new ServiceWindow();expressWindow.setType(CustomerType.EXPRESS);expressWindow.start();/* * 模拟各种客户来到银行取号排队等待服务(利用定时器) */Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){@Overridepublic void run() {//生成Common客户Integer number=NumberMachine.getInstance().getCommonManager().createNewNumber();System.out.println("第"+number+"号普通客户等待服务");}}, 0, Constants.COMMON_CUSTOMER_INTERVAL, TimeUnit.SECONDS);Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){@Overridepublic void run() {//生成Express客户Integer number=NumberMachine.getInstance().getExpressManager().createNewNumber();System.out.println("第"+number+"号快速客户等待服务");}}, 0, Constants.EXPRESS_CUSTOMER_INTERVAL, TimeUnit.SECONDS);Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){@Overridepublic void run() {//生成Vip客户Integer number=NumberMachine.getInstance().getVipManager().createNewNumber();System.out.println("第"+number+"号VIP客户等待服务");}}, 0, Constants.VIP_CUSTOMER_INTERVAL, TimeUnit.SECONDS);}}


0 0
原创粉丝点击