java 之 面试题交通信号灯

不仔细观察的话,交通信号灯的变化还是一下子描述不清的,神马左转右转,为什么要这样转。。。。

先看看视频吧。

 

下面贴出张老师的代码:

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 * 上面最后两行的灯是虚拟的,由于从南向东和从西向北、以及它们的对应方向不受红绿灯的控制,
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 /*下面元素表示与上面的元素的相反方向的灯,它们的“相反方向灯”和“下一个灯”应忽略不计!*/
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 /*当前灯是否为绿*/
36 private boolean lighted;
37 /*与当前灯同时为绿的对应方向*/
38 private String opposite;
39 /*当前灯变红时下一个变绿的灯*/
40 private String next;
41 public boolean isLighted(){
42 return lighted;
43 }
44
45 /**
46 * 某个灯变绿时,它对应方向的灯也要变绿
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,下面总共应该有6个方向能看到汽车穿过!");
54
55 }
56
57 /**
58 * 某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿
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秒将当前绿灯变为红灯,并让下一个方向的灯变绿*/
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条路线,即系统中总共要产生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 //每隔一秒检查对应的灯是否为绿,是则放行一辆车
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 /*产生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 /*产生整个交通灯系统*/
19 new LampController();
20 }
21
22 }
原文地址:https://www.cnblogs.com/hangxin1940/p/2045381.html