交通系统设计思路:
一:设计路(road)----
一:该路的名字
二:路上有很多的车,不停的在路上添上车的数量
二:设计灯(lamp)----一共有十二个灯
灯的特点: 1.顺序问题:从① -->②-à③à④ à①一直循环着
2.特殊的灯:有S2E , W2S , N2W , E2N 在图中可以看到它们没有受到红绿灯的影响,车辆可以总是行走。
3.对称问题:比如从南到北的灯亮着,那从北到南的灯当然也是亮着的。所以对称灯有四组:
S2N --> N2S W2E -->E2W
S2W--> S2E W2N-->E2S
4.灯的类型:红色,绿色
所以可以看到其实只要考虑8组灯就可以了,分别是特殊灯4组,对称灯4组。
对象设计思路:从灯的特点上看 灯需要有成员变量:对应的对称灯,下一个灯,灯的颜色。
三: 设计灯的控制器—>
一:控制灯当前的颜色由绿变成红
二:切换成下一个灯为绿色
四:主方法: 先生成12条路;
然后启动灯控制器;
下面是我自己敲的代码,按着视频看了两遍有了上面的总结后自己敲的。
Road类
package cn.itcast.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=null;
public Road(String name){
this.name=name;
//随机在该路上生成车辆的到来
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
@Override
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(){
@Override
public void run() {
if(vechicles.size()>0){
boolean lighted=Lamp.valueOf(Road.this.name).isLighted();
if(lighted){
System.out.println(vechicles.remove(0)+" is traversing!");
}
}
}},
1,
1,
TimeUnit.SECONDS);
}
}
Lamp类
package cn.itcast.traffic;
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 boolean lighted;
private String opposite;
private String next;
private Lamp(String opposite,String next,boolean lighted){
this.opposite=opposite;
this.next=next;
this.lighted=lighted;
}
private Lamp(){}
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, 下面总应该有六个方向能看到车辆穿过!");
}
public Lamp blackOut(){
this.lighted=false;
if(opposite!=null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp=null;
if(next!=null){
System.out.println("绿灯从 "+name()+"------->切换到 "+next);
nextLamp=Lamp.valueOf(next);
nextLamp.light();
}
return nextLamp;
}
}
LampController类
package cn.itcast.traffic;
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 pool = Executors.newScheduledThreadPool(1);
pool.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
currentLamp=currentLamp.blackOut();
}
}, 10, 10, TimeUnit.SECONDS);
}
}
最后是测试类
package cn.itcast.traffic;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
String[] directors=new String[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
for(int i=0;i<directors.length;i++){
new Road(directors[i]);
}
new LampController();
}
}