交通灯管理系统

package com.isoftstone.interview.traffic;


//编写Lamp类代码
public enum Lamp {
 S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W3N","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, boolean lighted){
  this.opposite = opposite;
  this.next = next;
  this.lighted = lighted;
  
 }
 
 private Lamp(){
 }
 
 
 
 
 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个方向能看到汽车能穿过! ");
 }
 
 //绿灯变红灯方法
 public Lamp blackOut(){
  this.lighted = false;
  if(opposite != null){
   Lamp.valueOf(opposite).blackOut();
  }
  Lamp nextLamp = null;
  if(opposite != null){
   nextLamp = Lamp.valueOf(next);
   System.out.println("绿灯从" + name() + "-------->切换为" + next);
   nextLamp.light();
  }
  return nextLamp;
 }

}

 

package com.isoftstone.interview.traffic;

import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

 

//编写Road类
public class Road {
 
 //各类交通工具
 List<String> vechicles = new ArrayList<String>();
 
 //路名
 private String name = null;
 
 //编写Road的构造函数。
 public Road (String name){     
  this.name = name;
  
  //创建一个线程池对象,处理汽车不停增加的线程需求。
  ExecutorService pool = Executors.newSingleThreadExecutor();  
  
  pool.execute(new Runnable(){      //创建一个新线程
   public void run(){                     //重写run方法,并在run 中加入处理代码
    for(int i = 1; i < 1000; i ++){    //i 为汽车数量。
     try {
      //设定汽车休息的时间为1-10秒
      Thread.sleep((new Random().nextInt(10) + 1) * 1000); 
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      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 = true;
     if(lighted);
     System.out.println(vechicles.remove(0) + " is traversing !");
    }
   }
  },
    1,
    1,
    TimeUnit.SECONDS);
  
 }

}

 

package com.isoftstone.interview.traffic;

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


//编写Lamp控制类
public class LampController {
 private Lamp currentLamp;
 
 LampController(){
  currentLamp = Lamp.S2N;
  currentLamp.light();
  
  //定时器     参数为线程数量
  ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); 
  timer.scheduleAtFixedRate(
    new Runnable(){
     public void run(){
      System.out.println("来了,宝贝!");
      currentLamp.blackOut();
     }
     
    },
    10,
    10,
    TimeUnit.SECONDS);
 }
}

 

package com.isoftstone.interview.traffic;

 

public class MainClass {

 /**
  * @param args
  */
 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<directions.length; i++){
   new Road(directions[i]);
  }
        new LampController();
  

 }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值