交通灯管理系统的项目需求
模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:
异步随机生成按照各个路线行驶的车辆.
例如:
由南向而来去往北向的车辆---直行车辆
由西向而来去往南向的车辆---右转车辆
由东向而来去往南向的车辆---左转车辆
信号灯忽略黄灯,只考虑红灯和绿灯.
应考虑左转车辆控制信号灯,右转车辆不受信号灯控制.
具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑.
注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆.
每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式来模拟).
随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置.
不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展线程序运行结果.
我们初步设想一下有哪些对象:红绿灯,红绿灯的控制系统,汽车,路.汽车看到自己所在路线对应的灯绿了就穿过路口吗?不是,还需要看其前面是否有车,看前面是否有车,该问哪个对象呢?
该问路,路中存储着车辆的集合,显然路上就应该有增加车辆和减少车辆的方法了.再看题目,我们这里并不要体现车辆移动的过程,只要捕捉车辆穿过路口的过程,也就是捕捉路上减少一辆车的过程,所以,这个车并不需要单独设计成为一个对象,用一个字符串表示就可以了.
Class Rope{
private Point start;
Private Point end;
public Rope(Point start,Poing end){
this.start = start;
this.end = end;
}
public Point nextPoint(Point currentPoint){
/*通过两点一线的数学公式可以计算出当前点的下一个点,这个细节不书属于设计阶段要考虑的问题,如果当前是终止点,则返回null,如果当前点不是线上的点,则抛出异常.*/
}
}
class Ball{
private Rope rope;
private Point currentPoint;
public Ball(Rope rope,startPoint){
this.rope = rope;
this.currentPoint = startPoint;
}
public void move(){
currentPoint = rope.nextPoint(currentPoint);
System.out.println("小球移动到了" + currentPoint);
}
}
人在黑板上画原
person,blackboard,circle
darw(){
x,y-->radius
}
StoneKnife = KnifeFactory.createKnife(stone)
Stone
mateialStoneKnife.cut(tree)
tree
chair = ChairFactory.makeChair(material)
material
chair
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);
private Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
private boolean lighted;
private String opposite;
private String next;
public boolean isLighted(){
return lighted;
}
public void light(){
this.lighted = true;
if(opposite!=null){
Lamp.valueOf(opposite).light();
}
System.out.println(name()+ "lamp is green,下面总共应该有6个方向能看到汽车穿过!");
}
public Lamp blackOut(){
this.lighted = false;
if(opposite!=null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp = Lamp.valueOf(next);
if(next != null){
System.out.println("绿灯从" + name() +"------------->切换为" + next);
nextLamp.light();
}
return nextLamp;
}
}
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.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
public class MainClass {
/**
* @param args
*/
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++){
new Road(directions[i]);
}
new LampController();
}
}
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 = null;
public Road(String name){
this.name=name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
public void run(){
for(int i=1;i<1000;i++){
try{
Thread.sleep((new Random().nextInt(10)+1)*1000);
}catch(InterruptedException e){
e.printStackTrace();
}
vechicles.add(Road.this.name+"_"+i);
}
}
});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
private boolean lighted;
public void run(){
if(vechicles.size()>0){
boolean ligthed = true;
if(lighted){
System.out.println(vechicles.remove(0) + "is traversing !");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}