黑马程序员----银行业务调度系统

------- android培训、java培训、期待与您交流! ----------

 

 

>>需求

 

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

 

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

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

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

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

 

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

 

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

 

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

 

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

 

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

 

 

 

 

>>面向对象的分析和设计

 

思想:

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

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

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

 

 

 

 

 

>> NumberManager和NumberMachine类

 

NumberManager类

定义一个用于存储上一个客户号码的成员变量和用于存储所有等待服务的客户号码的队列集合。

定义一个产生新号码的方法和获取马上要为之服务的号码的方法,这两个方法被不同的线程操作了相同的数据,所以,要进行同步。

 

NumberMachine类

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

将NumberMachine类设计成单例。

 

 

 

 

>> ServiceWindow与CustomerType枚举类

 

CustomerType枚举类

系统中有三种类型的客户,所以用定义一个枚举类,其中定义三个成员分别表示三种类型的客户。

重写toString方法,返回类型的中文名称。这是在后面编码时重构出来的,刚开始不用考虑。

 

ServiceWindow类

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

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

 

 

 

 

>> MainClass类与Constants类

 

MainClass类

用for循环创建出4个普通窗口,再创建出1个快速窗口和一个VIP窗口。

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

 

Constants类

定义三个常量:

MAX_SERVICE_TIME、 MIN_SERVICE_TIME 、COMMON_CUSTOMER_INTERVAL_TIME

 

 

 

 

>>测试

 

eg:

 

//———————— numberManager————————

package com.cty.bank;

 

import java.util.ArrayList;

import java.util.List;

 

public class NumberManager {

 

         privateInteger lastNumber = 1;

         //因为两个方法都操作了同一个数据,所以加上同步保证线程安全

         privateList queueNumber = new ArrayList();

        

         publicsynchronized Integer generateNewNumber()

         {

                   queueNumber.add(lastNumber);

                   returnlastNumber++;

         }

        

         publicsynchronized Integer fetchServiceNumber()

         {

                   //如果返回为Null就糟了,lastNumber改成Integer

                   Integernumber = null;

                   if(queueNumber.size()> 0)

                   {

                            number= queueNumber.remove(0);

                            returnnumber;

                   }

                   returnnumber;

         }

}

 

 

//———————— numberMachine————————

package com.cty.bank;

 

public class NumberMachine {

 

         privateNumberManager commManager = new NumberManager();

         privateNumberManager speedyManager = new NumberManager();

         privateNumberManager vipManager = new NumberManager();

        

         publicNumberManager getCommManager() {

                   returncommManager;

         }

         publicNumberManager getSpeedyManager() {

                   returnspeedyManager;

         }

         publicNumberManager getVipManager() {

                   returnvipManager;

         }

        

         //单例设计模式

         privateNumberMachine(){}

        

         privatestatic NumberMachine numberMachine = null;

        

         publicsynchronized static NumberMachine getInstance()

         {

                   if(numberMachine== null)

                   {

                            numberMachine= new NumberMachine();

                            returnnumberMachine;

                   }

                   returnnumberMachine;

         }

        

}

 

 

//———————— CustomerType————————

package com.cty.bank;

 

public enum CustomerType {

 

         COMMON,SPEEDY,VIP;

        

         publicString toStirng()

         {

                   switch(this)

                   {

                   caseCOMMON:

                            return"普通";

                   caseSPEEDY:

                            return"快速";

                   caseVIP:

                            return"贵宾";

                   }

                   returnnull;

         }

}

 

 

//———————— Constants ————————

package com.cty.bank;

 

public class Constants {

 

         //定义提供服务的时间范围

         publicstatic int MAX_SERVICE_TIME = 18;

         publicstatic int MIN_SERVICE_TIME = 6;

        

         //定义各种类型顾客访问的频率

         //publicstatic int VIP_CUSTOMER_INTERVLA_TIME = 1;

         publicstatic int COMM_CUSTOMER_INTERVLA_TIME = 1;

         //publicstatic int SPEEDY_CUSTOMER_INTERVLA_TIME = 3;

}

 

 

//———————— ServiceWindows————————

package com.cty.bank;

 

import java.util.Random;

import java.util.concurrent.Executors;

 

public class ServiceWindows {

 

         privateCustomerType type = CustomerType.COMMON;

         privateint windowId = 1;

        

         //为什么用set方法而不用构造函数呢?因为模拟窗口的类型是可以以后变化的

         publicvoid setType(CustomerType type) {

                   this.type= type;

         }

         publicvoid setWindowId(int windowId) {

                   this.windowId= windowId;

         }

        

        

         publicvoid startService()

         {

                   Executors.newSingleThreadExecutor().execute(newRunnable(){

                            publicvoid run()

                            {

                                     while(true)

                                     {

                                               StringwindowName = null;

                                               switch(type)

                                               {

                                               caseCOMMON:

                                                        commonService();

                                                        break;

                                               caseSPEEDY:

                                                        speedyService();

                                                        break;

                                               caseVIP:

                                                        vipService();

                                                        break;

                                               }

                                     }

                            }

                   });

         }

        

         privatevoid commonService(){

                   StringwindowName = "第" + windowId + "号" +type.toStirng() + "窗口";           

                   System.out.println(windowName+ "开始获取普通任务!");

                   IntegerserviceNumber =

NumberMachine.getInstance().getCommManager().fetchServiceNumber();

                   if(serviceNumber!= null ){

                            System.out.println(windowName+

"开始为第" + serviceNumber + "号普通客户服务");            

                            intmaxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;

                            intserviceTime =

new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME;

        

                            try{

                                     Thread.sleep(serviceTime*1000);

                            }catch (InterruptedException e) {

                                     e.printStackTrace();

                            }       

                            System.out.println(windowName+ "完成为第" +

serviceNumber +"号普通客户服务,总共耗时" + serviceTime + "秒");

                   }else{

                            System.out.println(windowName+ "没有取到普通任务,正在空闲一秒");              

                            try{

                                     Thread.sleep(1000);

                            }catch (InterruptedException e) {

                                     e.printStackTrace();

                            }                                   

                   }

         }

        

         privatevoid speedyService(){

                   IntegerserviceNumber =

                           NumberMachine.getInstance().getSpeedyManager().fetchServiceNumber();

                   StringwindowName = "第" + windowId + "号" +type.toStirng() + "窗口";  

                   System.out.println(windowName+ "开始获取快速任务!");                 

                   if(serviceNumber!=null){

                            System.out.println(windowName+ "开始为第" +

serviceNumber + "号快速客户服务");                    

                            intserviceTime = Constants.MIN_SERVICE_TIME;

                            try{

                                     Thread.sleep(serviceTime*1000);

                            }catch (InterruptedException e) {

                                     e.printStackTrace();

                            }                

                            System.out.println(windowName+ "完成为第" + serviceNumber

+ "号快速客户服务,总共耗时" + serviceTime + "秒");              

                   }else{

                            System.out.println(windowName+ "没有取到快速任务!");                                 

                            commonService();

                   }

         }

        

         privatevoid vipService(){

 

                   IntegerserviceNumber =

                           NumberMachine.getInstance().getVipManager().fetchServiceNumber();

                   StringwindowName = "第" + windowId + "号" +type.toStirng() + "窗口";  

                   System.out.println(windowName+ "开始获取VIP任务!");                  

                   if(serviceNumber!=null){

                            System.out.println(windowName+ "开始为第" +

serviceNumber + "号VIP客户服务");  

                            intmaxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;

                            intserviceTime =

new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME;

                            try{

                                     Thread.sleep(serviceTime*1000);

                            }catch (InterruptedException e) {

                                     e.printStackTrace();

                            }                

                            System.out.println(windowName+ "完成为第" +

serviceNumber + "号VIP客户服务,总共耗时" + serviceTime + "秒");    

                   }else{

                            System.out.println(windowName+ "没有取到VIP任务!");                                  

                            commonService();

                   }       

         }

}

 

 

//———————— main ————————

package com.cty.bank;

 

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

 

public class BankMain {

 

         publicstatic void main(String[] args) {

                  

                   //开启4个普通窗口

                   for(int i = 1; i <= 4; i++) {

                            ServiceWindowscommWindow = new ServiceWindows();

                            commWindow.setType(CustomerType.COMMON);

                            commWindow.setWindowId(i);

                            commWindow.startService();

                   }

                  

                   //开启一个快速窗口

                   ServiceWindowsspeedyWindow = new ServiceWindows();

                   speedyWindow.setType(CustomerType.SPEEDY);

                   speedyWindow.setWindowId(5);

                   speedyWindow.startService();

                  

                   //开启一个vip窗口

                   ServiceWindowsvipWindow = new ServiceWindows();

                   vipWindow.setType(CustomerType.VIP);

                   vipWindow.setWindowId(6);

                   vipWindow.startService();

                  

                   //取号器开启三种类型客户线程

                   //普通客户拿号

                   Executors.newScheduledThreadPool(1).scheduleAtFixedRate(

                                     newRunnable(){

                                               publicvoid run(){

                                                        IntegerserviceNumber

= NumberMachine.getInstance().getCommManager().generateNewNumber();

                                                       

                                                        System.out.println("第" +serviceNumber

+ "号普通客户正在等待服务!");        

                                               }

                                     },

                                     0,

                                     Constants.COMM_CUSTOMER_INTERVLA_TIME,

                                     TimeUnit.SECONDS);

                  

                   //快速客户拿号

                   Executors.newScheduledThreadPool(1).scheduleAtFixedRate(

                                     newRunnable(){

                                               publicvoid run(){

                                                        IntegerserviceNumber

= NumberMachine.getInstance().getSpeedyManager().generateNewNumber();

                                                        System.out.println("第" +serviceNumber +

"号快速客户正在等待服务!");

                                               }

                                     },

                                     0,

                                     Constants.COMM_CUSTOMER_INTERVLA_TIME*3,

                                     TimeUnit.SECONDS);

                  

                   //VIP客户拿号

                   Executors.newScheduledThreadPool(1).scheduleAtFixedRate(

                                     newRunnable(){

                                               publicvoid run(){

                                                        IntegerserviceNumber

= NumberMachine.getInstance().getVipManager().generateNewNumber();

                                                        System.out.println("第" +serviceNumber +

"号VIP客户正在等待服务!");

                                               }

                                     },

                                     0,

                                     Constants.COMM_CUSTOMER_INTERVLA_TIME*6,

                                     TimeUnit.SECONDS);

         }       

}


你可能感兴趣的:(技术日记)