交通燈
交通燈的核心是:由路來(lái)生成車,并且檢測(cè)燈的顏色來(lái)放行汽車。抽象了現(xiàn)實(shí)的隨機(jī)車輛行駛。燈的數(shù)量是確定的,使用了枚舉。并且由車的通行對(duì)稱性,得出對(duì)稱的燈顏色需要一致。并使用定時(shí)器設(shè)置燈的點(diǎn)亮。
1 //抽象出所有的燈,共12個(gè)燈
2 package traffic;
3
4 //每個(gè)方向都有一個(gè)燈,因?yàn)檐囕v行駛是對(duì)稱結(jié)構(gòu), 并且對(duì)稱的等的顯示顏色是一樣的
5
6 public enum Lamp {
7 //元素各表示一個(gè)控制方向的燈
8 S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
9
10 N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
11
12 //這幾個(gè)方向已經(jīng)包含在了上面定義的方向中
13 S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
14
15 private Lamp(String opposite,String next,boolean lighted){
16 this.opposite = opposite;
17 this.next = next;
18 this.lighted = lighted;
19 }
20
21
22
23 private boolean lighted;
24
25 private String opposite;
26
27 private String next;
28 public boolean isLighted(){
29 //是否綠燈
30 return lighted;
31 }
32
33 //對(duì)稱方向的燈顏色要一致
34 public void light(){
35 this.lighted = true;
36 if(opposite != null){
37 Lamp.valueOf(opposite).light();
38 }
39 System.out.println(name() + " lamp is green,有6個(gè)方向可以看到車通過(guò)");
40
41 }
42
43 // 對(duì)稱的燈顏色一致 并且點(diǎn)綠下一個(gè)燈
44 public Lamp blackOut(){
45 this.lighted = false;
46 if(opposite != null){
47 Lamp.valueOf(opposite).blackOut();
48 }
49
50 Lamp nextLamp= null;
51 if(next != null){
52 nextLamp = Lamp.valueOf(next);
53 System.out.println("綠燈?" + name() + "切換為" + next);
54 nextLamp.light();
55 }
56 return nextLamp;
57 }
58 }
59
2 package traffic;
3
4 //每個(gè)方向都有一個(gè)燈,因?yàn)檐囕v行駛是對(duì)稱結(jié)構(gòu), 并且對(duì)稱的等的顯示顏色是一樣的
5
6 public enum Lamp {
7 //元素各表示一個(gè)控制方向的燈
8 S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
9
10 N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
11
12 //這幾個(gè)方向已經(jīng)包含在了上面定義的方向中
13 S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
14
15 private Lamp(String opposite,String next,boolean lighted){
16 this.opposite = opposite;
17 this.next = next;
18 this.lighted = lighted;
19 }
20
21
22
23 private boolean lighted;
24
25 private String opposite;
26
27 private String next;
28 public boolean isLighted(){
29 //是否綠燈
30 return lighted;
31 }
32
33 //對(duì)稱方向的燈顏色要一致
34 public void light(){
35 this.lighted = true;
36 if(opposite != null){
37 Lamp.valueOf(opposite).light();
38 }
39 System.out.println(name() + " lamp is green,有6個(gè)方向可以看到車通過(guò)");
40
41 }
42
43 // 對(duì)稱的燈顏色一致 并且點(diǎn)綠下一個(gè)燈
44 public Lamp blackOut(){
45 this.lighted = false;
46 if(opposite != null){
47 Lamp.valueOf(opposite).blackOut();
48 }
49
50 Lamp nextLamp= null;
51 if(next != null){
52 nextLamp = Lamp.valueOf(next);
53 System.out.println("綠燈?" + name() + "切換為" + next);
54 nextLamp.light();
55 }
56 return nextLamp;
57 }
58 }
59
1 //燈控制器,根據(jù)車輛通行的對(duì)稱性可知,對(duì)稱方向的燈要顏色一致
2 package traffic;
3
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.TimeUnit;
7
8 public class LampController {
9 private Lamp currentLamp;
10
11 public LampController(){
12 //剛開(kāi)始讓由南向北的燈變綠;
13 currentLamp = Lamp.S2N;
14 currentLamp.light();
15
16 //每隔10秒將燈變紅 點(diǎn)亮下一個(gè)綠燈
17 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
18 timer.scheduleAtFixedRate(
19 new Runnable(){
20 public void run(){
21 currentLamp = currentLamp.blackOut();
22 }
23 },
24 10,
25 10,
26 TimeUnit.SECONDS);
27 }
28 }
29
2 package traffic;
3
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.TimeUnit;
7
8 public class LampController {
9 private Lamp currentLamp;
10
11 public LampController(){
12 //剛開(kāi)始讓由南向北的燈變綠;
13 currentLamp = Lamp.S2N;
14 currentLamp.light();
15
16 //每隔10秒將燈變紅 點(diǎn)亮下一個(gè)綠燈
17 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
18 timer.scheduleAtFixedRate(
19 new Runnable(){
20 public void run(){
21 currentLamp = currentLamp.blackOut();
22 }
23 },
24 10,
25 10,
26 TimeUnit.SECONDS);
27 }
28 }
29
1 //由路產(chǎn)生車,并檢測(cè)燈的顏色,放行汽車
2 package traffic;
3
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Random;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ScheduledExecutorService;
10 import java.util.concurrent.TimeUnit;
11
12 //road 產(chǎn)生車輛 ,一個(gè)方向?qū)?yīng)3條路, 共有4個(gè)方向 12條路
13 public class Road {
14 private List<String> vechicles = new ArrayList<String>();
15
16 private String name =null;
17 public Road(String name){
18 this.name = name;
19
20 //模擬車輛
21 ExecutorService pool = Executors.newSingleThreadExecutor();
22 pool.execute(new Runnable(){
23 public void run(){
24 for(int i=1;i<1000;i++){
25 try {
26 Thread.sleep((new Random().nextInt(10) + 1) * 1000);
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 vechicles.add(Road.this.name + "_" + i);
31 }
32 }
33
34 });
35
36 //每隔一秒檢查燈
37 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
38 timer.scheduleAtFixedRate(
39 new Runnable(){
40 public void run(){
41 if(vechicles.size()>0){
42 boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
43 if(lighted){
44 System.out.println(vechicles.remove(0) + " is traversing !");
45 }
46 }
47
48 }
49 },
50 1,
51 1,
52 TimeUnit.SECONDS);
53
54 }
55 }
56
2 package traffic;
3
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Random;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ScheduledExecutorService;
10 import java.util.concurrent.TimeUnit;
11
12 //road 產(chǎn)生車輛 ,一個(gè)方向?qū)?yīng)3條路, 共有4個(gè)方向 12條路
13 public class Road {
14 private List<String> vechicles = new ArrayList<String>();
15
16 private String name =null;
17 public Road(String name){
18 this.name = name;
19
20 //模擬車輛
21 ExecutorService pool = Executors.newSingleThreadExecutor();
22 pool.execute(new Runnable(){
23 public void run(){
24 for(int i=1;i<1000;i++){
25 try {
26 Thread.sleep((new Random().nextInt(10) + 1) * 1000);
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 vechicles.add(Road.this.name + "_" + i);
31 }
32 }
33
34 });
35
36 //每隔一秒檢查燈
37 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
38 timer.scheduleAtFixedRate(
39 new Runnable(){
40 public void run(){
41 if(vechicles.size()>0){
42 boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
43 if(lighted){
44 System.out.println(vechicles.remove(0) + " is traversing !");
45 }
46 }
47
48 }
49 },
50 1,
51 1,
52 TimeUnit.SECONDS);
53
54 }
55 }
56
1 //主類
2 package traffic;
3
4 public class MainClass {
5
6
7 public static void main(String[] args) {
8
9 //生成12條路
10 String [] directions = new String[]{
11 "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
12 };
13 for(int i=0;i<directions.length;i++){
14 new Road(directions[i]);
15 }
16
17 //生成交通燈,開(kāi)始自動(dòng)運(yùn)行
18 new LampController();
19 }
20
21 }
22
2 package traffic;
3
4 public class MainClass {
5
6
7 public static void main(String[] args) {
8
9 //生成12條路
10 String [] directions = new String[]{
11 "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
12 };
13 for(int i=0;i<directions.length;i++){
14 new Road(directions[i]);
15 }
16
17 //生成交通燈,開(kāi)始自動(dòng)運(yùn)行
18 new LampController();
19 }
20
21 }
22
posted on 2011-01-11 11:08 杰點(diǎn) 閱讀(162) 評(píng)論(0) 編輯 收藏 所屬分類: JAVA