首先是交通燈項目:
思路分析:
十字路口交通燈,這個問題聯繫實際生活,思考起來還算是好理解。
首先考慮面向對象設計思想分析問題:
對象: (1)紅綠燈
(2)控制紅綠燈的控制器
(3)汽車
(4)路
分析:汽車看到自己所在的路線對應的紅綠燈綠了就穿過路口?不是。
需要分析前面到底有沒有車。
如何知道前面有沒有車呢?問路。
路中存儲着車輛的集合,顯然路上就有增加車輛和減少車輛的方法。
注意面向對象設計依據,誰擁有數據,誰就對外提供操作這些數據的方法。
整個十字路口,相當於有12條路線。
(1)四條右拐。S2E, E2N, N2W, W2S,這四條線路相當於綠燈常亮。
(2)四條直行。S2N, N2S, E2W, W2E
(3)四條左拐。S2W, E2S, N2E, W2N
將(2)(3)合併,選出兩組代表:
S2N,S2W :
E2W,E2S
思路清晰後開始寫代碼:
首先是路:
publicclass Roader {
List<String> vechicles =new ArrayList<String>();
//定義路的名字
private Stringname;
public Roader(String name){
this.name = name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
publicvoid run(){
//隨機產生車
for(int i =1;i<1000;i++){
try {
Thread.sleep((new Random().nextInt(10) + 1)*100);
}catch (InterruptedException e) {
e.printStackTrace();
}
//可以將name變爲final
//vechicles.add(name+"-"+i);
vechicles.add(Roader.this.name+"-"+i);
}
}
});
//定時器每隔一秒判斷一下燈是不是綠燈,如果是,則去掉一輛車
ScheduledExecutorService timer= Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
publicvoid run(){
if(vechicles.size()>0){
boolean lighted=Lamp.valueOf(Roader.this.name).isLighted();
if(lighted)
System.out.println(vechicles.remove(0)+" is traversing !");
}
}
},
1,
1,
TimeUnit.SECONDS);
}
第二 燈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 Lamp(){
}
privatebooleanlighted;
//燈的名字
private Stringopposite;
private Stringnext;
publicboolean isLighted(){
returnlighted;
}
//如果我亮了,我對面的燈也得變亮
publicvoid 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){
nextLamp = Lamp.valueOf(next);
System.out.println("綠燈從"+name()+"-----切換爲"+next);
nextLamp.light();
}
return nextLamp;
}
}
第三燈的控制器LampControllor
publicclass LampController {
private LampcurrentLamp;
public LampController(){
currentLamp = Lamp.S2N;
currentLamp.light();
//十秒後變燈的顏色
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
publicvoid run(){
System.out.println("來了");
currentLamp =currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
最後主函數
publicclass MainClass {
publicstaticvoid 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 Roader(directions[i]);
}
//創建燈控制器線程
new LampController();
}
}
---------------------- android培訓、java培訓、期待與您交流! ----------------------詳細請查看:http://edu.csdn.net/heima