黑马程序员-交通灯管理系统(张孝祥老师)

  • ------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
  • 交通灯:

package com.itheima.traffic;



public enum Lamp {


/*
S2N,S2W,E2W,E2S,N2S,N2E,W2E,W2N,W2S,N2W,E2N,S2E;
*/
//设定12个交通灯,并且指定其对应的副灯和下一个灯,并且初始灯的状态
S2N("N2S","S2W","red"), S2W("N2E","E2W","red"), E2W("W2E","E2S","red"), E2S("W2N","S2N","red"),
N2S(null,null,"red"), N2E(null,null,"red"), W2E(null,null,"red"), W2N(null,null,"red"),
W2S(null,null,"green"), N2W(null,null,"green"), E2N(null,null,"green"), S2E(null,null,"green");

private String opposite;//对应的副灯
private String next;//下一灯
private String color;//灯的颜色
private Lamp(String opposite, String next, String color) {
this.opposite = opposite;
this.next = next;
this.color = color;
}

//将灯变成绿色,并且把对应的副灯也变成绿色
public void colorGreen(){

this.color = "green";
if(opposite != null){//如果是主灯

Lamp.valueOf(opposite).colorGreen();//通过valueOf方法得到对应字符串的枚举
}
System.out.println(name()+"变绿了,奔跑吧!!!!!!!!");
}
public String isColor(){
return color;
}


//将灯变成红色,并且把对应的灯也变成红色,把下一个灯变成绿色
public Lamp colorRed(){

this.color = "red";
if(opposite != null){
Lamp.valueOf(opposite).colorRed();
}

Lamp nextLame = null;//一定要定义在外面
if(next != null){
nextLame = Lamp.valueOf(next);
System.out.println(name()+"变红了,速度跑吧-----------!");
nextLame.colorGreen();//将下一个灯变绿
}
return nextLame;
}

}

package com.itheima.traffic;


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 {


private List<String> car = new ArrayList<String>();//定义一个集合用来存储车
private String name = null;
public Road(String name) {
super();
this.name = name;

//在某个时间,执行代码,安排车上路
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){


@Override
public void run() {

for(int i=1; i<100; i++){

try {
Thread.sleep((new Random().nextInt(10)+1)*1000);//设定时间间隔1秒到10秒之间
} catch (InterruptedException e) {
e.printStackTrace();
}
car.add(Road.this.name+"路开进了"+i+"辆车!");//储存进集合要上路的车辆
}
}
});

//单线程执行,每隔1秒对灯的状态进行检查,如果是绿灯,那么放行车辆
ScheduledExecutorService tempr = Executors.newScheduledThreadPool(1);
tempr.scheduleAtFixedRate(
new Runnable() {

@Override
public void run() {

if(car.size()>0){//如果路上有车
String color = Lamp.valueOf(Road.this.name).isColor();//得到当前灯的颜色
if("green".equals(color)){
System.out.println(car.remove(0));//如果是绿色,那么就放行一辆车,并打印信息
}
}
}
}, 
1, 
1, 
TimeUnit.SECONDS);//秒为单位
}

}

  • 交通灯控制系统:

package com.itheima.traffic;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class LampControl {


private Lamp currentLame;

public LampControl(){

//初始执行S2N为绿灯
currentLame = Lamp.S2N;
currentLame.colorGreen();

//每隔10秒来转换一个红绿灯
ScheduledExecutorService tempr = Executors.newScheduledThreadPool(1);
tempr.scheduleAtFixedRate(
new Runnable() {

@Override
public void run() {

currentLame = currentLame.colorRed();//将当前的灯变成红色,并且返回下一个变绿的灯
}
}, 
10, 
10, 
TimeUnit.SECONDS);
}
}

  • 交通灯管理系统演示:

package com.itheima.traffic;


public class TrafficTest {


public static void main(String[] args) {

//创建12个路线
String[] cars = new String[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","W2S","N2W","E2N","S2E"};

for(int i=0; i<cars.length; i++){

new Road(cars[i]);
}

//创建交通灯控制对象
new LampControl();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值