现在的位置: 首页 > 综合 > 正文

黑马程序员–13交通灯管理系统

2018年05月11日 ⁄ 综合 ⁄ 共 3709字 ⁄ 字号 评论关闭

------- android培训java培训、期待与您交流! ---------- 

 

 

画图最清楚。

共有12种行走路线。车总是当自己的路线为绿灯时通行。

分析对象:

红绿灯,红绿灯的控制系统。

汽车,路。汽车看到自己的路线对应的等绿了就穿过路口了吗?不是,还需要看前面是否有车,看前面是否有车该问那一个对象呢?该问路,

路中存储这车辆的集合,显然路上就应该有增加车辆和减少车辆的方法了。再看题目,我们这里并不要体现车辆移动的过程,只是要捕捉出

车辆穿过路口的过程,也就是捕捉路上减少一辆车的过程,所以,车并不需要设成一个对象,用一个字符串表示就可以了。

面向对象设计把握一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法。再牢牢掌握几个典型的案例就可以了,人在黑板上画圆,

列车司机紧急刹车,售货员同级收货小票的金额,你把们关上了等。

如:一个小球,在绳子的一段滚到另一端。

class Rope{
 
 private Point start;
 private Point end;
 public Rope(Point start,Point end){
  this.start = start;
  this.end = end;
 }
 public Point nextPoint(Point currentPoint){
  /*通过两点一线的数学公式可以计算出当前点的下一个点,这个细节不属于
   * 设计阶段要考虑的问题,如果当前点是终点,则返回null,如果当前点
   * 不是线上的点,则抛出异常。*/
 }
}
class Ball{
 private Rope rope;
 private Point currentPoint;
 public Ball(Rope rope,Point  start){
  this.rope = rope;
  this.currentPoint = start;
 }
 public void move(){
  currentPoint = rope.nextPoint(currentPoint);
  System.out.println("小球移动到了"+currentPoint);
  
 }
}

 

这个程序主要需要4个类,灯,路,灯控制器,MainClass类。
Lamp类:

package com.itheima.learn;

public enum Lamp {
 S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","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(){
 }
 private Lamp(String opposite,String next,boolean lighted){
  this.opposite = opposite;
  this.next = next;
  this.lighted = lighted;
 }
 private boolean lighted;
 private String next;
 private String opposite;
 
 public boolean isLighted(){
  return lighted;
 }
 public void light(){
  this.lighted = true;
  if(opposite != null){
   Lamp.valueOf(opposite).light();
  }
 }
 public Lamp blackOut(){
  this.lighted = false;
  if(opposite != null){
   Lamp.valueOf(opposite).blackOut();
  }
  Lamp nextLamp = null;
  if(next !=null){
   nextLamp = Lamp.valueOf(next);
   nextLamp.light();
   System.out.println("绿灯从"+name()+"------------>切换为"+next);
  }
  return nextLamp;
 }
}

------------------------------

Road类:

/**
 * 每个Road对象代表一条路线,总共有12条路线,即系统中总共要产生12个Road实例对象。
 * 每条路线上随机增加新的车辆,增加到一个集合中保存。
 * 每条路线每隔一秒都会检查控制本路线的灯是否为绿,是则将本路线保存车的集合中的第一辆车移除,即表示车穿过了路口。
  *
 */
public class Road {
 private List<String> vechicles = new ArrayList<String>();
 
 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 {
      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);
  
 }
}

---------------------------

LampController类:

public class LampController {
 private Lamp currentLamp;
 
 public LampController(){
  currentLamp = Lamp.S2N;
  currentLamp.light();
  
  ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
  timer.scheduleAtFixedRate(
    new Runnable() {
     @Override
     public void run() {
      currentLamp = currentLamp.blackOut();
     }
    },
    10,
    10,
    TimeUnit.SECONDS);
 }
}
-------------------------------

MainClass类:

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();
 }
}

 

 

------- android培训java培训、期待与您交流! ----------  详细请查看:http://edu.csdn.net/heima/

抱歉!评论已关闭.