---------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
交通灯管理系统
分析需求
对象: 车辆(异步随即生成车辆,线路不通)
信号灯只考虑红灯和绿灯
左转车辆考虑信号灯 ,右转车辆不被信号灯控制 。
南北车辆与东西车辆交替走,直行先走,左转后走(每辆车通过时间为1s)
只需要逻辑实现需求
设计对象:Raod类封装线路(1.定义路的属性(录得名称,路上的交通工具,2.随即产生的车辆(不断产生)方法,车辆通过后路上车辆的方法)
Lamp类封装交通灯类(1。通过枚举定义了所有方向的灯,2.获取对象的方法 3.定义灯的属性,4。定义灯的方法)
LampController类封装为交通灯的控制类(1。定义控制灯运行规则的方法)
程序代码:
Lamp.java
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 String opposite; //对面的灯
private String next; //下一个灯
private boolean lighted; //灯的状态
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;
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;
}
}
---------------------------
LampController.java
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LampController {
private Lamp currentLamp;
public LampController() { //设定当前灯
currentLamp = Lamp.S2N;
currentLamp.light();
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
public void run() {
currentLamp = currentLamp.blackOut();
}
}, 10, 10, TimeUnit.SECONDS);//定时器
}
}
----------------------------
Road.java
import java.util.*;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Road {
private String name;
List<String> vechicles = new ArrayList<String>();
public Road(String name) {
this.name = name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable() {
public void run() {
for (int i = 1; i < 1000; i++) {
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);// 10s随机上路的车辆
}catch (InterruptedException e) {
e.printStackTrace();
}
vechicles.add(Road.this.name + "_" + i);// 访问外部类的成员变量
}
}});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);// 定时器
timer.scheduleAtFixedRate(new Runnable() {
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);// 固定频率调用,这里是1秒
}
}
---------------------------------------------------------------
Startclass.java
public class Startclass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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]);
}
new LampController();
}
}
------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------