您的位置:首页 > 职场人生

黑马程序员--Java7K破解题---交通灯

2014-03-26 17:56 471 查看
切不可空想,一定要画图!

面向对象:红绿灯、红绿灯的控制系统、汽车、路线。

面向对象的重要经验:谁拥有数据,谁就对外提供操作这些数据的方法。

例子:人在黑板上画圆。(person、blackboard,circle三个对象,画圆的动作存在圆上面。

小球从绳子的一端滚到另一端的面向对象设计:

class Rope{
private Point start;
private Point end;
public Rope(Point start,Point end){
this.start=start;
this.end=end;
}
public Point nexPoint(Point currentPoint){}
}
class Ball{
Private Rope rope;
private Point currentPoint;
public Ball(Rope rope,startPoint){
this.rope=rope;
this.currentPoint=statPoint;
}
public void move(){
currentPoint=rope.nextPoint(currentPoint);
System.out.prrintln("小球移动到了"+currentPoint);
}
}


Lamp类表示一个交通灯。(12灯。)使用枚举创建。

public enum Lamp {
S2N("N2S",false,"S2W"),S2W("N2E",false,"E2W"),
E2W("W2E",false,"E2S"),E2S("W2N",false,"S2N"),

S2E("N2W",true,"S2E"),E2N("W2S",true,"E2n"),

N2S(null,false,null),N2E(null,false,null),W2E(null,false,null),
W2N(null,false,null),N2W(null,false,null),W2S(null,false,null);

private Lamp(String opposite,boolean lighted,String next){
this.opposite=opposite;
this.next=next;
this.lighted=lighted;
}
private Lamp(){};

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();
//opposite.light();
}
System.out.println(name()+"lamp is green 下面有六路车通过");
}
public Lamp blackOut(){
this.lighted=false;
if(opposite !=null){
Lamp.valueOf(opposite).blackOut();
//opposite.light();
}
Lamp nextLamp=null;
if (next !=null){
nextLamp=Lamp.valueOf(next);
System.out.println("l绿灯从"+name()+"----->切换为"+next);
nextLamp.light();

}
return nextLamp;
}

}


线路的代码:

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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
vechicles.add(Road.this.name+"_"+i);
}
}
});

ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
if(vechicles.size()>0){
boolean lighted=Lamp.valueOf(Road.this.name).isLighted();
if(lighted==true){
System.out.println(	vechicles.remove(0)+"  is travling");
}
}

}
},
1,
1,
TimeUnit.SECONDS);
}
}
交通灯控制器代码:

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);

}

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