杰點

          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 杰點 閱讀(162) 評論(0)  編輯  收藏 所屬分類: JAVA


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          留言簿

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 贺兰县| 香港 | 黔西县| 天柱县| 萍乡市| 苏尼特左旗| 新民市| 宜州市| 定远县| 安陆市| 营山县| 辽阳市| 韶山市| 图木舒克市| 广灵县| 财经| 金寨县| 长泰县| 新民市| 永寿县| 高要市| 凌海市| SHOW| 望城县| 舟山市| 屏边| 桓台县| 上饶县| 额敏县| 全州县| 宣化县| 许昌市| 云林县| 莱阳市| 云龙县| 屏东市| 武安市| 青阳县| 利川市| 谷城县| 武宣县|