日夜复习,只为进黑马.各种努力!以下是个人写的交通灯,请大牛帮忙分析分析,哪里好与不好!!!!多谢!!!!!!
- package TrafficLampDemo;
- public enum Lamp {
- Lamp1, Lamp2, Lamp3, Lamp4;
-
- /* 一共有四种灯
- * S2N,N2S Lamp1
- * S2W,N2E Lamp2
- * E2W,W2E Lamp3
- * E2S,W2N Lamp4
- */
- //四种灯的顺序
- public Lamp nextLamp() {
- switch (this) {
- case Lamp1:
- return Lamp2;
- case Lamp2:
- return Lamp3;
- case Lamp3:
- return Lamp4;
- case Lamp4:
- return Lamp1;
- }
- return null;
- }
-
- //打印中文
- public String toString() {
- String name = null;
- switch (this) {
- case Lamp1:
- name = "从南到北,从北到南";
- break;
- case Lamp2:
- name = "从南到西,从北到东";
- break;
- case Lamp3:
- name = "从东到西,从西到东";
- break;
- case Lamp4:
- name = "从东到南,从西到北";
- break;
- }
- return name;
- }
- }
复制代码- package TrafficLampDemo;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class LampControl {
-
- //设置一个默认灯
- private Lamp currentLamp = Lamp.Lamp1;
-
- //这个数组其实可以不写的,下面数字调用的就是这个数组的脚标
- String[] roads = { "S2N", "S2W", "E2W", "E2S", "N2S", "N2E", "W2E", "W2N"};
- //构造函数,新建对象时控制灯的转换
- LampControl() {
- System.out.println(currentLamp+"绿灯了");
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
- public void run() {
- System.out.println(currentLamp+"红灯了");
- currentLamp = currentLamp.nextLamp();//10秒跳转灯
- System.out.println(currentLamp+"绿灯了");
- }
- }, 10, 10, TimeUnit.SECONDS);
- }
- public void start() {
-
- //每秒检查一次当前什么灯在亮
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
- public void run() {
- switch (currentLamp) {
- case Lamp1:
- cross(0, 4);
- break;
- case Lamp2:
- cross(1, 5);
- break;
- case Lamp3:
- cross(2, 6);
- break;
- case Lamp4:
- cross(3, 7);
- break;
- }
- }
- }, 1, 1, TimeUnit.SECONDS);
- }
- //绿灯的车1秒通过一个
- public void cross(int x, int y) {
-
- //两个相对应的方向
- Road road1 = Road.valueOf(roads[x]);
- Road road2 = Road.valueOf(roads[y]);
-
- //判断是否有车,有车就过
- Integer num1 = road1.crossCar();
- Integer num2 = road2.crossCar();
- if (num1 != null | num2 != null) {
- if (num1 != null) {
- System.out.println(road1 + "的第" + num1 + "辆车开过去了");
- }
- if (num2 != null) {
- System.out.println(road2 + "的第" + num2 + "辆车开过去了");
- }
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
复制代码- package TrafficLampDemo;
- import java.util.LinkedList;
- import java.util.Random;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public enum Road {
- //S是南 N是北 W是西 E是东
- S2N,S2W,E2W,E2S,
- N2S,N2E,W2E,W2N,
- S2E,E2N,N2W,W2S;
-
- int lastCar = 1;
- //这里紧紧是增删操作,我觉得用LinkedList比较好
- LinkedList<Integer> list = new LinkedList<Integer>();
-
- //每一个路都可以独立随机产生车辆
- public synchronized void produceCar(){
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){
- public void run(){
- list.addLast(lastCar++);
-
- }
- }, 1, new Random().nextInt(10)+1, TimeUnit.SECONDS);
- }
-
- //让绿灯去调用,让车辆过
- public synchronized Integer crossCar(){
- if(list.size()!=0){
- return list.removeFirst();
- }
- return null;
- }
-
-
- //打印中文
- public String toString(){
- switch(this){
- case S2N:
- return "从南到北";
- case S2W:
- return "从南到西";
- case E2W:
- return "从东到西";
- case E2S:
- return "从东到南";
- case N2S:
- return "从北到南";
- case N2E:
- return "从北到东";
- case W2E:
- return "从西到东";
- case W2N:
- return "从西到北";
- case S2E:
- return "从南到东";
- case E2N:
- return "从东到北";
- case N2W:
- return "从北到西";
- case W2S:
- return "从西到南";
- }
- return null;
- }
- }
复制代码- package TrafficLampDemo;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class MainClass {
- static int num ;
- public static void main(String[] args) {
- final String[] roads = { "S2N", "S2W", "E2W", "E2S", "N2S", "N2E",
- "W2E", "W2N", "S2E", "E2N", "N2W", "W2S" };
-
- // 每条路随机产生车辆
- for (int i = 0; i < roads.length; i++) {
- Road.valueOf(roads[i]).produceCar();
- }
- //此处是没有灯的右转模拟,如果想要看模拟就把注释打开
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
- public void run() {
- for (int i = 8; i < roads.length; i++) {
- Road road = Road.valueOf(roads[i]);
- Integer num = road.crossCar();
- if (num != null) {
- //System.out.println(road + "的第" + num + "辆车开过去了");
- try {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }, 1, 1, TimeUnit.SECONDS);
- //开启交通灯控制
- new LampControl().start();
- }
- }
复制代码
|