Java面试题(交通灯管理系统)

交通灯系统

 

面向对象的分析和设计
(重要思想)
谁拥有该数据,谁就对外提供操作这些数据的方法。  
eg:
人在黑板上画图
person,blackboard,circle
draw(){
x,y-->radius
}
两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子
KnifeFactory.createKnife(Stone stone1,Stone stone2)
Stone
StoneKnife
tree
material = StoneKnife.cut(tree)
chair = ChairFactory.makeChair(material)
球从一根绳子的一端移动到另一端
绳子是用来提供小球移动的(所以~~绳子拥有数据,就得提供这些数据的方法)
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("小球移动了"+currentPoint);
}
}

 
模拟实现十字路口的交通灯管理系统逻辑,需求如下

异步随机生成按照各个路线行驶的车

例如: 由南向而来去往北向的车辆 ---- 直行车辆
由西向而来去往南向的车辆 ---- 右转车辆

由东向而来去往南向的车辆 ---- 左转车辆  。。。

信号灯忽略黄灯,只考虑红灯和绿灯

应考虑左转车辆控制信号灯,右转车辆不受信号灯控制

具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑。注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆

每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式模拟)

随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置

不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果
--------------------------------------------------------------------------------
思路分析:路上存在一个集合,集合里装着车,看红绿灯减少车;
12个灯,拐弯的灯是常亮的——枚举
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 Lamp(String opposite,String next,booleanlighted){
this.opposite = opposite;
this.next = next;   
this.lighted = lighted;
}
private boolean lighted;
private String opposite;
private String next;
public boolean isLighted(){
return lighted;
}
public void light(){
this.lighted = true;
if(opposite != null){
Lamp.valueOf(opposite).light();

}
System.out.println(name() + " lamp isgreen,下面总共应该有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;
}
}

 
------------------------------------------------------------------------------------
控制器
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(){
System.out.println("来啊"); 
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}

 
-----------------------------------------------------------------------------------
主函数
public class MainClass {

public static void main(String[] args) {
String [] directions = new String[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
for(int i=0;i
new Road(directions[i]);
}
new LampController();
}

}

 
------------------------------------------------------------------------------------------
Road控制器
public class Road {
private List vechicles = new ArrayList();
private String name =null;
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);
} catch (InterruptedException e) {
e.printStackTrace();
}
vechicles.add(Road.this.name + "_" + i);//访问外部类不用finally
}
}
});
//每隔一秒检查对应的灯是否为绿,是则放行一辆车
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);
}
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值