1. 程式人生 > >黑馬程式設計師--第二十七天:交通燈系統

黑馬程式設計師--第二十七天:交通燈系統

---------------------- ASP.Net+Android+IO開發S.Net培訓、期待與您交流! ----------------------

//總體上減少了Lamp列舉的2個成員變數和2個方法以及test類的一個數組。
//LampController 提取了一個lightNextGroup()方法。

package com.itheima.trafficlamp;

public class Test {//該類減少一個數組。

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println("Start");
		Lamp[] lamps = Lamp.values();
		new LampController();
		
		for(Lamp lamp : lamps){
			new Road(lamp);
		}
	}
}


package com.itheima.trafficlamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Road {//模擬車輛的增加和減少進行了提取

	List<String> vehicles = new ArrayList<String>();
	Lamp vName = null;
	
	public Road(Lamp name){
		vName = name;
		addVehicles();
		removeVehicles();
	}


	private void removeVehicles() {//模擬車輛減少
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		timer.scheduleAtFixedRate(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if((!vehicles.isEmpty())&& vName.isLighted())
				System.out.println(vehicles.remove(0)+" is traversing");
			}
		}, 1, 1, TimeUnit.SECONDS);
	}
	
	
	private void addVehicles() {//模擬增加車輛
		ExecutorService addV = Executors.newSingleThreadExecutor();
		addV.execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i =0; i<1000;i++){
					try {
						Thread.sleep((new Random().nextInt(10)+1)*1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					vehicles.add(vName+" ::: "+i);
				}
			}
		});
	}
}


package com.itheima.trafficlamp;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class LampController {//該類提取了一個 lightNextGroup()方法。

	int group = 0;
	Lamp[] lamps = Lamp.values();
	
	public LampController(){
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		timer.scheduleAtFixedRate(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				lightNextGroup();
			}
		}, 0, 10, TimeUnit.SECONDS);
	}
	
	public void lightNextGroup(){//控制當前組的燈變成紅的,下一組燈變為亮的。
		//當前組設為false;
		lamps[group = group%8].setLighted(false);
		lamps[group+1].setLighted(false);
		//下一組設為true;
		group = group+2;
		System.out.println("Lamps of "+lamps[group] +" and "+ lamps[group+1]+" turned to green");
		lamps[group].setLighted(true);
		lamps[group+1].setLighted(true);
	}
}


package com.itheima.trafficlamp;

public enum Lamp {//對Lamp 這個列舉進行了優化,與張孝祥老師的相比 該列舉省去了 
	//opposite 燈,next燈,以及light()方法 和 blackOut()方法。
	S2N___A,N2S___A,//groupA, controlled by controller
	S2W___B,N2E___B,//groupB, controlled by controller
	W2E___C,E2W___C,//groupC, controlled by controller
	W2N___D,E2S___D,//groupD, controlled by controller
	S2E___R(true),E2N___R(true),N2W___R(true),W2S___R(true);
	//groupR, turns right, not controlled by controller, whose lights are always green.
	
	private boolean lighted = false;
	
	public boolean isLighted() {
		return lighted;
	}

	public void setLighted(boolean lighted) {
		this.lighted = lighted;
	}

	Lamp(){};
	
	Lamp(boolean lighted){
		this.lighted = lighted;
	}
}

---------------------- ASP.Net+Android+IO開發S.Net培訓、期待與您交流! ----------------------