---------------------- android培训、java培训、期待与您交流! ----------------------
自己先写了一个试试.
大概想到有几个重要的类
Direction方向
Car车子
Lamp灯
从Direction类入手
一开始理解有点错误
我以为每个方向只有一个车道,所以只定义了1个ArrayList
这个方向的车子全放在这个ArrayList
后来看视频才知道前往3个方向的车子应该占用不同的车道
也就是放进不同的ArrayList中
所以在原来的代码中给Direction类加了一个方向属性turn
class Direction{
private static final long TIME_CREATECAR=5000;
public ArrayList<Car> al; //该方向的车道,先进先出的队列
public Lamp lamp; //该方向上的灯
public Turn turn; //该方向的转向方向(好拗口...)
private String name; //"东","南","西","北"
private final Timer timer; //这个计时器用于创建该车道的新车子
public Direction(int turn,int name){
if(turn==0) //通过传递进来的参数初始化变量
this.turn=Turn.STRAIGHT;
else if(turn==1)
this.turn=Turn.LEFT;
else
this.turn=Turn.RIGHT;
if(name==0)
this.name="南";
else if(name==1)
this.name="东";
else if(name==2)
this.name="北";
else
this.name="西";
al=new ArrayList<Car>();
if(this.turn!=Turn.RIGHT) //这里是初始化交通灯的初始状态
lamp=new Lamp((name%2)*2-turn<0?3:(name%2)*2-turn);
else
lamp=new Lamp(5);
new Thread(new Runnable(){
public void run() {
while(true){
if(!al.isEmpty()){
Car c=al.get(0); //从队列首位拿一个车子出来让它过去
if(lamp.getColor()==Color.GREEN){
al.remove(c);
String str=c.dir+" 方的一辆车 "+c.dir.turn+" 通过十字路口";
System.out.println(str);
try {
TrafficLamp.bufw.write(str);
TrafficLamp.bufw.newLine();
TrafficLamp.bufw.flush();
}catch(IOException e){
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}).start();
timer=new Timer();
class TimerAction extends TimerTask{ //创建新车子
private Direction dir;
public TimerAction(Direction dir){
super();
this.dir=dir;
}
@Override
public void run() {
new Car(dir);
String str=dir+" 方一新车辆前来,将要 "+dir.turn;
System.out.println(str);
try {
TrafficLamp.bufw.write(str);
TrafficLamp.bufw.newLine();
TrafficLamp.bufw.flush();
}catch(IOException e){
e.printStackTrace();
}
this.cancel();
timer.schedule(new TimerAction(dir),TIME_CREATECAR+(long)(Math.random()*TIME_CREATECAR));
}
}
timer.schedule(new TimerAction(this),(long)(Math.random()*TIME_CREATECAR));
}
public String toString(){ //覆盖toString,方便显示到log
return name;
}
}
enum Color{
RED,GREEN;
}
class Lamp{
private static final long TIME_CHANGELAMP=15000;
private Color c;
private int stage;
private Timer t;
/*
Lamp有一个状态的属性
每个Lamp
不管是哪个方向
哪个转向方向
都是绿红红红这样的循环
所以定义4个状态0,1,2,3
然后一直走就可以了
状态5特指右转车道 一直为绿
*/
public Lamp(int stage){
this.stage=stage;
if(stage==0||stage==5)
c=Color.GREEN;
else
c=Color.RED;
if(stage!=5){
t=new Timer();
t.schedule(new TimerTask(){
@Override
public void run() {
nextStage();
}
},TIME_CHANGELAMP,TIME_CHANGELAMP);
}
}
public Color getColor(){
return c;
}
public void nextStage(){
stage++;
if(stage==1){
c=Color.RED;
}else if(stage==4){
c=Color.GREEN;
stage=0;
}
}
}
enum Turn{ //枚举3个转向方向
LEFT,RIGHT,STRAIGHT;
public String toString(){
if(this==LEFT)
return "左转弯";
else if (this==RIGHT)
return "右转弯";
else
return "直行";
}
}
class Car{ //Car对象创建时绑定到车道上
public Direction dir;
public Car(Direction dir){
this.dir=dir;
dir.al.add(this);
}
}
public class TrafficLamp {
public static BufferedWriter bufw=null;
public void init(){
try {
bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("traffic.log")));
}catch(FileNotFoundException e){
e.printStackTrace();
}
for(int i=0;i<12;i++){
new Direction(i%3,i/3);
}
}
public static void main(String[] args){
new TrafficLamp().init();
}
}
最后效果感觉蛮不错,不过感觉代码还是有点臃肿,有不少看起来不太好的判断和数据绑定。
而且想到哪里写到哪里,感觉没有贯彻面向对象的思想。
比如输出到log文件,应该抽出一个函数的,直接加在各个地方不好。
详细请查看: http://edu.csdn.net/heima