黑马--银行业务调度系统

来源:互联网 发布:ubuntu kde plasma 5 编辑:程序博客网 时间:2024/05/01 00:11

模拟实现银行业务调度系统逻辑,具体需求如下:

银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。

       有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。

       异步随机生成各种类型的客户,生成各类型用户的概率比例为:

       VIP客户:普通客户:快速客户  =  1 :6 :3。

       客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。

       各类型客户在其对应窗口按顺序依次办理业务。

       当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。

       随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。

       不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。

解题思路

       面向对象的分析和设计:

(一)有三种对应类型的客户:VIP客户,普通客户,快速客户。

异步随机生成各种类型的客户,各类型客户在其对应窗口按顺序依次办理业务。

1、首先,经常在银行办理业务的人更有利于理解本系统,例如,我跑过一次银行,对银行的这个业务算是比较熟悉了,我知道每一个客户其实就是由银行的一个取号机器产生号码的方式来表示的。所以,我想到要有一个号码管理器对象,让这个对象不断地产生号码,就等于随机生成了客户。

2、由于有三类客户,每类客户的号码编排都是完全独立的,所以,我想到本系统一共要产生三个号码管理器对象,各自管理一类用户的排队号码。这三个号码管理器对象统一由一个号码机器进行管理,这个号码机器在整个系统中始终只能有一个,所以,它要被设计成单例。

(二)各类型客户在其对应窗口按顺序依次办理业务 ,准确地说,应该是窗口依次叫号。

1、各个窗口怎么知道该叫哪一个号了呢?它一定是问的相应的号码管理器,即服务窗口每次找号码管理器获取当前要被服务的号码。

2、如果我不是多次亲身经历银行的这种业务,再加之积累了大量面向对象的应用开发经验,我也不知道能否轻松进行这种设计,能否发掘出其中隐含的对象信息,我真说不出具体的经验是什么,就是日积月累出来的一种感觉。

(三)类图:

       类的编码实现
(一)NumberManager类
1、定义一个用于存储上一个客户号码的成员变量和用于存储所有等待服务的客户号码的队列集合。
2、定义一个产生新号码的方法和获取马上要为之服务的号码的方法,这两个方法被不同的线程操作了相同的数据,所以,要进行同步。

源码:

package com.isoftstone.intervierw.bank;import java.util.ArrayList;import java.util.List;public class NumberManager {<span style="white-space:pre"></span>private int lastNumber;<span style="white-space:pre"></span>privateList<Integer> queueNumbers = new ArrayList<Integer>();<span style="white-space:pre"></span>public synchronized Integer generateNewNumber(){<span style="white-space:pre"></span>queueNumbers.add(++lastNumber);<span style="white-space:pre"></span>return lastNumber;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public synchronized Integer fetchNumber(){<span style="white-space:pre"></span>if(queueNumbers.size()>0){<span style="white-space:pre"></span>return queueNumbers.remove(0);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return null;<span style="white-space:pre"></span>}}


(二)NumberMachine类

1、定义三个成员变量分别指向三个NumberManager对象,分别表示普通、快速和VIP客户的号码管理器,定义三个对应的方法来返回这三个NumberManager对象。
2、将NumberMachine类设计成单例。

源码:

package com.isoftstone.intervierw.bank;public class NumberMachine {<span style="white-space:pre"></span>private NumberMachine(){}<span style="white-space:pre"></span>private static NumberMachine instance =null;<span style="white-space:pre"></span>public static NumberMachine getInstance(){<span style="white-space:pre"></span>if(instance==null){<span style="white-space:pre"></span>synchronized (NumberMachine.class) {<span style="white-space:pre"></span>if(instance==null){<span style="white-space:pre"></span>instance= new NumberMachine();<span style="white-space:pre"></span>return instance;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return instance;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private NumberManager commonManager = new NumberManager();<span style="white-space:pre"></span>private NumberManager expressManager = new NumberManager();<span style="white-space:pre"></span>private NumberManager vipManager = new NumberManager();<span style="white-space:pre"></span>public NumberManager getCommonManager() {<span style="white-space:pre"></span>return commonManager;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public NumberManager getExpressManager() {<span style="white-space:pre"></span>return expressManager;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public NumberManager getVipManager() {<span style="white-space:pre"></span>return vipManager;<span style="white-space:pre"></span>}}


(三)CustomerType枚举类
1、系统中有三种类型的客户,所以用定义一个枚举类,其中定义三个成员分别表示三种类型的客户。
2、重写toString方法,返回类型的中文名称。这是在后面编码时重构出来的。

源码:

package com.isoftstone.intervierw.bank;public enum CustomerType {<span style="white-space:pre"></span>COMMON,EXPRESS,VIP;<span style="white-space:pre"></span>public String toString(){<span style="white-space:pre"></span>switch(this){<span style="white-space:pre"></span>case COMMON:<span style="white-space:pre"></span>return "普通";<span style="white-space:pre"></span>case EXPRESS:<span style="white-space:pre"></span>return "快速";<span style="white-space:pre"></span>default:<span style="white-space:pre"></span>return name();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}


(四)ServiceWindow类

1、定义一个start方法,内部启动一个线程,根据服务窗口的类别分别循环调用三个不同的方法。

2、定义三个方法分别对三种客户进行服务,为了观察运行效果,应详细打印出其中的细节信息。

源码:

package com.isoftstone.intervierw.bank;import java.util.Random;import java.util.concurrent.Executors;public class ServiceWindow {<span style="white-space:pre"></span>private CustomerType type;<span style="white-space:pre"></span>private int windowId;<span style="white-space:pre"></span>public void setType(CustomerType type) {<span style="white-space:pre"></span>this.type = type;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setWindowId(int windowId) {<span style="white-space:pre"></span>this.windowId = windowId;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void start(){<span style="white-space:pre"></span>Executors.newSingleThreadExecutor().execute(new Runnable(){<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void run() {<span style="white-space:pre"></span>while(true){<span style="white-space:pre"></span>switch(type){<span style="white-space:pre"></span>case COMMON:<span style="white-space:pre"></span>commonService();<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case EXPRESS:<span style="white-space:pre"></span>expressService();<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>default:<span style="white-space:pre"></span>vipService();<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>});<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private void commonService() {<span style="white-space:pre"></span>Integer serviceNumber = NumberMachine.getInstance().getCommonManager().fetchNumber();<span style="white-space:pre"></span>String windowName ="第"+windowId+"号"+type+"窗口";<span style="white-space:pre"></span>System.out.println(windowName+"正在获取普通任务");<span style="white-space:pre"></span>if(serviceNumber!=null){<span style="white-space:pre"></span>System.out.println(windowName+"获取到普通任务");<span style="white-space:pre"></span>long startServiceTime =System.currentTimeMillis();<span style="white-space:pre"></span>System.out.println("请"+serviceNumber+"号"+type+"客户到"+windowId+"号"+type+"窗口");<span style="white-space:pre"></span>System.out.println(windowName+"正在为"+serviceNumber+"号普通客户服务");<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>Thread.sleep(new Random().nextInt(Constant.MAX_SERVICE_TIME-Constant.MIN_SERVICE_TIME)+Constant.MIN_SERVICE_TIME);<span style="white-space:pre"></span>} catch (InterruptedException e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>long serviceTime =System.currentTimeMillis()-startServiceTime;<span style="white-space:pre"></span>System.out.println(windowName+"为第"+serviceNumber+"个普通客户服务,耗时"+serviceTime/1000+"秒");<span style="white-space:pre"></span>}else{<span style="white-space:pre"></span>System.out.println(windowName+"没有获取到普通任务,休息1秒钟");<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>Thread.sleep(1000);<span style="white-space:pre"></span>} catch (InterruptedException e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private void expressService() {<span style="white-space:pre"></span>Integer serviceNumber = NumberMachine.getInstance().getExpressManager().fetchNumber();<span style="white-space:pre"></span>String windowName ="第"+windowId+"号"+type+"窗口";<span style="white-space:pre"></span>System.out.println(windowName+"正在获取"+type+"任务");<span style="white-space:pre"></span>if(serviceNumber!=null){<span style="white-space:pre"></span>System.out.println(windowName+"获取到"+type+"任务");<span style="white-space:pre"></span>long startServiceTime =System.currentTimeMillis();<span style="white-space:pre"></span>System.out.println("请"+serviceNumber+"号"+type+"客户到"+windowId+"号"+type+"窗口");<span style="white-space:pre"></span>System.out.println(windowName+"正在为"+serviceNumber+"号"+type+"客户服务");<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>Thread.sleep(new Random().nextInt(Constant.MAX_SERVICE_TIME-Constant.MIN_SERVICE_TIME)+Constant.MIN_SERVICE_TIME);<span style="white-space:pre"></span><span style="white-space:pre"></span>} catch (InterruptedException e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>long serviceTime =System.currentTimeMillis()-startServiceTime;<span style="white-space:pre"></span>System.out.println(windowName+"为第"+serviceNumber+"个"+type+"客户服务,耗时"+serviceTime/1000+"秒");<span style="white-space:pre"></span>}else{<span style="white-space:pre"></span>System.out.println(windowName+"没有获取到"+type+"任务,为普通窗口提供服务");<span style="white-space:pre"></span>commonService();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private void vipService() {<span style="white-space:pre"></span>Integer serviceNumber = NumberMachine.getInstance().getVipManager().fetchNumber();<span style="white-space:pre"></span>String windowName ="第"+windowId+"号"+type+"窗口";<span style="white-space:pre"></span>System.out.println(windowName+"正在获取"+type+"任务");<span style="white-space:pre"></span>if(serviceNumber!=null){<span style="white-space:pre"></span>System.out.println(windowName+"获取到"+type+"任务");<span style="white-space:pre"></span>long startServiceTime =System.currentTimeMillis();<span style="white-space:pre"></span>System.out.println("请"+serviceNumber+"号"+type+"客户到"+windowId+"号"+type+"窗口");<span style="white-space:pre"></span>System.out.println(windowName+"正在为"+serviceNumber+"号"+type+"客户服务");<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>Thread.sleep(new Random().nextInt(Constant.MAX_SERVICE_TIME-<span style="white-space:pre"></span>Constant.MIN_SERVICE_TIME)+Constant.MIN_SERVICE_TIME);<span style="white-space:pre"></span>} catch (InterruptedException e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>long serviceTime = System.currentTimeMillis()-startServiceTime;<span style="white-space:pre"></span>System.out.println(windowName+"为第"+serviceNumber+"个"+type+"客户服务,耗时"+serviceTime/1000+"秒");<span style="white-space:pre"></span>}else{<span style="white-space:pre"></span>System.out.println(windowName+"没有获取到"+type+"任务,为普通窗口提供服务");<span style="white-space:pre"></span>commonService();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}


(五)Constants类

1、定义三个常量:MAX_SERVICE_TIME、MIN_SERVICE_TIME、COMMON_CUSTOMER_INTERVAL_TIME。

源码:

package com.isoftstone.intervierw.bank;public class Constant {<span style="white-space:pre"></span>public static int MAX_SERVICE_TIME = 10000;<span style="white-space:pre"></span>public static int MIN_SERVICE_TIME = MAX_SERVICE_TIME / 10;<span style="white-space:pre"></span>public static int MIN_TIME = 1;}


(六)MainClass类
  1、用for循环创建出4个普通窗口,再创建出1个快速窗口和一个VIP窗口。

  2、接着再创建三个定时器,分别定时去创建新的普通客户号码、新的快速客户号码、新的VIP客户号码。

源码:

package com.isoftstone.intervierw.bank;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;public class MainClass {<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>final ServiceWindow vipServiceWindow = new ServiceWindow();<span style="white-space:pre"></span>vipServiceWindow.setWindowId(6);<span style="white-space:pre"></span>vipServiceWindow.setType(CustomerType.VIP);<span style="white-space:pre"></span>vipServiceWindow.start();<span style="white-space:pre"></span>final ServiceWindow expressServiceWindow = new ServiceWindow();<span style="white-space:pre"></span>expressServiceWindow.setWindowId(5);<span style="white-space:pre"></span>expressServiceWindow.setType(CustomerType.EXPRESS);<span style="white-space:pre"></span>expressServiceWindow.start();<span style="white-space:pre"></span>for(int i=1;i<=4;i++){<span style="white-space:pre"></span>final ServiceWindow commonServiceWindow = new ServiceWindow();<span style="white-space:pre"></span>commonServiceWindow.setWindowId(i);<span style="white-space:pre"></span>commonServiceWindow.setType(CustomerType.COMMON);<span style="white-space:pre"></span>commonServiceWindow.start();<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>Executors.newScheduledThreadPool(1).scheduleAtFixedRate(<span style="white-space:pre"></span>new Runnable(){<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void run() {<span style="white-space:pre"></span>Integer customerNumber= NumberMachine.getInstance().getCommonManager().generateNewNumber();<span style="white-space:pre"></span>System.out.println(customerNumber+"号"+CustomerType.COMMON+"客户正在等待服务");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}, <span style="white-space:pre"></span>0,<span style="white-space:pre"></span>Constant.MIN_TIME,<span style="white-space:pre"></span>TimeUnit.SECONDS);<span style="white-space:pre"></span>Executors.newScheduledThreadPool(1).scheduleAtFixedRate(<span style="white-space:pre"></span>new Runnable(){<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void run() {<span style="white-space:pre"></span>Integer customerNumber = NumberMachine.getInstance().getExpressManager().generateNewNumber();<span style="white-space:pre"></span>System.out.println(customerNumber+"号"+CustomerType.EXPRESS+"客户正在等待服务");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}, <span style="white-space:pre"></span>0,<span style="white-space:pre"></span>Constant.MIN_TIME * 2,<span style="white-space:pre"></span>TimeUnit.SECONDS);<span style="white-space:pre"></span>Executors.newScheduledThreadPool(1).scheduleAtFixedRate(<span style="white-space:pre"></span>new Runnable(){<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void run() {<span style="white-space:pre"></span>Integer customerNumber = NumberMachine.getInstance().getVipManager().generateNewNumber();<span style="white-space:pre"></span>System.out.println(customerNumber+"号"+CustomerType.VIP+"客户正在等待服务");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}, <span style="white-space:pre"></span>0,<span style="white-space:pre"></span>Constant.MIN_TIME * 6,<span style="white-space:pre"></span>TimeUnit.SECONDS);<span style="white-space:pre"></span>}}


0 0
原创粉丝点击