黑马程序员-----交通灯和银行业务调度面试题

来源:互联网 发布:高尔夫网络电视直播 编辑:程序博客网 时间:2024/05/16 07:08

  -----------android培训java培训、java学习型技术博客、期待与您交流!------------

交通信号灯和银行业务调度面试题

一,需求分析:
   1,需求:
     ①,异步随机生成按照各个线路行驶的车辆:直行,左转,右转;
     ②,信号灯只考虑红绿,不考虑黄灯
     ③,左转受信号灯控制,右转不受信号灯控制
     ④,南北车辆与东西车辆交替放行。同方向车辆先放行直行车辆在放行左转车辆
     ⑤,每辆车通过路口时间为1
     ⑥,随机生成车辆时间及红绿灯交换时间自定,可以设置;
Road类:

定义一个集合,存储所有的车子。

定义一个线程池,每隔1-10秒往集合中添加车子。

定义一个定时器,首先判断灯是否为绿灯,如果灯为绿,每隔一秒将路上的第一辆车从集合中移除,模拟车子经过十字路口。

Lamp类:

定义一个枚举类,枚举类中添加12个元素,代表十二个方向的车,每个元素以对面的灯,下一个灯,及灯现在的状态为参数。

  定义灯的点亮方法,将自己和对面的灯点亮。

定义灯的熄灭方法(变红),将自己和对面的灯都熄灭,并让自己的下一盏灯点亮,这个方法同时返回一个Lamp对象,即为当前被点亮的灯对象,方便LampController调用。

LampController类:

  首先点亮枚举类中的第一个方向的灯,该灯点亮,则其相反方向的灯同时点亮。

定义一个定时器,每隔10秒钟调用灯的熄灭方法,则下一个方向及其相反方向的灯被点亮。

MainClass

  产生12Road对象,生成LampController对象



线程池定义:

ExecutorService pool=Executors.newSingleThreadExecutor();

  pool.execute(new Runnable(){

      publicvoid run()

      {         

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

          {

              try {

                  Thread.sleep((newRandom().nextInt(10)+1)*1000);

                  vehicles.add(Road.this.name + "_" + i);

              }catch(Exception e) {

                  e.printStackTrace();

              }

          }

      }

   });

定时器定义:

ScheduledExecutorServicetimer=Executors.newScheduledThreadPool(1);

  timer.scheduleAtFixedRate(new Runnable(){

      publicvoid run(){

          if(vehicles.size()>0)

          {

              boolean lighted = Lamp.valueOf(Road.this.name).isLighted();

              if(lighted)

              {

                  System.out.println(vehicles.remove(0)+"is traversing!");

              }

             

          }

      }

   },

          1,

          1,

          TimeUnit.SECONDS);

   }





银行业务调度系统



面向对象的分析与设计



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

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

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

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

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





NumberMachine

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

NumberManager

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

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

ServiceWindow

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

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

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

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

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

MainClass类:

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

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

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

packageBankDispatchSystem;



importjava.util.Random;

importjava.util.concurrent.Executors;



publicclass ServiceWindow {

   privateMON;

   private intwindowId = 1;

 

   public voidsetType(CustomerType type) {

     this.type = type;

   }



   public voidsetWindowId(int windowId) {

     this.windowId = windowId;

   }





 

   public voidstart(){

     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();

                }

                intcostTime=(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();

                }

                intcostTime=(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();

                }

                intcostTime=(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 {



   publicstatic 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);


 -----------android培训java培训、java学习型技术博客、期待与您交流!------------


0 0
原创粉丝点击