黑马程序员_6_模拟交通灯管理
来源:互联网 发布:淘宝的集市店 编辑:程序博客网 时间:2024/06/01 10:35
模拟交通灯管理系统
交通灯具备 灯 。方向。路径
灯:是唯一的只有红绿,不考虑黄灯,所以用枚举定义灯,只给出两个对象。
public enum Lemp
{
RED,GREED;//定义两个固定对象
public Lemp getNext()//换灯方法
{
return this==RED?GREED:RED;
}
public String toString()
{
return this==RED?"红灯":"绿灯";
}
}
方向:总共四个方向,东南西北,每个方向都已两盏灯,一盏控制相对方向的直行灯,一盏控制右侧的拐弯灯。
enum Direction
{
//初始化灯,南北两路直行灯为绿灯
N(Lemp.GREED,Lemp.RED),
S(Lemp.GREED,Lemp.RED),
W(Lemp.RED,Lemp.RED),
E(Lemp.RED,Lemp.RED);
private Lemp left,reght;
private Direction(Lemp left,Lemp reght)//每条路应该有两盏灯,一盏表示直行,一盏表示拐弯
{
this.left=left;
this.reght=reght;
}
public void setLeft()//换拐弯灯
{
this.left=left.getNext();
}
public void setReght()//换直行灯
{
this.reght=reght.getNext();
}
public Lemp getLeft() //获取拐弯灯
{
return left;
}
public Lemp getReght() //获取直行灯
{
return reght;
}
public String toString()
{
return this==N?"南面":this==S?"北面":this==E?"西面":"东面";
}
}
车辆:具备来的方向和将要去的方向,与判断去向的某一盏灯,将这些信息都封装在车里。
public enum Car
{
//车类,每辆车具备 去向与来的方向,和捕捉某条路径的对应灯
N_S(Direction.N,Direction.S,LR.R),N_E(Direction.N,Direction.E,LR.L),
S_N(Direction.S,Direction.N,LR.R),S_W(Direction.S,Direction.W,LR.L),
E_W(Direction.E,Direction.W,LR.R),E_S(Direction.E,Direction.S,LR.L),
W_E(Direction.W,Direction.E,LR.R),W_N(Direction.W,Direction.N,LR.L);
private LR lr;
private Direction x,y;//x=来的方向 y=将要去的方向
private Car(Direction x,Direction y,LR lr)
{
this.x=x;
this.y=y;
this.lr=lr;
}
public String toString()
{
return x+"开往"+y;
}
public Direction getX()
{
return this.x;
}
public Direction getY()
{
return this.y;
}
public LR getLR()
{
return this.lr;
}
}
enum LR//左右枚举
{
L,R;
}
路径:每条路径就相当于一个容器,有两条线程,一条负责产生车辆,另一条将消费车,把车从指定的容器里取出,(需要在指定灯为绿的情况下才能执行)。主要用于模拟车辆行驶。
public class Path
{
private List<String> path;//存储容器
private Car car;//汽车
private int connt=1;//计数器
Path(Car car)//每条路作为容器,在构造时,应该存储对应信息的车
{
this.car=car;
this.path=new ArrayList<>();
init();
PDCar();
}
private void init()//作为一条线程,在路径产生时,每间隔五秒将会产生一两对应路径车
{
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
new Runnable(){
public void run()
{
addCar("第"+(Path.this.connt++)+"辆从"+Path.this.car+"的车");
}
},
0,
5,
TimeUnit.SECONDS);
}
private synchronized void addCar(String Carname)//存入集合
{
System.out.println(Carname+"进入十字路口的"+this.car.getX());
this.path.add(Carname);
}
private void PDCar()//对车判断灯拐弯的这则判断左灯,直行则判断右灯
{
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
new Runnable(){
public void run()
{
if(Path.this.car.getLR()==LR.L)
{
if(getDirL()==Lemp.GREED)
delCar();
}else
{
if(getDirR()==Lemp.GREED)
delCar();
}
}
},
0,
1,
TimeUnit.SECONDS);
}
private Lemp getDirR()
{
return this.car.getY().getReght();
}
private Lemp getDirL()
{
return this.car.getY().getLeft();
}
private synchronized void delCar()
{
if(Path.this.path.size()>0)
System.out.println(Path.this.path.remove(0)+"开往"+car.getY()+"耗时1秒");
else System.out.println("目前"+car.getX()+"没有车向"+car.getY()+"行驶");
}
}
交通灯管理器,具备东南西北四个方向与路径,还有控制灯的方法,都封装都这个枚举里
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
enum trafficRG //交通灯管理,将路径与车辆路径灯都组装在一起
{
TRAFFIC_SIGNAL;
public void open()//开启交通灯
{
manAge();
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
//每隔十秒捕捉每条路的两盏灯当前新信息
new Runnable(){
public void run()
{
System.out.println("南面--拐弯灯="+Direction.N.getLeft()+"--"+"直行灯="+Direction.N.getReght());
System.out.println("北面--拐弯灯="+Direction.S.getLeft()+"--"+"直行灯"+Direction.S.getReght());
System.out.println("东面--拐弯灯="+Direction.W.getLeft()+"--"+"直行灯="+Direction.W.getReght());
System.out.println("西面--拐弯灯="+Direction.E.getLeft()+"--"+"直行灯="+Direction.E.getReght());
}
},
0,
10,
TimeUnit.SECONDS);
addPath();
}
private void manAge()//设置灯的变化时间,(实在找不出规律,写的整体自己都看不下去)
{
Executors.newCachedThreadPool().execute(new Runnable()
{
public void run()
{
Direction.N.setLeft();Direction.S.setLeft();
Direction.N.setReght();Direction.S.setReght();
try{Thread.sleep(10000);}catch(Exception e){}
Direction.N.setReght();Direction.S.setReght();
Direction.E.setLeft();Direction.W.setLeft();
try{Thread.sleep(10000);}catch(Exception e){}
Direction.E.setLeft();Direction.W.setLeft();
Direction.E.setReght();Direction.W.setReght();
try{Thread.sleep(10000);}catch(Exception e){}
Direction.E.setReght();Direction.W.setReght();
Direction.N.setLeft();Direction.S.setLeft();
}
});
}
private void addPath()//八条路径对应指定路径车
{
new Path(Car.N_S);
new Path(Car.N_E);
new Path(Car.S_N);
new Path(Car.S_W);
new Path(Car.W_E);
new Path(Car.W_N);
new Path(Car.E_W);
new Path(Car.E_S);
}
}
主程序:调用交通灯管理系统
public class Main {
public static void main(String[] args)
{
trafficRG.TRAFFIC_SIGNAL.open();//建立交通灯,并开启
}
}
- 黑马程序员_6_模拟交通灯管理
- 黑马程序员~模拟交通灯
- 黑马程序员: 交通灯管理
- 黑马程序员----交通灯管理
- 黑马程序员-交通灯管理
- 黑马程序员───模拟交通灯管理系统
- 黑马程序员_java_基础加强_模拟交通灯管理系统
- 黑马程序员-Java模拟交通灯
- 黑马程序员-模拟交通灯控制系统
- 黑马程序员-模拟交通灯控制系统
- 黑马程序员-JAVA-交通灯模拟
- 《黑马程序员》交通灯管理系统
- 黑马程序员----交通灯管理系统
- <黑马程序员>交通灯管理系统
- 【黑马程序员】交通灯管理系统
- 黑马程序员 交通灯管理系统
- 黑马程序员--交通灯管理系统
- 黑马程序员--交通灯管理系统
- 插入排序算法及其分析
- 开通微信支付的方法
- 第三次练习赛解题报告及标程
- socket程包含头文件问题
- Ubuntu 12.04 安装 gcc-4.8 及 gdb 7.6
- 黑马程序员_6_模拟交通灯管理
- 游戏mvc框架与模块
- bitset应用
- 黑马程序员_7_模拟银行调度
- cocos2d-x设计模式发掘之九:委托模式
- 提升网页加载速度 | google网站速度测试
- Art of Multiprocessor Programming 答案 ch7
- 实习一月
- Class文件的格式