交通灯管理系统

来源:互联网 发布:php array key 编辑:程序博客网 时间:2024/05/29 04:49
---------------------- android培训、java培训、期待与您交流! ----------------------

今天总结下交通灯管理系统,其中涉及到多线程、枚举等重要知识点

.Road类:通过设计一个道路来来模拟道路中车辆通过路口的运行情况

   public class Road {
   //
面向接口编程
    List<String> vechices=new ArrayList<String>();
    private String name;
    public Road(String name){
    this.name=name;
   ExecutorService pool=Executors.newSingleThreadExecutor();//
线程池确保对象建立完毕也有线程在执行
    pool.execute(
         new Runnable(){
             public void run(){      
                for(int i=1;i<=100;i++){
                try {
                     Thread.sleep((new Random().nextInt(10)+1)*1000);
                   } catch (InterruptedException e) {       
                e.printStackTrace();
               }
              vechices.add(Road.this.name+"_"+i);
          }
         }
       });
   ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);//
定时器,循环执行
   timer.scheduleAtFixedRate(
         new Runnable(){
             public void run(){
                   if(vechices.size()>0){
                   boolean lighted=Lamp.valueOf(Road.this.name).islighted();
                   if(lighted){
                     System.out.println(vechices.remove(0)+" is traving ");
                   }
                 }
               }
             }, 
          1,//
过一秒后执行
          1,//
每过一秒执行
          TimeUnit.SECONDS);//
时间单位
      }
    }

.Lamp类:设计一个路灯类,控制不同道路间红绿灯以及相反方向的灯的红绿转换

   public enum 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),
      E2N(null,null,true),N2W(null,null,true),W2S(null,null,true),S2E(null,null,true);


      private String opposite;
      private String next;
      private boolean lighted;
 
      private Lamp(String opposite,String next,boolean lighted){
             this.opposite=opposite;
             this.next=next;
             this.lighted=lighted;
  
      }
     public boolean islighted(){
             return lighted;
      }
 
     public void light(){
             this.lighted=true;
         if(opposite!=null){
              Lamp.valueOf(opposite).light();
     }
     System.out.println(name()+" Lamp id green,
在以下六个方向有车通过");
     }
 
    public Lamp blackout(){
      this.lighted=false;
        if(opposite!=null){
          Lamp.valueOf(opposite).blackout();
     }
   
  Lamp nextLamp=null;
       if(next!=null){
         nextLamp=Lamp.valueOf(next);
         System.out.println("green lamp from "+name()+" to "+ next+" and "+Lamp.valueOf

         (next).opposite);
          nextLamp.light();
     }
        return nextLamp;
 }
 
.LampController类:控制Lamp类的对象,

                     通过控制设计一个定时器来循环控制当前路灯和其下一个路灯的红绿转换。

      public class LampController {
      Lamp currentLamp;
 
      public LampController(){
       currentLamp=Lamp.S2N;
       currentLamp.light();
  
   ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);
     timer.scheduleAtFixedRate(
        new Runnable(){
        public void run(){
        currentLamp.blackout();
           }
        },
        10,
        10,
        TimeUnit.SECONDS);
 }
}

.主类:定义一个字符串数组drection存储道路信息,

        循环建立路的匿名对象,添加车辆信息,控制路灯

   public class MainClass {
      public static void main(String[] args) {
      String[] drection=new String[]{
            "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","N2W","W2S","S2E","E2N"
       };
  
      for(int i=0;i<drection.length;i++){
          new Road(drection[i]);
       }
      new LampController();
    }

}

 

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