银行业务调度系统


面向对象的分析与设计

 

需求 : 有三种类型的客户:VIP客户,普通客户,快速客户,异步随机生成各种类型的客户,各种类型客户在其对应窗口按顺序依次办理业务。

          号码管理器对象,让这个对象不断地产生号码,就等于随机生成了客户。

          由于有三类客户,每类客户的号码编排都是完全独立的,所以,想到本系统一共要产生三个号码管理器对象,各自管理一类用户的排队号码。

          这三个号码管理器对象统一由一个号码机器进行管理,这个号码机器在整个系统中只能有一个,所以,它要被设计成单例。

          各类型客户在其对应的窗口依次办理业务,准确地说,应该是窗口依次叫号。

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

各类关系如下

             https://img-my.youkuaiyun.com/uploads/201208/23/1345720966_1576.jpg

NumberMachine

定义得到号码管理器对象的功能,因为号码生成器只有一个,使用单例设计模式。

NumberManager

实现取号功能,将号码加入队列中;

如果队列中号码个数大于零,则取出队列中的第一个。因为是多线程,加synchronized

ServiceWindow

定义开始方法,定义一个进程池,根据不同的客户执行不同的服务代码

普通客户服务代码,获取对象,如果有对象,为其服务,结束后,再次调用方法,如果没有,先休息一秒钟,再获取对象

快速客户服务代码,获取对象,如果有对象,为其服务,结束后,再次调用方法,如果没有对象,获取普通客户对象,为普通客户服务,服务时间固定为一秒

VIP客户服务代码,获取对象,如果有对象,为其服务,结束后,再次调用方法,如果没有对象,获取普通客户对象,为普通客户服务

将程序中用到的常量在单独的类中定义,单独定义一个枚举类,存放客户类型,并复写toString()方法。

MainClass类:

按照题目要求,生成4个普通窗口,1个快速窗口,1VIP窗口,调用窗口的start()方法,开始获取相应客户,获取相应客户后为之服务。

建立线程池,按照客户比例设置相应的定时器间隔来生成客户。

---------------ServiceWindow---------------


packageBankDispatchSystem;

importjava.util.Random;
importjava.util.concurrent.Executors;

publicclass 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(newRunnable(){

           public void run(){

              while(true)

              {

                  switch(type)

                  {

                  case COMMON:

                     commonService();

                     break;

                  case EXPRESS:

                     expressService();

                     break;

                  case VIP:

                     vipService();

                     break;

                  }

              }

           }

});

    }
           private void commonService() {

              String windowName="第"+windowId+type+"窗口";/

//此处使用type,而不固定为普通,因为快速窗口及VIP窗口会调用该方法

              Integer number =NumberMachine.getInstance().getCommonManager().fetchServiceNumber();

              System.out.println(windowName+"正在获取任务");

              if(number!= null)

              {

                  System.out.println(windowName+"为第"+number+"个普通客户服务");

                  long beginTime = System.currentTimeMillis();

                  int maxRand =Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;

                  long serveTime = newRandom().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;

                  try {

                     Thread.sleep(serveTime);

                  } catch (Exception e) {

                     e.printStackTrace();

                  }

                  int costTime=(int)(System.currentTimeMillis()-beginTime)/1000;

                  System.out.println(windowName+"为第"+number+"个普通客户完成服务,耗时"+costTime+"秒");

              }

              else{

                  System.out.println(windowName+"没有取到服务任务,先休息一秒钟嘛");

                  try {

                     Thread.sleep(1000);

                  } catch (Exception e) {

                     e.printStackTrace();

                  }

              }

           }

           private void expressService() {

              String windowName="第"+windowId+"号"+type+"窗口";

              Integer number =NumberMachine.getInstance().getExpressManager().fetchServiceNumber();

              System.out.println(windowName+"正在获取任务");

              if(number!= null)

              {

                  System.out.println(windowName+"为第"+number+"个"+type+"客户服务");

                  long beginTime = System.currentTimeMillis();

                  try {

                     Thread.sleep(Constants.MIN_SERVICE_TIME);

                  } catch (Exception e) {

                     e.printStackTrace();

                  }

                  int costTime=(int)(System.currentTimeMillis()-beginTime)/1000;

                  System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime+"秒");

              }

              else{

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

                  commonService();

                  }

              }

           private void vipService() {

              String windowName="第"+windowId+"号"+type+"窗口";

              Integer number = NumberMachine.getInstance().getVipManager().fetchServiceNumber();

              System.out.println(windowName +"正在获取任务");

              if(number!= null)

              {

                  System.out.println(windowName+"为第"+number+"个"+type+"客户服务");

                  long beginTime =System.currentTimeMillis();

                  int maxRand =Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;

                  long serveTime = newRandom().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;

                  try {

                     Thread.sleep(serveTime);

                  } catch (Exception e) {

                     e.printStackTrace();

                  }

                  int costTime=(int)(System.currentTimeMillis()-beginTime)/1000;

                  System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime+"秒");

              }

              else{

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

                  commonService();

              }

              }

           }

--------------------MainClass_Bank--------------------

packageBankDispatchSystem;

importjava.util.concurrent.Executors;
importjava.util.concurrent.TimeUnit;

publicclass MainClass_Bank {
    public static void main(String[] args) {

       for(int i=1;i<5;i++)

       {
           ServiceWindow commonWindow = newServiceWindow();

           commonWindow.setWindowId(i);
           commonWindow.start();

       }
       ServiceWindow expressWindow = newServiceWindow();
       expressWindow.setType(CustomerType.EXPRESS);
       expressWindow.start();
       ServiceWindow vipWindow = newServiceWindow();
       vipWindow.setType(CustomerType.VIP);
       vipWindow.start();
       Executors.newScheduledThreadPool(1).scheduleAtFixedRate(newRunnable(){
           public void run()
           {
              Integer number =NumberMachine.getInstance().getCommonManager().generateNewManager();
               System.out.println(number+"号普通客户等待服务");

           }
       },
       0,
       Constants.COMMON_CUSTOMER_INTERVAL_TIME,
       TimeUnit.SECONDS);
       //使用定时器产生普通客户,快速客户及VIP客户    Executors.newScheduledThreadPool(1).scheduleAtFixedRate(newRunnable(){

           public void run()
           {
              Integer number = NumberMachine.getInstance().getExpressManager().generateNewManager();

              System.out.println(number+ "号快速客户等待服务");

           }
       },
       0,
       Constants.COMMON_CUSTOMER_INTERVAL_TIME*2,

       TimeUnit.SECONDS);
       Executors.newScheduledThreadPool(1).scheduleAtFixedRate(newRunnable(){

           public void run()

           {

              Integer number =NumberMachine.getInstance().getVipManager().generateNewManager();

              System.out.println(number+ "号VIP客户等待服务");

           }
       },
       0,
       Constants.COMMON_CUSTOMER_INTERVAL_TIME*6,

       TimeUnit.SECONDS);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值