交通灯管理系统 学习笔记



设计分析:


系统中有12个方向上的灯,在程序的其他地方要根据灯的名称就可以获得对应的灯的实例对象,综合这些因素,将Lamp类用java5中的枚举形式定义更为简单。
每个Lamp对象中的亮黑状态用lighted变量表示,选用S2N、S2W、E2W、E2N这四个方向上的Lamp对象依次轮询变亮,Lamp对象中还要有一个oppositeLampName变量来表示它们相反方向的灯,再用一个nextLampName变量来表示此灯变亮后的下一个变亮的灯。这三个变量用构造方法的形式进行赋值,因为枚举元素必须在定义之后引用,所以无法再构造方法中彼此相互引用,所以,相反方向和下一个方向的灯用字符串形式表示。 
增加让Lamp变亮和变黑的方法:light和blackOut,对于S2N、S2W、E2W、E2N这四个方向上的Lamp对象,这两个方法内部要让相反方向的灯随之变亮和变黑,blackOut方法还要让下一个灯变亮。
除了S2N、S2W、E2W、E2N这四个方向上的Lamp对象之外,其他方向上的Lamp对象的nextLampName和oppositeLampName属性设置为null即可,并且S2N、S2W、E2W、E2N这四个方向上的Lamp对象的nextLampName和oppositeLampName属性必须设置为null,以便防止light和blackOut进入死循环。


实现如下:

public enum Lamp {
	//南和东 控制, 四组设置两对儿,还有相应的相反两队儿
	S2N("N2S","S2W",false),S2W("N2E","E2W",false),
	E2W("W2E","E2S",false),E2S("W2N","S2N",false),
	//对应的线路
	N2S(null,null,false),N2E(null,null,false),
	W2E(null,null,false),W2N(null,null,false),
	//右手边
	S2E(null,null,true),E2N(null,null,true),
	N2W(null,null,true),W2S(null,null,true);
	
	//是否亮的,绿灯
	private boolean lighted;
	//相反的灯,此处用字符串表示;如果用枚举,后边定义的用不了,还没定义
	private String opposite;
	//下一个灯 绿边红,忽略黄灯
	private String next;
	
	private Lamp() {}
	private Lamp(String opposite,String next, boolean lighted) {
		this.opposite = opposite;
		this.next = next;
		this.lighted = lighted;
	}
	
	public boolean isLighted() {
		return lighted;
		
		
	}
	//绿灯
	public void light() {
		this.lighted = true;
		//不让死循环,另一个方向的灯的相反方向灯为null
				if(opposite!=null) {
					Lamp.valueOf(opposite).light();
				}
				
				System.out.println(name() + " lamp is green,下面总共应该有6个方向能看到汽车穿过!");
	}
	//红灯,返回下一个灯
	public Lamp blackOut() {
		this.lighted = false;
		if(opposite!=null) {
			//相对的灯也跟着变
			Lamp.valueOf(opposite).blackOut();
		}
		
		//下一个灯亮,然后带动相对的灯也亮
		Lamp nextLamp = null;
		if(next!=null) {
			nextLamp =Lamp.valueOf(next);
			System.out.println("绿灯从" + name() + "-------->切换为" + next);
			nextLamp.light();
		}
		return nextLamp;
	}
}

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 {
	
	//车的集合
	private List<String> vechicles = new ArrayList<String>();
	
	//路的名字,这里是方向S2E
	private String name = null;
	
	public Road(String name) {
		this.name = name;
		
		ExecutorService pool = Executors.newSingleThreadExecutor();
		pool.execute(new Runnable(){

			@Override
			public void run() {
				for(int i=1;i<1000;i++) {
					try {
						//1 到 10 秒钟,模拟车辆出现频率
						Thread.sleep((new Random().nextInt(10)+1)*1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					vechicles.add(Road.this.name+"_"+i);
				}
			}
			
		});
		
		//汽车查看路灯的状况
		
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		timer.scheduleAtFixedRate(
				new Runnable() {

					@Override
					public void run() {
						if(vechicles.size()>0) {
							//判断是否是绿灯
							boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
							if(lighted) {
								//汽车穿过
								System.out.println(vechicles.remove(0)+ " is traversing");
							}
						}
					}
					
				}, 
				1, 
				1, 
				TimeUnit.SECONDS);
	}
}

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

/**
 * 控制灯的交替
 * @author ETHAN
 *
 */
public class LampController {
	private Lamp currentLamp;
	
	public LampController() {
		currentLamp = Lamp.S2N;
		//S2N, lighted------>true
		//改变枚举类成员,枚举成员就是一个实例对象
		currentLamp.light();
		
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		
		//每隔10秒钟把当前灯变黑
		timer.scheduleAtFixedRate(
				new Runnable() {
					@Override
					public void run() {
						System.out.println("Come on!");
						//返回下一个变凉的灯
						currentLamp = currentLamp.blackOut();
					}
				},
				10,
				10, 
				TimeUnit.SECONDS);
	}
}

public class MainClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*产生12个方向的路线*/		
		String [] directions = new String[]{
				"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"		
		};
		
		for(int i=0;i<directions.length;i++){
			new Road(directions[i]);
		}
		
		/*产生整个交通灯系统*/	
		//灯 10秒钟 交替
		new LampController();
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值