JDK5交通灯模拟控制系统

时间:2020-10-05 15:37:12 JAVA认证 我要投稿

关于JDK5交通灯模拟控制系统

  为了方便广大程序猿交流和学习,下面小编准备了关于JDK5交通灯模拟控制系统,欢迎大家参考!

  本系统由 Lamp.java , LampController.java , Road.java 和MainClass.java组成。

  Lamp.java :

  package com.isoftstone.interview.traffic;

  public enum Lamp {

  //前进 ,左拐 ,右拐

  S2N("N2S","S2W",false), S2W("N2E","E2W",false), S2E(null,null,true),

  E2W("W2E","E2S",false), E2S("W2N","S2N",false), E2N(null,null,true),

  N2S(null,null,false) , N2E(null,null,false), N2W(null,null,true),

  W2E(null,null,false) , W2N(null,null,false), W2S(null,null,true);

  String opposite;

  String next;

  boolean lighted;

  //构造函数:初始化当前灯

  private Lamp(String opposite,String next,boolean lighted){

  this.opposite = opposite;

  this.next = next;

  this.lighted = lighted;

  }

  //返回当前灯的.状态

  public boolean isLighted(){return lighted;}

  public void light(){

  this.lighted = true;

  if(opposite != null){

  Lamp.valueOf(opposite)。light();

  }

  System.out.println(name() + "is Green. Soon there will be cars crossed the street at six deractions.");

  }

  public Lamp blackout(){

  //关闭当前灯 : 设为false

  this.lighted = false;

  Lamp nextLamp = null;

  if(opposite != null){Lamp.valueOf(opposite)。blackout();}

  //检查下一个灯并启动它

  if(next != null){

  nextLamp = Lamp.valueOf(next);

  System.out.println(name() + " to the " + next + " 's light is Green.");

  nextLamp.light();

  }

  return nextLamp;

  }

  }

  LampController.java

  package com.isoftstone.interview.traffic;

  import java.util.concurrent.Executors;

  import java.util.concurrent.TimeUnit;

  public class LampController {

  private Lamp currentLamp;

  public LampController(){

  currentLamp = Lamp.S2N;

  currentLamp.light();

  //启动一个线程 : 每十秒将当前灯设置为红

  Executors.newScheduledThreadPool(1)。scheduleAtFixedRate(

  new Runnable() {

  public void run() {

  currentLamp = currentLamp.blackout();

  }

  },

  10,

  10,

  TimeUnit.SECONDS

  );

  }

  }

  Road.java

  package com.isoftstone.interview.traffic;

  import java.util.List;

  import java.util.ArrayList;

  import java.util.Random;

  import java.util.concurrent.Executors;

  import java.util.concurrent.TimeUnit;

  public class Road {

  private String name;

  private List vehicles = new ArrayList();

  public Road(String name){

  this.name = name;

  //模拟车辆不断随机上路的过程

  Executors.newSingleThreadExecutor()。execute(new Runnable() {

  public void run() {

  for(int i = 0 ; i < 1000 ;i++){

  try {

  Thread.sleep((new Random()。nextInt(10) + 1) * 1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  vehicles.add(Road.this.name + "_" + i);

  }

  }

  });

  //每隔一秒检查对应的灯是否为绿,如果是 ,则放行一辆车,具体操作为从vehicles集合中移除第一辆车。

  Executors.newScheduledThreadPool(1)。scheduleAtFixedRate(

  new Runnable() {

  public void run() {

  if(vehicles.size() > 0){

  if(Lamp.valueOf(Road.this.name)。isLighted()){

  System.out.println(vehicles.remove(0) + " is traversing");

  }

  }

  }

  },

  1,

  1,

  TimeUnit.SECONDS);

  }

  }

  最后在Main方法中启动系统:public static void main(String[] args) {

  String[] deractions = {"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};

  //模拟十二条方向的路线

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

  new Road(deractions[i]);

  }

  //启动交通灯控制器

  new LampController();

  }

【关于JDK5交通灯模拟控制系统】相关文章:

EDA的交通灯控制系统设计06-04

关于专升本填报志愿模拟演练06-28

数控机床控制系统故障诊断技巧11-14

关于沙盘模拟的重要性解析05-16

单斗液压挖掘机分工况节能控制系统10-18

关于司法考试模拟试题及答案解析08-30

关于临床执业医师笔试模拟试题及答案09-26

关于执业医师综合笔试模拟试题及答案08-31

CorelDRAW认证模拟试题11-15