------- android培训、java培训、期待与您交流! ----------
一、需求:
1,模拟实现十字路口的交通灯管理系统逻辑,异步随机生成按照各个路线行驶的车辆。例如:
由南向而来去往北向的车辆 ---- 直行车辆
由西向而来去往南向的车辆 ---- 右转车辆
由东向而来去往南向的车辆 ---- 左转车辆
。。。
2,信号灯忽略黄灯,只考虑红灯和绿灯。
3,应考虑左转车辆控制信号灯,右转车辆不受信号灯控制。
4,具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑。
注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆。
5,每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式模拟)。
6,随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置。
二、分析:
十字路口包括直行线路、左转线路和右转线路一共十二条线路,其中四条右转线路不受信号灯控制,因此只考虑其它八条线路。八条线路中包括东西南北的左转和直行线路,每次只能放行两条线路即相对两个方向的直行或者左转线路,共放行四次循环一次,因此可将八条线路分为四组,每次放行一组,轮流放行。
整个系统用面向对象的设计思想总共可创建三个类,分别是:Road,十二条线路用十二个Road对象表示,Road内有数据成员用于存储路上的车,并适时增减;枚举类Lamp,用于表示十二组红绿灯,并提供操作红绿灯的方法;红绿灯控制器LampController,用于控制十二组红绿灯的显示。
三、代码如下:
package com.isoftstone.interview.traffic;
public class MainClass {
/**
* @param args
*/
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();
}
}
//Road.java
package com.isoftstone.interview.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> vechicles = new ArrayList<String>();
private String name;
public Road(String name){
this.name = name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
@Override
public void run() {
int i = 1;
while(true){
try {
Thread.sleep((new Random().nextInt(10)+1)*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
vechicles.add(Road.this.name+i);
i++;
}
}});
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(
new Runnable(){
@Override
public void run() {
if(vechicles.size()>0){
boolean light = Lamp.valueOf(Road.this.name).isLighted();
if(light){
System.out.println(Lamp.valueOf(Road.this.name).attribute+"的车辆:"+vechicles.remove(0)+"通过了路口");
}
}
}},
1,
1,
TimeUnit.SECONDS);
}
}
//Lamp.java
package com.isoftstone.interview.traffic;
public enum Lamp {
S2N(false,"N2S","S2W","由南向北"),S2W(false,"N2E","E2W","由南向西"),
E2W(false,"W2E","E2S","由东向西"),E2S(false,"W2N","S2N","由东向南"),
N2S(false,null,null,"由北向南"),N2E(false,null,null,"由北向东"),
W2E(false,null,null,"由西向东"),W2N(false,null,null,"由西向北"),
S2E(true,null,null,"由南向东"),E2N(true,null,null,"由东向北"),
N2W(true,null,null,"由北向西"),W2S(true,null,null,"向西向南");
private boolean lighted;
private String opposite;
private String next;
public String attribute;
private Lamp(boolean lighted, String opposite, String next,String attribute) {
this.lighted = lighted;
this.opposite = opposite;
this.next = next;
this.attribute = attribute;
}
public boolean isLighted(){
return lighted;
}
public void light(){
this.lighted = true;
System.out.println(name()+"变成了绿灯!以下有六个方向的车辆通行");
if(opposite!=null)
Lamp.valueOf(opposite).light();
}
public Lamp blackOut(){
this.lighted = false;
System.out.println(name()+"变成了红灯");
if(opposite!=null)
Lamp.valueOf(opposite).blackOut();
Lamp nextLamp = null;
if(next!=null){
nextLamp = Lamp.valueOf(next);
nextLamp.light();
}
return nextLamp;
}
}
//LampController.java
package com.isoftstone.interview.traffic;
import java.util.concurrent.ExecutorService;
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.light();
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(
new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}