黑馬程序員--交通燈管理系統


-------android培訓java培訓、期待與您交流! ----------                   -------android培訓java培訓、期待與您交流! ---------- 

 

交通燈管理系統項目需求

模擬實現十字路口的交通燈管理系統邏輯,具體需求如下:

異步隨機生成按照各個路線行駛的車輛

信號燈忽略黃燈,只考慮紅燈和綠燈
應考慮左轉車倆控制信號燈,右轉車倆不受信號燈控制
具體信號燈控制邏輯與現實生活中普通交通燈邏輯相同,不考慮特殊情況下的控制邏輯
每輛車通過路口時間爲1秒(提示,可通過縣城Sleep的方式模擬)
隨即生成車輛時間間隔以及紅綠燈交換時間間隔自定,可以設置
不要求實現gui,只考慮系統邏輯實現,可通過log方式展現程序運行結果

面向對象的分析與設計
每條路線上都會出現多輛車,路線上要隨即增加新的車,在燈綠期間還要每秒鐘減少一輛車
設計一個Road類來表示路線,每個Road對象代表一條路線,總共有12條路線,即系統中總共產生12個Road實例對象
每條路線上隨機增加新的車輛,增加到一個集合中保存
每條路線上每隔一秒都會查看控制本路線的燈是否爲綠,是則將本路線保存車的稽覈中的第一輛車移除,即表示車穿過了路口

每條路線每隔一秒都會檢查在控制本路線的燈是否爲綠,一個燈由綠變紅時,應該將下一個方向的燈變綠
設計一個Lamp類來表示一個交通燈,每個交通燈維護一個狀態,亮(綠),或不亮(紅)每個交通燈要有變亮和變黑的方法,並且要有自己
的亮黑狀態
總共有12條路線,所以,系統中總共要產生12個交通燈,右拐彎的路線本來不受燈的控制,但是爲了讓程序採用統一的處理方式,故假設出有四個右拐彎的燈
,之是這些燈爲常亮狀態
除了右拐彎方向的去他8條路線的燈,他們是倆倆成對兒的,可以分爲4組,所以,在編程處理時,只要從這4組中各取出一個燈,對這四個燈依次輪流變亮
與這4個燈對應的燈則隨之一同變化,因此Lamp類匯要有一個變量來記住自己相反方向的燈,在一個Lamp對象的變亮和變黑方法中
將對應方向的燈也變亮和變黑,每隔燈變黑時,都伴隨着下一個燈的變亮

  1. package com.heima.traffic;
  2. public enum Lamp {
  3. S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
  4. N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
  5. S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
  6. private Lamp(String opposite,String next,boolean lighted){
  7. this.opposite = opposite;
  8. this.next = next;
  9. this.lighted = lighted;
  10. }
  11. private boolean lighted;
  12. private String opposite;
  13. private String next;
  14. public boolean isLighted(){
  15. return lighted;
  16. }
  17. public void light(){
  18. this.lighted = true;
  19. if(opposite != null){
  20. Lamp.valueOf(opposite).light();
  21. }
  22. System.out.println(name() + " lamp is green,下面總共應該有6個方向能看到汽車穿過!");
  23. }
  24. public Lamp blackOut(){
  25. this.lighted = false;
  26. if(opposite != null){
  27. Lamp.valueOf(opposite).blackOut();
  28. }
  29. Lamp nextLamp= null;
  30. if(next != null){
  31. nextLamp = Lamp.valueOf(next);
  32. System.out.println("綠燈從" + name() + "-------->切換爲" + next);
  33. nextLamp.light();
  34. }
  35. return nextLamp;
  36. }
  37. }


LampController

  1. package com.heima.traffic;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.ScheduledExecutorService;
  4. import java.util.concurrent.TimeUnit;
  5. public class LampController {
  6. private Lamp currentLamp;
  7. public LampController(){
  8. //剛開始讓由南向北的燈變綠;
  9. currentLamp = Lamp.S2N;
  10. currentLamp.light();
  11. ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
  12. timer.scheduleAtFixedRate(
  13. new Runnable(){
  14. public void run(){
  15. System.out.println("來啊");
  16. currentLamp = currentLamp.blackOut();
  17. }
  18. },
  19. 10,
  20. 10,
  21. TimeUnit.SECONDS);
  22. }
  23. }


MainClass類

  1. package com.heima.traffic;
  2. public class MainClass {
  3. public static void main(String[] args) {
  4. String [] directions = new String[]{
  5. "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
  6. };
  7. for(int i=0;i<directions.length;i++){
  8. new Road(directions[i]);
  9. }
  10. new LampController();
  11. }
  12. }


Road類

  1. package com.heima.traffic;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Random;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.ScheduledExecutorService;
  8. import java.util.concurrent.TimeUnit;
  9. public class Road {
  10. private List<String> vechicles = new ArrayList<String>();
  11. private String name =null;
  12. public Road(String name){
  13. this.name = name;
  14. //模擬車輛不斷隨機上路的過程
  15. ExecutorService pool = Executors.newSingleThreadExecutor();
  16. pool.execute(new Runnable(){
  17. public void run(){
  18. for(int i=1;i<1000;i++){
  19. try {
  20. Thread.sleep((new Random().nextInt(10) + 1) * 1000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. vechicles.add(Road.this.name + "_" + i);
  25. }
  26. }
  27. });
  28. //每隔一秒檢查對應的燈是否爲綠,是則放行一輛車
  29. ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
  30. timer.scheduleAtFixedRate(
  31. new Runnable(){
  32. public void run(){
  33. if(vechicles.size()>0){
  34. boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
  35. if(lighted){
  36. System.out.println(vechicles.remove(0) + " is traversing !");
  37. }
  38. }
  39. }
  40. }1,
  41. 1,
  42. TimeUnit.SECONDS);
  43. TimeUnit.SECONDS);
  44. }
  45. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章