------- android培训、java培训、期待与您交流! ----------
一、系统实现要求:
交通灯管理系统
——————————————————————————————————————
模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:
1.异步随机生成按照各个路线行驶的车辆。
例如:
由南向而来去往北向的车辆 ---- 直行车辆
由西向而来去往南向的车辆 ---- 右转车辆
由东向而来去往南向的车辆 ---- 左转车辆
。。。
2.信号灯忽略黄灯,只考虑红灯和绿灯。
3 应考虑左转车辆控制信号灯,右转车辆不受信号灯控制。
4 具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑。
注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆。
5 每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式模拟)。
6 随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置。
7 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
——————————————————————————————————————
二、系统分析
1.名词提炼法-------要求中涉及的名词:车辆,路线,信号灯;三者之间的关系,每条线路上都有不同信号灯和车辆。
2.设计模型:
十字路口共有12条路线,其中四条右转线路一直处于通行状态,其余8条线路,可分为方向相对的四组,每一组路线上的红绿灯状态一致,每组选取一条线路组成四条切换灯状态的线路。
线路模型:有自己的名字,车辆池,已建立就异步产生车辆和移动车辆,并根据其上的交通灯判断是否移除车辆。
交通灯模型:使用枚举,它记录对面的灯,以实现每组灯状态一致;记录下一个灯,以实现切换状态
交通灯控制系统模型:记录当前亮着的主控制灯,并定时切换。
3. 路线示意图
——————————————————————————————————————
三、代码实现:
1.Road
import java.util.ArrayList; 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 { /* * 每条路线,都有自己的名字(name),用于存储车辆的集合(vehicles)。名字和它身上的红绿灯同名,以此获取交通灯枚举(Lamp) * 同时,每条路线,已生成就应该不停的 随机 产生车辆和 定时移走车辆 */ private String name;//路线名,与路上的红绿灯同名 private ArrayList<String> vehicles = new ArrayList<String>();//用于存储本线路上的车辆 public Road(String name){ this.name=name; //随机产生车辆 ExecutorService carMaker=Executors.newSingleThreadExecutor(); carMaker.execute(new Runnable(){ @Override public void run() { for(int x =0;x<1000;x++){ //通过sleep方法控制产生车辆的时间间隔,把握节奏 try { Thread.sleep((new Random().nextInt(10)+1)*1000);//这里用随机数,以更逼近现实。 } catch (InterruptedException e) { e.printStackTrace(); } //为通过的车辆编号,并增加到集合中,这里的name有两个,一个构造传进来的参数,一个是外部类的成员变量,所以要区分下 vehicles.add(Road.this.name+"_"+x); } } }); //每个1秒移走车辆,用定时器实现操作 ScheduledExecutorService carMoveTimer=Executors.newScheduledThreadPool(1); carMoveTimer.scheduleAtFixedRate( new Runnable(){ @Override public void run() { //如果路上有车且路上的交通灯为绿才移走车辆 if(vehicles.size()!=0){ //通过路的名字获取其交通灯,并判断交通灯是否亮 boolean lighted=Lamp.valueOf(Road.this.name).isLighted(); if(lighted){ //remove(int index)返回的是移走的那个元素 System.out.println(vehicles.remove(0)+"----正在穿过路口"); } } } }, 0, 1, TimeUnit.SECONDS); } }
2.交通灯Lamp类
/* 每个灯,含有三个属性:是否亮(lighted),对面的灯(opposite),下一个要亮的灯(nextLamp); 三个方法:是否亮(boolean lighted),灯转亮(light()),灯熄灭(breakout()) */ public enum Lamp { //四个主控制灯,交替顺序:先南向北,然后左转,再东向西,然后左转 S2N("S2W","N2S",false),S2W("E2W","N2E",false),E2W("E2S","W2E",false),E2S("S2N","W2N",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 boolean lighted; private String opposite; private String next; private Lamp(String next,String opposite, boolean lighted) { this.next = next; this.opposite = opposite; this.lighted = lighted; } public boolean isLighted() { return lighted; } public void light(){ this.lighted=true; if(this.opposite!=null){ Lamp.valueOf(opposite).light(); } System.out.println(name() + " is Green,下边应该有6个方向能够看到汽车通过"); } public Lamp breakout(){ this.lighted=false; if(opposite!=null){//应该判断是否为空,以免陷入死循环 Lamp.valueOf(opposite).breakout(); } Lamp nextLamp =null; if(next!=null){//应该判断是否为空,以免陷入死循环 nextLamp=Lamp.valueOf(next); nextLamp.light(); System.out.println("绿灯从"+name()+"--------->切换为"+next); } return nextLamp; } }
3.交通灯控制器LampController类:
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /* 交通灯控制器,记录当前亮的灯 */ public class LampController { private Lamp currentLamp=Lamp.S2N; LampController() { super(); currentLamp.light(); //定期将当前亮着的灯熄灭。 ScheduledExecutorService constrollTimer =Executors.newScheduledThreadPool(1); constrollTimer.scheduleAtFixedRate( new Runnable(){ @Override public void run() { currentLamp = currentLamp.breakout(); } }, 10, //这里要延迟10s,否则一开始会有8条路的绿灯都是亮的,而是将只应该有4条 10, TimeUnit.SECONDS); } }
4.运行启动类:MainClass
public class MainClass { public static void main(String[] args) { //生成十二条路线 String[] roadNames={"S2N","S2W","E2W","E2S", "N2S","N2E","W2E","W2N", "S2E","E2N","N2W","W2S"};
for(int x=0;x<12;x++){ new Road(roadNames[x]); } //启动交通灯控制器 new LampController(); }
}
5.运行结果:
N2S is Green,下边应该有6个方向能够看到汽车通过
S2N is Green,下边应该有6个方向能够看到汽车通过
S2E_0----正在穿过路口
N2S_0----正在穿过路口
N2W_0----正在穿过路口
N2S_1----正在穿过路口
S2E_1----正在穿过路口
W2S_0----正在穿过路口
S2N_0----正在穿过路口
S2E_2----正在穿过路口
N2S_2----正在穿过路口
N2W_1----正在穿过路口
E2N_0----正在穿过路口
N2E is Green,下边应该有6个方向能够看到汽车通过
S2W is Green,下边应该有6个方向能够看到汽车通过
绿灯从S2N--------->切换为S2W
S2W_0----正在穿过路口
N2E_0----正在穿过路口
N2E_1----正在穿过路口
S2W_1----正在穿过路口
N2E_2----正在穿过路口
S2E_3----正在穿过路口
W2S_1----正在穿过路口
S2E_4----正在穿过路口
N2W_2----正在穿过路口
E2N_1----正在穿过路口
S2E_5----正在穿过路口
W2S_2----正在穿过路口
W2E is Green,下边应该有6个方向能够看到汽车通过
E2W is Green,下边应该有6个方向能够看到汽车通过
绿灯从S2W--------->切换为E2W
E2W_0----正在穿过路口
W2E_0----正在穿过路口
E2W_1----正在穿过路口
W2E_1----正在穿过路口
E2N_2----正在穿过路口
N2W_3----正在穿过路口
E2W_2----正在穿过路口
W2E_2----正在穿过路口
W2E_3----正在穿过路口
E2N_3----正在穿过路口
S2E_6----正在穿过路口
E2W_3----正在穿过路口
E2N_4----正在穿过路口
W2E_4----正在穿过路口
S2E_7----正在穿过路口
E2W_4----正在穿过路口
W2E_5----正在穿过路口
W2S_3----正在穿过路口
W2N is Green,下边应该有6个方向能够看到汽车通过
E2S is Green,下边应该有6个方向能够看到汽车通过
绿灯从E2W--------->切换为E2S
E2S_0----正在穿过路口
W2N_0----正在穿过路口
N2W_4----正在穿过路口
E2S_1----正在穿过路口
W2N_1----正在穿过路口
E2S_2----正在穿过路口
W2N_2----正在穿过路口
E2S_3----正在穿过路口
W2N_3----正在穿过路口
E2S_4----正在穿过路口
W2N_4----正在穿过路口
E2S_5----正在穿过路口
W2N_5----正在穿过路口
E2N_5----正在穿过路口
W2N_6----正在穿过路口
W2S_4----正在穿过路口
S2E_8----正在穿过路口
E2N_6----正在穿过路口
W2S_5----正在穿过路口
N2W_5----正在穿过路口
N2S is Green,下边应该有6个方向能够看到汽车通过
S2N is Green,下边应该有6个方向能够看到汽车通过
绿灯从E2S--------->切换为S2N
S2N_1----正在穿过路口
N2S_3----正在穿过路口
------- android培训、java培训、期待与您交流! ----------