交通灯管理系统

    技术2022-06-29  51

    两个项目的设计按照面向对象的程序设计思想:谁拥有数据,谁就提供操作数据的方法。

    经典案例:

    1、石头、石刀、木头、椅子

          石刀工厂生产石刀,传入的参数是石头,木材由石刀砍出,传入参数为木头,椅子由椅子工厂加工,传入参数为木材

    2、画圆

          圆提供画圆的方法,传入的参数是圆心和半径。

    3、线和小球的关系

          小球有滚动的方法,线提供小球滚动的方向、下一点的坐标

    4、刹车

          人给车刹车的信号,车提供刹车的方法

    5、关门

          门提供关门的方法:旋转等。

     

           交通灯管理系统:

     

           联系实际,熟悉交通灯的工作方式,十字路口共十二个方向,可简化为四个方向来设计,分别由东边和南边出发,有由东向西、由东向南、由南向西、由南向北四个左转方向要考虑,由西向东、由西向北、由北向东、由北向南四个左转方向为上边四个方向对应,另外有由东向北、由北向西、由西向南、由南向东四个右转方向各由一个常绿灯来控制不限行。

           主要设计三个类:路、路灯、路灯控制器。

    1、  路类,路上有车,路提供操作车的方法,产生路面的车辆,车辆是否过路口由路控制,路不断检查对面红绿灯的状况以决定是否对本路面上的车辆放行;

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

    2、  路灯

    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(String opposite,String next,boolean lighted){

                  this.opposite = opposite;

                  this.next = next;

                  this.lighted = lighted;

           }

     

           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(next != null){

                         nextLamp = Lamp.valueOf(next);

                         System.out.println("绿灯从" + name() + "-------->切换为" + next);              

                         nextLamp.light();

                  }

                  return nextLamp;

           }

    }

     

    3、  路灯控制器

    public class LampController {

           private Lamp currentLamp;

          

           public LampController(){

                  //刚开始让由南向北的灯变绿;          

                  currentLamp = Lamp.S2N;

                  currentLamp.light();

                 

                  //每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿      

                  ScheduledExecutorService timer =  Executors.newScheduledThreadPool(1);

                  timer.scheduleAtFixedRate(

                                new Runnable(){

                                       public  void run(){

                                              System.out.println("来啊");

                                              currentLamp = currentLamp.blackOut();

                                }

                                },

                                10,

                                10,

                                TimeUnit.SECONDS);

           }

    }

     

     

    4、产生Main方法

    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)