java 之 面試題-交通信號燈
不仔細觀察的話,交通信號燈的變化還是一下子描述不清的,神馬左轉(zhuǎn)右轉(zhuǎn),為什么要這樣轉(zhuǎn)。。。。
先看看視頻吧。
下面貼出張老師的代碼:
1 package com.isoftstone.interview.traffic;
2
3 /**
4 * 每個Lamp元素代表一個方向上的燈,總共有12個方向,所有總共有12個Lamp元素。
5 * 有如下一些方向上的燈,每兩個形成一組,一組燈同時變綠或變紅,所以,
6 * 程序代碼只需要控制每組燈中的一個燈即可:
7 * s2n,n2s
8 * s2w,n2e
9 * e2w,w2e
10 * e2s,w2n
11 * s2e,n2w
12 * e2n,w2s
13 * 上面最后兩行的燈是虛擬的,由于從南向東和從西向北、以及它們的對應(yīng)方向不受紅綠燈的控制,
14 * 所以,可以假想它們總是綠燈。
15 * @author 張孝祥 www.it315.org
16 *
17 */
18 /**/
19
20 public enum Lamp {
21 /*每個枚舉元素各表示一個方向的控制燈*/
22 S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
23 /*下面元素表示與上面的元素的相反方向的燈,它們的“相反方向燈”和“下一個燈”應(yīng)忽略不計!*/
24 N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
25 /*由南向東和由西向北等右拐彎的燈不受紅綠燈的控制,所以,可以假想它們總是綠燈*/
26 S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
27
28 private Lamp(String opposite,String next,boolean lighted){
29 this.opposite = opposite;
30 this.next = next;
31 this.lighted = lighted;
32 }
33
34
35 /*當(dāng)前燈是否為綠*/
36 private boolean lighted;
37 /*與當(dāng)前燈同時為綠的對應(yīng)方向*/
38 private String opposite;
39 /*當(dāng)前燈變紅時下一個變綠的燈*/
40 private String next;
41 public boolean isLighted(){
42 return lighted;
43 }
44
45 /**
46 * 某個燈變綠時,它對應(yīng)方向的燈也要變綠
47 */
48 public void light(){
49 this.lighted = true;
50 if(opposite != null){
51 Lamp.valueOf(opposite).light();
52 }
53 System.out.println(name() + " lamp is green,下面總共應(yīng)該有6個方向能看到汽車穿過!");
54
55 }
56
57 /**
58 * 某個燈變紅時,對應(yīng)方向的燈也要變紅,并且下一個方向的燈要變綠
59 * @return 下一個要變綠的燈
60 */
61 public Lamp blackOut(){
62 this.lighted = false;
63 if(opposite != null){
64 Lamp.valueOf(opposite).blackOut();
65 }
66
67 Lamp nextLamp= null;
68 if(next != null){
69 nextLamp = Lamp.valueOf(next);
70 System.out.println("綠燈從" + name() + "-------->切換為" + next);
71 nextLamp.light();
72 }
73 return nextLamp;
74 }
75 }
1 package com.isoftstone.interview.traffic;
2
3 import java.util.concurrent.Executors;
4 import java.util.concurrent.ScheduledExecutorService;
5 import java.util.concurrent.TimeUnit;
6
7 public class LampController {
8 private Lamp currentLamp;
9
10 public LampController(){
11 //剛開始讓由南向北的燈變綠;
12 currentLamp = Lamp.S2N;
13 currentLamp.light();
14
15 /*每隔10秒將當(dāng)前綠燈變?yōu)榧t燈,并讓下一個方向的燈變綠*/
16 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
17 timer.scheduleAtFixedRate(
18 new Runnable(){
19 public void run(){
20 System.out.println("來啊");
21 currentLamp = currentLamp.blackOut();
22 }
23 },
24 10,
25 10,
26 TimeUnit.SECONDS);
27 }
28 }
1 package com.isoftstone.interview.traffic;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Random;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.Executors;
8 import java.util.concurrent.ScheduledExecutorService;
9 import java.util.concurrent.TimeUnit;
10
11 /**
12 * 每個Road對象代表一條路線,總共有12條路線,即系統(tǒng)中總共要產(chǎn)生12個Road實例對象。
13 * 每條路線上隨機增加新的車輛,增加到一個集合中保存。
14 * 每條路線每隔一秒都會檢查控制本路線的燈是否為綠,是則將本路線保存車的集合中的第一輛車移除,即表示車穿過了路口。
15 * @author 張孝祥 www.it315.org
16 *
17 */
18 public class Road {
19 private List<String> vechicles = new ArrayList<String>();
20
21 private String name =null;
22 public Road(String name){
23 this.name = name;
24
25 //模擬車輛不斷隨機上路的過程
26 ExecutorService pool = Executors.newSingleThreadExecutor();
27 pool.execute(new Runnable(){
28 public void run(){
29 for(int i=1;i<1000;i++){
30 try {
31 Thread.sleep((new Random().nextInt(10) + 1) * 1000);
32 } catch (InterruptedException e) {
33 e.printStackTrace();
34 }
35 vechicles.add(Road.this.name + "_" + i);
36 }
37 }
38
39 });
40
41 //每隔一秒檢查對應(yīng)的燈是否為綠,是則放行一輛車
42 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
43 timer.scheduleAtFixedRate(
44 new Runnable(){
45 public void run(){
46 if(vechicles.size()>0){
47 boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
48 if(lighted){
49 System.out.println(vechicles.remove(0) + " is traversing !");
50 }
51 }
52
53 }
54 },
55 1,
56 1,
57 TimeUnit.SECONDS);
58
59 }
60 }
1 package com.isoftstone.interview.traffic;
2
3 public class MainClass {
4
5 /**
6 * @param args
7 */
8 public static void main(String[] args) {
9
10 /*產(chǎn)生12個方向的路線*/
11 String [] directions = new String[]{
12 "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
13 };
14 for(int i=0;i<directions.length;i++){
15 new Road(directions[i]);
16 }
17
18 /*產(chǎn)生整個交通燈系統(tǒng)*/
19 new LampController();
20 }
21
22 }
浙公網(wǎng)安備 33010602011771號