1. 參加張老師的交通燈系統的需求分析,對象有: 路,燈,燈控制器。
燈 (用枚舉實現)
package com.isoftstone.interview.traffic;
/**
* 每個Lamp元素代表一個方向上的燈,總共有12個方向,所有總共有12個Lamp元素。
* 有如下一些方向上的燈,每兩個形成一組,一組燈同時變綠或變紅,所以,
* 程序代碼只需要控制每組燈中的一個燈即可:
* s2n,n2s
* s2w,n2e
* e2w,w2e
* e2s,w2n
* s2e,n2w
* e2n,w2s
* 上面最後兩行的燈是虛擬的,由於從南向東和從西向北、以及它們的對應方向不受紅綠燈的控制,
* 所以,可以假想它們總是綠燈。
* @author 張孝祥 www.it315.org
*
*/
/**/
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 Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
/*當前燈是否爲綠*/
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();
}
System.out.println(name() + " lamp is green,下面總共應該有6個方向能看到汽車穿過!");
}
/**
* 某個燈變紅時,對應方向的燈也要變紅,並且下一個方向的燈要變綠
* @return 下一個要變綠的燈
*/
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;
}
}
路
package com.isoftstone.interview.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;
/**
* 每個Road對象代表一條路線,總共有12條路線,即系統中總共要產生12個Road實例對象。
* 每條路線上隨機增加新的車輛,增加到一個集合中保存。
* 每條路線每隔一秒都會檢查控制本路線的燈是否爲綠,是則將本路線保存車的集合中的第一輛車移除,即表示車穿過了路口。
* @author 張孝祥 www.it315.org
*
*/
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) {
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){
System.out.println(vechicles.remove(0) + " is traversing !");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
燈控制器
package com.isoftstone.interview.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();
/*每隔10秒將當前綠燈變爲紅燈,並讓下一個方向的燈變綠*/
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
System.out.println("來啊");
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
測試類
package com.isoftstone.interview.traffic;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
/*產生12個方向的路線*/
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 Road(directions[i]);
}
/*產生整個交通燈系統*/
new LampController();
}
}