杰点

hello java

交通灯

交通灯的核心是:由路来生成车,并且检测灯的颜色来放行汽车。抽象了现实的随机车辆行驶。

灯的数量是确定的,使用了枚举。并且由车的通行对称性,得出对称的灯颜色需要一致。并使用定时器设置灯的点亮。


 1 //抽象出所有的灯,共12个灯
 2 package traffic;
 3 
 4 //每个方向都有一个灯,因为车辆行驶是对称结构, 并且对称的等的显示颜色是一样的
 5 
 6 public enum Lamp {
 7     //元素各表示一个控制方向的灯    
 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     //这几个方向已经包含在了上面定义的方向中
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     //对称方向的灯颜色要一致
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个方向可以看到车通过");
40         
41     }
42     
43 // 对称的灯颜色一致 并且点绿下一个灯
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 //灯控制器,根据车辆通行的对称性可知,对称方向的灯要颜色一致
 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         //刚开始让由南向北的灯变绿;        
13         currentLamp = Lamp.S2N;
14         currentLamp.light();
15         
16         //每隔10秒将灯变红 点亮下一个绿灯
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 //由路产生车,并检测灯的颜色,放行汽车
 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 产生车辆 ,一个方向对应3条路, 共有4个方向 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         //生成交通灯,开始自动运行    
18         new LampController();
19     }
20 
21 }
22 

posted on 2011-01-11 11:08 杰点 阅读(164) 评论(0)  编辑  收藏 所属分类: JAVA


只有注册用户登录后才能发表评论。


网站导航:
 
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

留言簿

文章分类

文章档案

搜索

最新评论