----------------------ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
--------------------------------------------------------交通灯管理系统----------------------------------------------------------------
1.模拟实现银交通灯管理系统逻辑,具体需求如下从上图中可以很明确的看出共有12条路线,如图上所示;上图可以分为以下四种情况:
情况一:南北直行车辆:S2N,N2S;
情况四:东西直行车辆通行完毕,东西左转车辆通行:E2S,W2N。
具体代码实现如下所示:
**
* 每个Road对象代表一条路线,总共有12条路线,即系统中总共要产生12个Road实例对象。 每条路线上随机增加新的车辆,增加到一个集合中保存。
* 每条路线每隔一秒都会检查控制本路线的灯是否为绿,是则将本路线保存车的集合中的第一辆车移除,即表示车穿过了路口。
*/
public class Road {
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 {
// 每1到10秒内有辆车上路
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
} 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);
}
}
/**
* 每个Lamp元素代表一个方向上的灯,总共有12个方向,所有总共有12个Lamp元素。 有如下一些方向上的灯,每两个形成一组,一组灯同时变绿或变红,所以,
* 程序代码只需要控制每组灯中的一个灯即可: s2n,n2s s2w,n2e e2w,w2e e2s,w2n s2e,n2w e2n,w2s
* 上面最后两行的灯是虚拟的,由于从南向东和从西向南、以及它们的对应方向不受红绿灯的控制, 所以,可以假想它们总是绿灯。
*/
public enum Lamp {
/* 每个枚举元素各表示一个方向的控制灯 */
S2N("N2S", "S2W", false), S2W("N2E", "E2W", false), E2W("W2E", "E2S", false), E2S("W2N", "N2S", 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() { }
private Lamp(String opposite, String next, boolean lighted) {
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 is green,下面总共应该有6个方向能看到汽车穿过!");
}
/**
* 某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿
*
* @return 下一个要变绿的灯
*/
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;
/* 每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿 */
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);
}
}
//S2N,S2W,E2W,E2S,N2S,N2E,W2E,W2N,S2E,E2N,N2W,W2S
public class MainClass {
public static void main(String[] args) {
/* 产生12个方向的路线 */
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();
}
}
h.不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
public class NumberManager {
private int lastNumber = 1;// 上一次取号的号码
private List queueNumber = new ArrayList();// 正在排队序列的号码
// 生成下一个取号的号码
public synchronized Integer generateNewManager() {
queueNumber.add(lastNumber);//将上一次取得的号码放入队列中
return lastNumber++;
}
// 获取下一个服务的号码
public synchronized Integer fetchServiceNumber() {
Integer number = null;
if (queueNumber.size()>0) {
number = queueNumber.remove(0);
}
return number;
}
}
public class NumberMachine{
private NumberManager commonManager = new NumberManager();// 普通用户
private NumberManager expressManager = new NumberManager();// 快速用户
private NumberManager vipManager = new NumberManager();// VIP用户
public NumberManager getCommonManager() {
return commonManager;
}
public NumberManager getExpressManager() {
return expressManager;
}
public NumberManager getVipManager() {
return vipManager;
}
//实现单例模式
private NumberMachine() {}
public static NumberMachine getInstance() {
return instance;
}
private static NumberMachine instance = new NumberMachine();
}
//定义枚举表示客户类型
public enum CustomerType{
COMMON, EXPRESS, VIP;
public String toString()
{
String name = null;
switch (this){
case COMMON:
name = "普通";
break;
case EXPRESS:
name = "快速";
break;
case VIP:
name = name();
break;
}
return name;
}
}
public class ServiceWindow{
private CustomerType type = CustomerType.COMMON;// 定义服务窗口的类型,默认是普通用户
private int windowId = 1;
public void setType(CustomerType type){
this.type = type;
}
public void setWindowId(int windowId){
this.windowId = windowId;
}
// 取号方法
public void start(){
Executors.newSingleThreadExecutor().execute(new Runnable(){
public void run(){
while (true){
switch (type){
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
/**
* 普通客户服务
* */
private void commonService(){
String windowName = "第" + windowId + "号" + "普通窗口";
Integer number = NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
System.out.println(windowName + "正在获取任务!");
if (number != null){
System.out.println(windowName + "为第" + number + "个普通客户服务");
long beginTime = System.currentTimeMillis();
int maxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
long serverTime = new Random().nextInt(maxRand) + Constants.MIN_SERVICE_TIME + 1;
try{
Thread.sleep(serverTime);
}
catch (InterruptedException e){
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个普通客户完成服务,耗时" + (costTime / 1000) + "秒");
}
else{
System.out.println(windowName + "没有取到任务!先休息1秒钟吧!");
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
/**
* 快速客户服务
* */
private void expressService(){
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName + "正在获取任务!");
if (number != null){
System.out.println(windowName + "为第" + number + "个" + type + "客户服务");
long beginTime = System.currentTimeMillis();
try{
Thread.sleep(Constants.MIN_SERVICE_TIME);
}catch (InterruptedException e){
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + type + "客户完成服务,耗时" + (costTime / 1000) + "秒");
}else{
System.out.println(windowName + "没有取到任务!");
//当快速窗口没有任务时,需要为普通用户服务
commonService();
}
}
/**
* VIP客户服务
* */
private void vipService(){
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName + "正在获取任务!");
if (number != null){
System.out.println(windowName + "为第" + number + "个" + type + "客户服务");
long beginTime = System.currentTimeMillis();
int maxRand = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
long serverTime = new Random().nextInt(maxRand) + Constants.MIN_SERVICE_TIME + 1;
try{
Thread.sleep(serverTime);
}
catch (InterruptedException e){
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + type + "客户完成服务,耗时" + (costTime / 1000) + "秒");
}
else{
System.out.println(windowName + "没有取到任务");
//当VIP窗口没有任务时,需要为普通用户服务!
commonService();
}
}
}
public class MainClass{
public static void main(String[] args){
// 创建4个普通用户窗口
for (int i = 1; i < 5; i++){
ServiceWindow commonWindow = new ServiceWindow();
commonWindow.setWindowId(i);
commonWindow.start();
}
// 创建1个快速用户窗口
ServiceWindow expressWindow = new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.start();
// 创建1个VIP用户窗口
ServiceWindow vipWindow = new ServiceWindow();
vipWindow.setType(CustomerType.VIP);
vipWindow.start();
// 普通客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){
public void run(){
Integer number = NumberMachine.getInstance().getCommonManager().generateNewManager();
System.out.println("第" + number + "号普通客户等待服务!");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME, TimeUnit.SECONDS);
// 普通客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){
public void run(){
Integer number = NumberMachine.getInstance().getVipManager().generateNewManager();
System.out.println("第" + number + "号vip客户等待服务!");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, TimeUnit.SECONDS);
// 快速客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){
public void run(){
Integer number = NumberMachine.getInstance().getExpressManager().generateNewManager();
System.out.println("第" + number + "号快速客户等待服务!");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2, TimeUnit.SECONDS);
}
}
public class Constants{
public static int MAX_SERVICE_TIME = 10000;// 最长服务时间为10秒
public static int MIN_SERVICE_TIME = 1000;// 最短服务时间为1秒
/*
* 每个普通窗口服务一个客户的平均时间为5秒,一共有4个这样的窗口,也就是说银行的所有普通窗口合起来
* 平均1.25秒内可以服务完一个普通客户,再加上快速窗口和VIP窗口也可以服务普通客户,所以, 1秒钟产生一个普通客户比较合理
*/
public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}