首页 > 代码库 > 黑马程序员——交通信号灯管理系统

黑马程序员——交通信号灯管理系统

 
题目:
 
模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:
 
异步随机生成按照各个路线行驶的车辆。
 
例如:
 
由南向而来去往北向的车辆 右转车辆
 
由东向而来去往南向的车辆");
 }
 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;
 }
 
 package com.itheima.interview;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LampController {
 
 private Lamp currentLamp;
 
 LampController(){
  this.currentLamp = Lamp.S2N;
  currentLamp.light();
  
  ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
  timer.scheduleAtFixedRate(
    new Runnable(){
     public void run(){
      currentLamp = currentLamp.blackOut();
     }
    },
    10,
    10,
    TimeUnit.SECONDS);
 }
}
 
 package com.itheima.interview;
public class MainClass {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  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]);
  }
  new LampController();
 }
}

详情请查看:http://edu.csdn.net/heima

黑马程序员——交通信号灯管理系统