黑马程序员— 交通灯管理系统_代码实现

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

Lamp类编写:

/*每个Lamp元素代表一个方向上的灯,总共有12个,所以总共有12个元素*/
public enum Lamp {
 /* 每个枚举元素个表示灯的一个控制方向 */
 S2N("N2S", "S2W", false), S2W("N2E", "E2W", false), E2W("W2E", "E2S", false), E2S(
   "W2N", "S2N", false),
 /* 下面的元素表示与上面的元素相反方向的灯,其中的参数值,不重要,应忽略不计 */
 N2S(null,null,true), N2E(null,null,true), W2E(null,null,true), W2N(null,null,true),

 /* 由南向东和由西向北(右转)的不受红灯的控制,所以可以假设他们为绿灯 */
 S2E(null,null,true), E2N(null,null,true), N2W(null,null,true), W2S(null,null,true);

 /* 灯是否为绿灯 */
 private boolean lighted;
 /* 对应方向的灯 */
 private String opposite;
 /* 灯变红时下一个变绿的灯 */
 private String next;

 private Lamp(String opposite, String next,boolean lighted) {
  this.lighted = lighted;
  this.opposite = opposite;
  this.next = next;
 }
 private Lamp(){}

 public boolean isLighted(){
  return lighted;
 }
 /*
  * 将灯变绿
  * 我对应的灯亮了就回让你对应的灯亮,这样就会变成死循环,因此在
  * 前面加上if(opposite !=null)判断
  */
 public void light(){
  this.lighted = true;
  if(opposite != null){
   Lamp.valueOf(opposite).light(); //我当前的灯如果是绿的,就把我对应的灯也变绿
  }
  System.out.println(this.name() + " lamp is green,下面总共应该有6个方向能看到车行驶!");
 }
 /*
  * 将灯变为黑的,有下一个灯,将该灯变绿
  */
 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("绿灯从"+ this.name() + "------->切换为" +next);      nextLamp.light();    }  return nextLamp; }}

/*
 * Road类的编写
 * 思路:
 * 1,每个Road对象都有一个name成员变量来代表方向,有个vehicles成员变量来代表方向上的车辆集合
 * 2,在Road对象的构造函数中启动一个线程,每隔一个随机的时间向vehicles集合中增加一辆车
 * (用一个“路线名_id”形式的字符串来表示)
 * 3,在Road对象的构造函数中启动一个定时器,每隔一秒检查该方向的上的灯是否为绿灯,是则打印车
 * 辆集合和将集合中的第一辆车移除
 * 
 */
public class Road {
 /* 定义集合,用于存储车辆 */
 private List<String> vechicles = new ArrayList<String>();
 private String name = null;
 public Road(String name) {
  this.name = name;
  /*创建线程池,里面有很多线程,当有任务交给他们时,看池里面哪个线程空闲就让哪个线程执行*/
  ExecutorService pool = Executors.newFixedThreadPool(1);
  pool.execute(new Runnable() {  //new Runnable接口的实例对象
   public void run() {
    for (int i = 1; i < 1000; i++) {
     /*1到10秒随机产生一辆车*/
     try{Thread.sleep((new Random().nextInt(10)+1)*1000);}catch(InterruptedException e){}
     
     vechicles.add(Road.this.name+"_"+i);
    }
   }
  });
  /*定时器,调度池*/
  ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
  timer.scheduleAtFixedRate(
    new Runnable() {  /*将集合中的第一辆车移除*/
     public void run() {
      if(vechicles.size() > 0){
       //判断路上的灯是否是绿灯,如果是,就移除一辆车
       boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
       if(lighted){
        System.out.println(vechicles.remove(0) + " is traversing !");
       }
      }
     }
    },
    1, /*过一秒就开始执行任务*/
    1, /*上个任务执行完了,过1秒在开始执行任务*/
    TimeUnit.SECONDS);/*时间单位*/
 }
}

/*灯的控制器编写

 

,控制灯亮 */
public class LampController {
 private Lamp currentLamp;

 public LampController() {
  currentLamp = Lamp.S2N; // 首先先指定由南向北方向的灯为绿灯
  currentLamp.light();

  /* 每隔十秒就让当前的灯变红,下一个等变绿,那么就需要创建线程池 */
  ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
  timer.scheduleAtFixedRate(new Runnable() {
   public void run() {
    System.out.println("来啦");
    currentLamp = currentLamp.blackOut(); // 将当前灯变黑,并将下一个灯变绿
   }
  }, 
  10, // 过10秒开始执行任务,将灯变黑
  10,// 前面那个任务执行完了,过10秒在开始下一个任务
  TimeUnit.SECONDS);
 }
}

 

/*测试程序*/
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"};  //将12方向存储进数组

  for (int i = 0; i < directions.length; i++) {
    new Road(directions[i]);
  }
  new LampController(); //将第一个路口的等变绿
 }
}


------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值