A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Tking 中级黑马   /  2014-4-6 02:00  /  933 人查看  /  6 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

交通灯具备 灯 。方向。路径
灯:是唯一的只有红绿,不考虑黄灯,所以用枚举定义灯,只给出两个对象。
  1. public enum Lemp
  2. {
  3.         RED,GREED;//定义两个固定对象
  4.         public Lemp getNext()//换灯方法
  5.         {
  6.                 return this==RED?GREED:RED;
  7.         }
  8.         public String toString()
  9.         {
  10.                 return this==RED?"红灯":"绿灯";
  11.         }
  12. }
复制代码
方向:总共四个方向,东南西北,每个方向都已两盏灯,一盏控制相对方向的直行灯,一盏控制右侧的拐弯灯。
  1. enum Direction
  2. {
  3.         //初始化灯,南北两路直行灯为绿灯
  4.         N(Lemp.GREED,Lemp.RED),
  5.         S(Lemp.GREED,Lemp.RED),
  6.         W(Lemp.RED,Lemp.RED),
  7.         E(Lemp.RED,Lemp.RED);
  8.        
  9.        
  10.         private Lemp left,reght;
  11.        
  12.         private Direction(Lemp left,Lemp reght)//每条路应该有两盏灯,一盏表示直行,一盏表示拐弯
  13.         {
  14.                 this.left=left;
  15.                 this.reght=reght;
  16.         }
  17.         public void setLeft()//换拐弯灯
  18.         {
  19.                 this.left=left.getNext();
  20.         }
  21.         public void setReght()//换直行灯
  22.         {
  23.                 this.reght=reght.getNext();
  24.         }
  25.         public Lemp getLeft() //获取拐弯灯
  26.   {
  27.                 return left;
  28.         }
  29.         public Lemp getReght() //获取直行灯
  30.   {
  31.                 return reght;
  32.         }
  33.         public String toString()
  34.         {
  35.                 return this==N?"南面":this==S?"北面":this==E?"西面":"东面";
  36.         }
  37.        
  38. }
复制代码
车辆:具备来的方向和将要去的方向,与判断去向的某一盏灯,将这些信息都封装在车里。
  1. public enum Car
  2. {
  3.         //车类,每辆车具备 去向与来的方向,和捕捉某条路径的对应灯
  4.         N_S(Direction.N,Direction.S,LR.R),N_E(Direction.N,Direction.E,LR.L),
  5.         S_N(Direction.S,Direction.N,LR.R),S_W(Direction.S,Direction.W,LR.L),
  6.         E_W(Direction.E,Direction.W,LR.R),E_S(Direction.E,Direction.S,LR.L),
  7.         W_E(Direction.W,Direction.E,LR.R),W_N(Direction.W,Direction.N,LR.L);
  8.         private LR lr;
  9.         private Direction x,y;//x=来的方向   y=将要去的方向
  10.         private Car(Direction x,Direction y,LR lr)
  11.         {
  12.                 this.x=x;
  13.                 this.y=y;
  14.                 this.lr=lr;
  15.         }
  16.         public String toString()
  17.         {
  18.                 return x+"开往"+y;
  19.         }
  20.         public Direction getX()
  21.         {
  22.                 return this.x;
  23.         }
  24.         public Direction getY()
  25.         {
  26.                 return this.y;
  27.         }
  28.         public LR getLR()
  29.         {
  30.                 return this.lr;
  31.         }
  32. }

  33. enum LR//左右枚举
  34. {
  35.         L,R;
  36. }
复制代码
路径:每条路径就相当于一个容器,有两条线程,一条负责产生车辆,另一条将消费车,把车从指定的容器里取出,(需要在指定灯为绿的情况下才能执行)。主要用于模拟车辆行驶。
  1. public class Path
  2. {
  3.         private List<String> path;//存储容器
  4.         private Car car;//汽车
  5.         private int connt=1;//计数器
  6.         Path(Car car)//每条路作为容器,在构造时,应该存储对应信息的车
  7.         {
  8.                 this.car=car;
  9.                 this.path=new ArrayList<>();
  10.                 init();
  11.                 PDCar();
  12.         }
  13.         private void init()//作为一条线程,在路径产生时,每间隔五秒将会产生一两对应路径车
  14.         {
  15.                 Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
  16.                                 new Runnable(){
  17.                                         public void run()
  18.                                         {
  19.                                                 addCar("第"+(Path.this.connt++)+"辆从"+Path.this.car+"的车");
  20.                                         }
  21.                                 },
  22.                                 0,
  23.                                 5,
  24.                                 TimeUnit.SECONDS);
  25.         }
  26.         private synchronized void addCar(String Carname)//存入集合
  27.         {
  28.                 System.out.println(Carname+"进入十字路口的"+this.car.getX());
  29.                 this.path.add(Carname);
  30.         }
  31.        
  32.         private void PDCar()//对车判断灯拐弯的这则判断左灯,直行则判断右灯
  33.         {
  34.                 Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
  35.                                 new Runnable(){
  36.                                         public void run()
  37.                                         {
  38.                                                 if(Path.this.car.getLR()==LR.L)
  39.                                                 {
  40.                                                         if(getDirL()==Lemp.GREED)
  41.                                                                 delCar();
  42.                                                 }else
  43.                                                 {
  44.                                                         if(getDirR()==Lemp.GREED)
  45.                                                                 delCar();
  46.                                                 }
  47.                                         }
  48.                                 },
  49.                                 0,
  50.                                 1,
  51.                                 TimeUnit.SECONDS);
  52.         }
  53.         private Lemp getDirR()
  54.         {
  55.                 return this.car.getY().getReght();
  56.         }
  57.         private Lemp getDirL()
  58.         {
  59.                 return this.car.getY().getLeft();
  60.         }
  61.         private synchronized void delCar()
  62.         {
  63.                 if(Path.this.path.size()>0)
  64.                         System.out.println(Path.this.path.remove(0)+"开往"+car.getY()+"耗时1秒");
  65.                 else System.out.println("目前"+car.getX()+"没有车向"+car.getY()+"行驶");
  66.         }
  67.        
  68.        
  69. }
复制代码
交通灯管理器,具备东南西北四个方向与路径,还有控制灯的方法,都封装都这个枚举里
  1. import java.util.concurrent.Executors;
  2. import java.util.concurrent.TimeUnit;

  3. enum trafficRG //交通灯管理,将路径与车辆路径灯都组装在一起
  4. {
  5.         TRAFFIC_SIGNAL;
  6.        
  7.         public void open()//开启交通灯
  8.         {
  9.                 manAge();
  10.                 Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
  11.                                 //每隔十秒捕捉每条路的两盏灯当前新信息
  12.                                 new Runnable(){
  13.                                         public void run()
  14.                                         {
  15.                                                 System.out.println("南面--拐弯灯="+Direction.N.getLeft()+"--"+"直行灯="+Direction.N.getReght());
  16.                                                 System.out.println("北面--拐弯灯="+Direction.S.getLeft()+"--"+"直行灯"+Direction.S.getReght());
  17.                                                 System.out.println("东面--拐弯灯="+Direction.W.getLeft()+"--"+"直行灯="+Direction.W.getReght());
  18.                                                 System.out.println("西面--拐弯灯="+Direction.E.getLeft()+"--"+"直行灯="+Direction.E.getReght());
  19.                                         }
  20.                                 },
  21.                                 0,
  22.                                 10,
  23.                                 TimeUnit.SECONDS);
  24.                 addPath();
  25.                
  26.         }
  27.         private void manAge()//设置灯的变化时间,(实在找不出规律,写的整体自己都看不下去)
  28.         {               
  29.                 Executors.newCachedThreadPool().execute(new Runnable()
  30.                 {
  31.                         public void run()
  32.                         {
  33.                                 Direction.N.setLeft();Direction.S.setLeft();
  34.                                 Direction.N.setReght();Direction.S.setReght();
  35.                                
  36.                                 try{Thread.sleep(10000);}catch(Exception e){}
  37.                                
  38.                                 Direction.N.setReght();Direction.S.setReght();
  39.                                 Direction.E.setLeft();Direction.W.setLeft();
  40.                                
  41.                                 try{Thread.sleep(10000);}catch(Exception e){}
  42.                                
  43.                                 Direction.E.setLeft();Direction.W.setLeft();
  44.                                 Direction.E.setReght();Direction.W.setReght();
  45.                                
  46.                                 try{Thread.sleep(10000);}catch(Exception e){}
  47.                                
  48.                                 Direction.E.setReght();Direction.W.setReght();
  49.                                 Direction.N.setLeft();Direction.S.setLeft();
  50.                         }
  51.                 });
  52.         }
  53.         private void addPath()//八条路径对应指定路径车
  54.         {
  55.                 new Path(Car.N_S);
  56.                 new Path(Car.N_E);
  57.                 new Path(Car.S_N);
  58.                 new Path(Car.S_W);
  59.                 new Path(Car.W_E);
  60.                 new Path(Car.W_N);
  61.                 new Path(Car.E_W);
  62.                 new Path(Car.E_S);
  63.         }
  64. }
  65. 主程序:调用交通灯管理系统
  66. public class Main {
  67.         public static void main(String[] args)
  68.         {
  69.                 trafficRG.TRAFFIC_SIGNAL.open();//建立交通灯,并开启
  70.         }
  71. }
复制代码


点评

赞一个也不加个技术分,存手编,融合了视频的思想,至少加个技术分撒  发表于 2014-4-7 16:06

评分

参与人数 1黑马币 +3 收起 理由
枫儿 + 3 赞一个!

查看全部评分

6 个回复

倒序浏览
格子夕 发表于 2014-4-6 17:50
不错的内容呢,支持一下

:P Thank you
回复 使用道具 举报
顶一个先!!!!!
回复 使用道具 举报
想问下  这个图是怎么画的呢  用什么工具可以画这样的图呢    貌似系统自带的画图工具不能化吧

点评

windows 自带的的绘图工具 ,只要用心,什么都可以搞定  发表于 2014-4-8 04:56
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马