黑马程序员 交通灯管理系统

-----------android培训java培训、java学习型技术博客、期待与您交流! -----------

交通灯管理系统

前提:
现假设交通灯系统只有红灯和绿灯,忽略黄灯。
随机产生车辆,绿灯时,每隔一秒,路上的车行驶过去。
思路

1,定义路灯:

十字路口车辆运行方向有12个,其中4个右拐可以认为一直是绿灯,其它8个两两对应。
因此只需要对S2N(南向北),S2W(南向西),W2N(西向北),W2E(西向)4个灯进行控制。其它对应的4个灯随之对应变化。
十二盏灯可以用一个枚举进行定义。当某盏灯变红时,它对应方向的灯也变红。它的下一盏灯变绿。
因此灯里面有三个属性:灯的颜色,其对应的灯,其下一盏灯。

张老师在视屏里面用true表示绿,false表示红。
为了更直观,我这里将将红灯,绿灯定义成枚举元素。
public enum Color {
	RED,GREEN
}

下面是灯的定义:

public enum Lamp
{
	S2N(Color.RED,"S2W","N2S"),S2W(Color.RED,"W2E","N2E"),W2E(Color.RED,"W2N","E2W"),W2N(Color.RED,"S2N","E2S"),//这里是四个控制的灯。
	N2S(Color.RED,null,null),N2E(Color.RED,null,null),E2W(Color.RED,null,null),E2S(Color.RED,null,null),//上面分别相对的灯
	S2E(Color.GREEN,null,null),E2N(Color.GREEN,null,null),N2W(Color.GREEN,null,null),W2S(Color.GREEN,null,null);//控制灯的下一盏灯。
	//定义灯的属性
	private Color color;
	private String next,oppsite;
	private Lamp(Color color,String nextLamp,String oppsiteLamp)
	{
		this.color = color;
		this.next = nextLamp;
		this.oppsite = oppsiteLamp;	
	}
	//获取灯的颜色
	public Color LampColor()
	{
		return this.color;
	}
	//定义将灯变绿的方法。
	public void toGreen()
	{
		this.color = Color.GREEN; 
		if(oppsite!=null)//为了防止两个相对的灯互相调用,变成死循环。
			Lamp.valueOf(oppsite).toGreen();
		System.out.println(this.name()+"变绿了");
	}
	//定义将灯变红的方法。
	public Lamp toRed()
	{
		this.color = Color.RED;
		System.out.println(this.name()+"变红了");
		if(oppsite!=null)
		{
			System.out.println(Lamp.valueOf(oppsite).name()+"变红了");
			Lamp.valueOf(oppsite).toRed();
		}

		Lamp nextLamp = null;
		if(next!=null)			
		{	
			nextLamp = Lamp.valueOf(next);
			nextLamp.toGreen();//将其相邻的灯nextLamp和其相对应的灯变绿。
		
		}
		return nextLamp;//返回变绿的灯。
	}

}

2,定义一个定时控制器来控制路灯。

原理是:控制器初始化的时候,将指定的两个对应的灯变绿。一段时间后使其变红,路灯枚举里面的变红方法使它的下一盏灯变绿。同时返回变绿的灯给控制器。
使控制器在一段时间后将变绿的灯变红。以此循环下去。
这里用到JAVA中的线程池和定时器
定时器:Executors.newScheduledThreadPool(1).scheduleAtFixedRate(command(运行的代码),initialDelay(初始运行时间), period(间隔时间), unit(时间单位));
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class LampController 
{
	//让控制器本身就有一个灯的属性
	private Lamp currentLamp;
	//将运行代码放在构造函数里面,使其对象建立时就运行。
	public LampController()
	{	//将S2N方向的灯先变绿
		currentLamp = Lamp.S2N;
		currentLamp.toGreen();
		//用定时器控制灯的颜色的变化
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		timer.scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						currentLamp = currentLamp.toRed();
					}
				}, 
				10, 
				10,
				TimeUnit.SECONDS);
	}
}


3,路上车辆运行情况。

通过面对对象的思想,车是路上的数据。因此将路定义成类,用字符串表示车辆是否通过。
这里通过Executors调用线程池运行代码,因为只需要一个线程运行:
Executors.newSingleThreadExecutor().execute(command);

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> vechicles = new ArrayList<String>();
	private String road = null; 
	//将代码封装到构造函数中,使其建立对象变运行。
	public Road(String road)
	{
		this.road = road;
		//定义线程,随机产生车辆
		ExecutorService pool = Executors.newSingleThreadExecutor();
		pool.execute(new Runnable(){
			public void run()
			{
				for(int x=1;x<1000;x++)
				{
					try {
						Thread.sleep(new Random().nextInt(10)+1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					vechicles.add(Road.this.road+"方向来了第"+x+"辆车");
				}
			}
		});
		//用定时器控制车辆每过一秒开过一辆。
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		timer.scheduleAtFixedRate
		(
				new Runnable()
				{
					public void run()
					{	//如果有车等待就判断是否为绿灯,再使车通过。
						if(vechicles.size()>0)
						{
							if(Lamp.valueOf(Road.this.road).LampColor()==Color.GREEN)
								System.out.println(vechicles.remove(0)+"开了过去");
						}
					}
				},
				1,
				1,
				TimeUnit.SECONDS);
	}
}

4,最后通过主类对以上三个类进行整合运行。
public class MainClass
{
	public static void main(String[] args)
	{
		String[] lamps = new String[]{"S2N","S2W","S2E","W2E","W2N","W2S","N2E","N2S","N2W","E2W","E2N","E2S"};
		for(String s:lamps)
		{
			new Road(s);
		}
		new LampController();
	}
}

最后整体上看,其实整个系统不复杂。最主要的还是要有清晰的思路。最核心的是要明白灯是怎样进行变换,灯与灯之间是如何进行联动。其次才能定义出等的属性。


-----------
android培训java培训、java学习型技术博客、期待与您交流! -----------


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值