交通灯管理系统

    技术2025-11-27  13

    一、补充的知识点1.面向对象的设计:谁拥有数据,谁就对外提供操作这些数据的方法。2.面向对象的经典例子:经典的例子:1).人在黑板上画圆分析:对象有人,黑板,圆(圆上有圆的半径,圆心),根据思想(谁拥有数据,谁就对外提供操作这些数据的方法),所有事圆画圆2).刹车,根据思想就是车提供刹车的方法3).售票员统计商品的价格,根据思想(票上有各个商品的价格),票提供统计商品的价格的方法;4).关门,门自己发出的动作5).球从一根绳子的一段移动到了另一端。分析:可以把绳子看做两个点,起始点,终止点,当球移动时,他要问绳子,下一个点是什么//绳子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){    //利用数学知识计算出下一个点的位置 }}

    //球class Ball{  private Rope rope;  private Point currentPoint;  public Ball(Rope rope,Point CurrentPoint){   this.rope=rope;   this.currentPoint=currentPoint;  }  public void move(){   currentPoint=rope.nextPoint(currentPoint); }}6).两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子分析:[stone,stoneKnife,tree,wood,chair]应该有一个stoneknife加工厂stoneKnife=stoneKnifeFactroy.createKnife(stone);wood=stoneKnife.cut(tree);chair=chairFactory.creatChair(wood);3.如果一个类用作工具类,并且其中有好多静态方法,一般都加s,如:Utils,Executors等等.4.boolean b=true;if(b){}//正确if(b==true){}//这也写不妥,因为b就true和false两个值,用!就能区别

    二.交通灯管理系统1.交通灯管理系统的分析**首先总结提取对象:路,红绿灯,红绿灯系统,人,汽车**对每个对象进行分析,排除不需要的对象:首先路类:路上有很多汽车,路提供增加,减少车的方法,路可作为一个集合,集合中装车,此外把灯变绿变红也有个控制系统;经过分析提取出的对象:红绿灯,红绿灯系统,路**对集体实际对象的分析:路:总共是12("S2N"," S2W"," E2W"," E2S","N2S"," N2E"," W2E"," W2N","S2E","E2N"," N2W"," W2S")条路,但是4条右拐的是常绿的,所以不用考虑,另外八条都是相互对应的,因此只考虑4条("S2N"," S2W"," E2W"," E2S")就可以了。    路可作为一个集合,集合中装着车,然后路看红绿灯是不是绿了,绿了就把第一辆车取走,路还在不停往自己的尾巴上加车。灯:对于灯我们考虑为12个灯,有12条路就有12个灯红绿灯控制器:只需控制好时间进行切换就可以了。**程序的设计:---红绿灯package com.isoftstone.interview.traffic;

    public enum Lamp { //12条路就有12个灯 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 boolean lighted; // 对面的灯 private String opposite; private String nextLamp; private Lamp( String opposite, String nextLamp,boolean lighted) {  this.opposite = opposite;  this.nextLamp = nextLamp;  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()+" Lamp is green ,下面总共有6个方向能看到汽车穿过"); } //灯变红的方法,返回下一个灯的状态 public Lamp blackOut() {  this.lighted = false;  if (opposite != null) {   Lamp.valueOf(opposite).blackOut();  }  Lamp nextL=null;  if (nextLamp != null) {   nextL=Lamp.valueOf(nextLamp);   System.out.println("绿灯从"+name()+"切换到:"+nextLamp);   nextL.light();  }  return nextL; }}

    ---路:package com.isoftstone.interview.traffic;

    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;

    public class Road { // 路上放车的集合 private java.util.List<String> vechicles = new ArrayList<String>(); // 路名 private String name;

     public Road(String name) {  this.name = name;  ExecutorService threadPool = Executors.newSingleThreadExecutor();  //添加一辆车  threadPool.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();     }     // 出现的车,加入到集合中     vechicles.add(Road.this.name+"-"+i);    }

       }  });  //当是绿灯时,移除一辆车  ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);  /*下面方法的参数解释   *  command - 要执行的任务。  initialDelay - 首次执行的延迟时间。  period - 连续执行之间的周期。  unit - initialDelay 和 period 参数的时间单位*/  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);   }}

    ---红绿灯控制器package com.isoftstone.interview.traffic;

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

    public class LampController { private Lamp currentLamp;

     public LampController() {  // 给定一个当前灯  currentLamp = Lamp.S2N;  currentLamp.light();  // 开始计时,控制红绿灯  ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);  timer.scheduleAtFixedRate(new Runnable() {   public void run() {    // System.out.println("control");    // 时间到后把当前灯变红,也就是把下一个灯变绿,在把灯变红的方法是有返回值的‘    // 正好返回的是下一个灯的状态,因此当前灯也就是下一个灯的状态    currentLamp = currentLamp.blackOut();

       }  }, 10, 10, TimeUnit.SECONDS);

     }

    }---测试类package com.isoftstone.interview.traffic;

    public class TrafficTest { 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)