面试题交通灯管理系统实现


                                            

                                         交通灯管理系统

 

模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:

Ø         异步随机生成按照各个路线行驶的车辆。

例如:

       由南向而来去往北向的车辆 ---- 直行车辆

       由西向而来去往南向的车辆 ---- 右转车辆

       由东向而来去往南向的车辆 ---- 左转车辆

       。。。 

Ø         信号灯忽略黄灯,只考虑红灯和绿灯。 

Ø         应考虑左转车辆控制信号灯,右转车辆不受信号灯控制。 

Ø         具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑。

注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆。 

Ø         每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式模拟)。

Ø         随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置 

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

 

视频 1—6为分析

视频7—11为代码设计

路的分析:增加车辆和减少车辆的方法,做线程使用JDK1.5的新技术:线程池   

          定时器的使用,集合中泛型的使用;

灯的分析:用到枚举技术

灯控制器:控制灯的变化情况

里面需要注意的几个重点:

1、             注意面向对象的设计

a)        两块石头磨成石刀,石刀砍树、树做成椅子……

Knife KnifeFactory(Stone first,Stone second))

2、           Lamp枚举类的设计------传入oppsite、next、lighted的设计

3、             灯亮和熄灭的设计

重点代码实现:

路的设计:

package com.isoftstone.interview; 

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

publicclass Road {

    List<String>vehicles=new ArrayList<String>();

    private Stringname=null;

    public Road(String name){

       this.name=name;

       ExecutorService pool= Executors.newSingleThreadExecutor();

       pool.execute(new Runnable(){

           publicvoid run(){

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

                   try {

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

                  } catch (InterruptedException e) {

                     e.printStackTrace();

                  }

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

              }     

           }

       });    

       ScheduledExecutorService timer=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 travelling!");

                        }

                     }

                  }

              },

              1,

              1,

              TimeUnit.SECONDS);

    }

}

灯的设计:

package com.isoftstone.interview;

publicenum Lamp {

    S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),

    N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),

    S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);

    private Stringoppsite;

    private Stringnext;

    privatebooleanlighted;

    private Lamp(String oppsite,String next,boolean lighted){

       this.oppsite=oppsite;

       this.next=next;    

       this.lighted=lighted;

    }

    publicboolean isLighted(){

       returnlighted;

    }

    publicvoid light(){

       this.lighted=true;

       if(oppsite!=null){

           Lamp.valueOf(oppsite).lighted=true;

       }

       System.out.println(this.name()+"lamp is green,下面会有6个方向能够看到车");

    }

    publicvoid blackOut(){

       this.lighted=false;

       if(oppsite!=null){

           Lamp.valueOf(oppsite).lighted=false;

       }

       Lamp nextLamp=null;

       if(next!=null){

           nextLamp=Lamp.valueOf(next);

           System.out.println("绿灯从"+name()+"-->切换为"+next);

           nextLamp.light();

       }

    }

}

灯控制器的设计:

package com.isoftstone.interview;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

publicclass LampController {

    public LampcurrentLamp;

    public LampController(){

       currentLamp=Lamp.S2N;

       currentLamp.light();

       ScheduledExecutorService pool=Executors.newScheduledThreadPool(1);

       pool.scheduleAtFixedRate(

              new Runnable(){

                  publicvoid run(){

                     currentLamp.blackOut();

                  }

              },

              10,

              10,

              TimeUnit.SECONDS);

    }

}

 

主类的设计:

package com.isoftstone.interview;

publicclass MainClass {

    publicstaticvoid main(String[] args){

       String[] directions=new String[]{

              "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"     

       };

       for(int i=0;i<directions.length;i++){

           new Road(directions[i]);

       }

       newLampController();

    }

}

 


你可能感兴趣的:(面试,String,null,交通,n2)