模拟交通灯管理系统

来源:互联网 发布:usb hub的数据读取 编辑:程序博客网 时间:2024/05/20 10:52

package com.itheima.traffic;

 /*  设计思路:  因为是模拟交通灯,所以想到的对象有:路,灯,汽车。  创建一个车的集合,通过添加,删除元素来实现车的行驶。  交通灯的方向采用枚举的方式列出。  */  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;public class Road {  private List<String> vechicls = new ArrayList<String>();  //创建一个车的集合 private String name=null;  public Road(String name)  //构造方法赋值(路) {   this.name = name;   ExecutorService pool = Executors.newSingleThreadExecutor();   //使用Executors工具类,创建一个 增加车的线程的  pool.execute(new Runnable(){    public void run()    {     for(int i = 1;i< 1000;i++)      //设定有1000辆车循环增加车辆    {      try {       Thread.sleep((new Random().nextInt(10)+1)*1000);       //设定每隔1-10秒增加一次     } catch (InterruptedException e) {       e.printStackTrace();      }      vechicls.add(Road.this.name+"_"+i);      //增加车辆(名字为:路_第几辆车)    }      }       });          ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);   // 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。//创建线程池,控制车辆行驶   timer.scheduleAtFixedRate(      new Runnable(){       public void run()       {        if(vechicls.size()>0)//当集合大小不为0时,才执行。       {         boolean lighted = Lamp.valueOf(Road.this.name).isLighted();        //调用Lamp类的isLighted();返回灯是否亮。        if(lighted)          System.out.println(vechicls.remove(0)+"is traversing!");        }       }             },      1,//第一秒开始     1,//每隔一秒执行一次     TimeUnit.SECONDS);//单位秒       }   } package com.itheima.traffic;/*  * 使用枚举,列举灯控制发方向 * 北:N  南:S  西:W  东:E  * S2N代表:南向北方向。以此类推。 * */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),  S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);  //列举出12个方向 private Lamp (String opposite,String next,boolean lighted)  {   this.opposite = opposite;   this.next = next;  }    private boolean lighted;  //灯是否是绿。 private String opposite;  //对面的灯 private String next;  //下一个灯 public boolean isLighted()  {   return lighted;  }  public void ligth()  {   this.lighted = true;   //设定某一个路口的灯亮绿   if(opposite !=null)   {        Lamp.valueOf(opposite).ligth();    //对面驶来方向的灯也要亮绿  }   System.out.println(name()+"  Lamp is Green  下边总共有6个方向的汽车穿过!");     }  /*某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿  * return 下一个要变绿的灯*/  public Lamp blackOut()  {   this.lighted = false;   if(opposite !=null)   {    Lamp.valueOf(opposite).blackOut();   }   Lamp nextLight = null;   if(next !=null)   {    nextLight = Lamp.valueOf(next);    System.out.println("绿灯从"+name()+"切换为--------->next");    nextLight.ligth();   }    return nextLight;    } }package com.itheima.traffic;import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; //创建灯控制器类public class LampController {   private Lamp currentLamp;   public LampController()   {    currentLamp = Lamp.S2N;    //设定当前灯控制的方向   currentLamp.ligth();    //灯为绿       ScheduledExecutorService timer =Executors.newScheduledThreadPool(1);    timer.scheduleAtFixedRate(      new Runnable(){       public void run()       {        currentLamp = currentLamp.blackOut();       }      },      10,      10,      TimeUnit.SECONDS);   }}package com.itheima.traffic; //创建Main类 执行public class MainClass { public static void 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++)//创建12条路线  {    new Road(directions[i]);   }   new LampController();   //开启灯控制器   }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿被蜜蜂蜇了怎么办 婴儿被黄蜂蛰了怎么办 蜜蜂在石头缝里怎么办 中蜂蜂王不产卵怎么办 冲电器充电变慢怎么办 摇号审核通过后怎么办 京东白条开不了怎么办 得了病心里压力大怎么办 电视家看直播卡怎么办 日上提货单丢了怎么办 想直飞香港l签怎么办 u盘识别不出来怎么办 卫生间下水道有小飞虫怎么办 橙光一直闪退怎么办 若白回来了,长安怎么办 没了你以后我该怎么办 玩cf的时候闪退怎么办 婴儿嗓子哭哑了怎么办 宝宝嗓子哭哑了怎么办 小孩嗓子哭哑了怎么办 孩子嗓子哭哑了怎么办 月经来了晚上漏怎么办 在学校月经漏了怎么办 月经来了血下不来怎么办 想让月经快点来怎么办 孩子来月经不规律怎么办 一个月来2次月经怎么办 14岁月经不规律怎么办 不是经期内裤有黑色血怎么办 月经量多血块多怎么办 网友见面没上她怎么办 拔完智齿老流血怎么办 学车教练不教怎么办 想开奶茶店没有经验怎么办 宝宝吃多了不消化怎么办 吃了糖精和鸡蛋怎么办 小米平板2太卡了怎么办 小米平板2卡顿怎么办 小米4黑屏没反应怎么办 小米5c开不了机怎么办 小平板开不了机怎么办