您的位置:首页 > 其它

交通灯管理系统

2012-04-08 08:47 197 查看
交通灯管理系统
一、需求分析。

在进行设计之前我们需要了解具体的十字路口交通规则:1、红灯停绿灯行。2、对于十字路口的四个路口而言,“向右转弯”始终是允许的即该方向始终保持绿灯。3、在十字路口处可以有12条行车路线可以选择(如下图所示),其中可以分为5组,每一组中的一条路线是可行的则表示改组中的其他路线也是可行的。

二、系统设计。

1、 对象分析:系统中可以抽象出的对象为:路、红绿灯和灯控制系统。(在面向对象的分析过程中拥有数据的对象提供操作数据的方法)

2、 对象设计:

1) 路:

每条路线上会出现多辆车,路就是一个集合,路线上要随机增加新的车,当在灯绿期间车通过路口需要一秒,即路集合中每秒减少一辆车,并返回减少车的名字。使用Road类表示路线,每个Road对象代表一个路线,前面已经分析了需要12条路线,即系统中要产生12个Road实例对象。

2) 红绿灯:

每条路线每隔一秒都会检查控制本路线的灯是否为绿,一个灯由绿变红时,应该将下一个方向的灯变绿。设计一个Lamp类来表示一个交通灯,每个交通灯都维护一个状态:亮(绿),不亮(红),每个交通灯要有变亮和变不亮的方法,并且能返回自己的状态。

总共有12条路线,所以,系统中总共要产生12个交通灯。右拐弯的路线本来不受灯的控制,但是为了让程序采用统一的处理方式,故假设出有四个右拐弯的灯,只是这些灯为常亮状态。除了右拐弯方向的其他8条路线的灯,它们是两两成对的,可以归为4组,所以,在编程处理时,只要从这4组中各取出一个灯,对这4个灯依次变亮,与这4个灯方向对应的灯则随之一同变化,因此Lamp类中要有一个变量来记住自己相反方向的灯对象,在一个Lamp对象的变亮和变黑方法中,将对应方向的灯也变亮和变黑。每个灯变黑时,都伴随者下一个灯的变亮,Lamp类中还应该有一个变量来记住自己的下一个灯对象。

无论在程序的什么地方去获得某个方向的灯时,每次获得的都是同一个实例对象,所以Lamp类改用枚举来做显然具有很大的方便性,永远都只有代表12个方向的灯的实例对象。

3) 灯控制系统:设计一个LampController类,它定时让当前的绿灯变红。

三、系统实现。

1、 Road类设计与实现。

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>vehicles =
newArrayList<String>();
//定义一个变量存储路线名
private Stringname =
null;
public Road(String name){
this.name = name;
//模拟车上路的情况,假设该路线上有99辆车
ExecutorServicepool = Executors.newSingleThreadExecutor();

pool.execute(new Runnable() {
@Override
public
void
run() {
//通过线程控制实现,路线每隔1-10秒增加一辆车
for (int i = 0; i < 100; i++) {
try {
Thread.sleep((new Random().nextInt(10)+1)*1000);
}catch (InterruptedException e) {
e.printStackTrace();
}
vehicles.add(Road.this.name +"_" + i);
}
}
});
//每隔一秒检查对应的灯是否为绿,如果是绿的就输出集合第一个,即第一辆车
ScheduledExecutorServicetimer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
@Override
publicvoid run() {
if (vehicles.size()>0) {
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if (lighted) {
System.out.println(vehicles.remove(0) +"is traversing !");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}

2、 Lamp类的设计与实现。

publicenum 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 Stringopposite;

//不同属于一个分组的下一个路线
private Stringnext;
/*
* 功能:判断灯的状态
* 参数:无
* 返回值:灯的状态(true表示绿灯亮 false表示红灯亮)
*/
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= null;
if(next !=null){
nextLamp = Lamp.valueOf(next);
System.out.println("绿灯从" + name()+"-------->切换为"
+next);
nextLamp.light();
}
//返回下一个灯变绿
return nextLamp;
}
}
3、 LampController 类的设计与实现。

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public
class
LampControl {
private LampcurrentLamp;
public LampControl(){
//刚开始让由南向北的灯变绿;
currentLamp = Lamp.S2N;
currentLamp.light();

//10秒灯的转换时间
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
@Override
publicvoid run() {
// 设定定时器
currentLamp =currentLamp.blackOut();
}},
10,
10,
TimeUnit.SECONDS);
}
}

四、测试类。

public
class
TestClass {
/**
* 该类主要用于测试
*/
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 LampControl();
}
}
输出结果:

N2S lamp is green,下面总共应该有6个方向能看到汽车穿过!
S2N lamp is green,下面总共应该有6个方向能看到汽车穿过!
W2S_0 is traversing !
N2S_0 is traversing !
N2W_0 is traversing !
S2N_0 is traversing !
S2E_0 is traversing !
E2N_0 is traversing !
绿灯从S2N-------->切换为S2W
N2E lamp is green,下面总共应该有6个方向能看到汽车穿过!
S2W lamp is green,下面总共应该有6个方向能看到汽车穿过!
S2W_0 is traversing !
N2E_0 is traversing !
S2W_1 is traversing !
W2S_1 is traversing !
S2W_2 is traversing !
E2N_1 is traversing !
N2W_1 is traversing !
N2E_1 is traversing !
S2W_3 is traversing !
E2N_2 is traversing !
S2E_1 is traversing !
W2S_2 is traversing !
N2E_2 is traversing !
N2W_2 is traversing !
绿灯从S2W-------->切换为E2W
W2E lamp is green,下面总共应该有6个方向能看到汽车穿过!
E2W lamp is green,下面总共应该有6个方向能看到汽车穿过!
E2W_0 is traversing !
W2E_0 is traversing !
E2W_1 is traversing !
W2E_1 is traversing !
E2W_2 is traversing !
W2E_2 is traversing !

·······
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: