-----------------ASP.Net+Unity开发、.Net培训、期待与您交流! ---------------
交通灯管理系统
项目需求:
模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:
由南向北的车辆---直行车辆
由西向南的车辆---右转车辆
由东向南的车辆---左转车辆
面向对象的分析与设计
人在黑板上画圆,是圆提供了画圆的方法。
列车司机紧急刹车,是车提供了刹车的方法。
售票员统计小票金额,是小票提供了计算总金额的方法。
class Rope{
private Point start;
private Point end;
public Rope(Point start,Point end){
this.start=start;
this.end=end;
}
public Point nextPoint(Point currentPoint){
}
}
class Ball{
private Rope rope;
private Point CurrentPoint;
public Ball(Rope rope ,startPoint){
this.rope=rope;
this.currentPoint= startPoint;
}
public void move(){
currentPoint =rope.nextPoint(CurrentPoint);
System.out.println("小球移动到了"+currenPoint);
}
}
设计一个Road类来表示路线,每个Road对象代表一条路线,总共有12条路线,即系统中
每条路线上随机增加新的车辆,增加到一个集合中保存。
每条路线每隔一秒都会检查控制本路线的灯是否为绿,是则将本路线保存车的集合中的
总共有12条线路,所以,系统中总共要产生12个交通灯,右转的路线的灯可以设置为常绿。
此外,除了右转的其他8条路线灯,它们是两两成对的,可以归为四组,所以在编程处理时
只要从这4组中各取一个灯,对着4个灯依次变绿,与这4个灯方向对应的灯则随之也变化,
因此Lamp类中要有一个变量来记住自己相反方向的灯,另外也要有一个变量来表示下一个灯
的状态,最后还要有一个变量表示这个灯自己的状态。
具体代码实现如下:
Road类:
package traffic.lamp;
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;
final List vechiles = new ArrayList();
Road( String name){
this.name=name;
//创建一个线程池
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute( new Runnable() {
public void run() {
for(int i=1;i<100;i++){
try{
Thread.sleep((new Random().nextInt(10)+1)*1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
vechiles.add(Road.this.name+"-"+i);
}
}
});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
public void run(){
if(vechiles.size()>0){
if(Lamp.valueOf(Road.this.name).lighted()){
System.out.println(vechiles.remove(0)+"正在通过...");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
Lamp类:
package traffic.lamp;
public enum Lamp {
S2N(false,"N2S","S2W"),S2W(false,"N2E","E2W"),E2W(false,"W2E","E2S"),E2S(false,"W2N","S2N"),
N2S(false,null,null),N2E(false,null,null),W2E(false,null,null),W2N(false,null,null),
S2E(true,null,null),E2N(true,null,null),N2W(true,null,null),W2S(true,null,null);
private boolean lighted;
private String oppLamp;
private String nextLamp;
private Lamp(boolean lighted,String oppoLamp,String nextLamp){
this.lighted=lighted;
this.oppLamp=oppoLamp;
this.nextLamp=nextLamp;
}
public boolean lighted(){
return lighted;
}
public boolean isLighted(){
this.lighted=true;
if(oppLamp!=null){
Lamp.valueOf(oppLamp).isLighted();
}
return lighted;
}
public Lamp isDarked(){
this.lighted=false;
if(nextLamp!=null){
Lamp.valueOf(nextLamp).isLighted();
System.out.println("绿灯从"+name()+"切换为"+Lamp.valueOf(nextLamp));
}
return Lamp.valueOf(nextLamp);
}
}
LampController类:
package traffic.lamp;
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.isLighted();
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
public void run() {
currentLamp=currentLamp.isDarked();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
MainTest类:
package traffic.lamp;
public class MainTest {
public static void main(String[] args) {
String[] direction = {"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};
for(int i=0;i<direction.length;i++){ new Road(direction[i]);
}
new LampController();
}
}
-------------ASP.Net+Unity开发、.Net培训、期待与您交流! ------------
详情请查看:www.itheima.com