交通灯管理系统视频

交通灯管理系统的项目需求

模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:

异步随机生成按照各个路线行驶的车辆.

例如:

由南向而来去往北向的车辆---直行车辆

由西向而来去往南向的车辆---右转车辆

由东向而来去往南向的车辆---左转车辆

 

信号灯忽略黄灯,只考虑红灯和绿灯.

应考虑左转车辆控制信号灯,右转车辆不受信号灯控制.

具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑.

注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆.

每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式来模拟).

随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置.

不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展线程序运行结果.

 

我们初步设想一下有哪些对象:红绿灯,红绿灯的控制系统,汽车,路.汽车看到自己所在路线对应的灯绿了就穿过路口吗?不是,还需要看其前面是否有车,看前面是否有车,该问哪个对象呢?

该问路,路中存储着车辆的集合,显然路上就应该有增加车辆和减少车辆的方法了.再看题目,我们这里并不要体现车辆移动的过程,只要捕捉车辆穿过路口的过程,也就是捕捉路上减少一辆车的过程,所以,这个车并不需要单独设计成为一个对象,用一个字符串表示就可以了.

 

 

Class Rope{

private Point start;

Private Point end;

public Rope(Point start,Poing 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,startPoint){

this.rope = rope;

this.currentPoint = startPoint;

}

 

public void move(){

currentPoint = rope.nextPoint(currentPoint);

System.out.println("小球移动到了" + currentPoint);

}

}

 

 

人在黑板上画原

person,blackboard,circle

 

darw(){

x,y-->radius

}

 

StoneKnife = KnifeFactory.createKnife(stone)

Stone

mateialStoneKnife.cut(tree)

tree

 

chair = ChairFactory.makeChair(material)

material

chair

 

 

 

 

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 = Lamp.valueOf(next);

if(next != null){

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

nextLamp.light();

}

return nextLamp;

}

}

 

 

 

 

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(){

           currentLamp = currentLamp.blackOut();

           }

           },

           10,

           10,

           TimeUnit.SECONDS);

 

           }

}

 

 

 

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

}

}

 

 

 

import java.util.ArrayList;

 

import java.util.List;

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 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(){

            

 

private boolean lighted;

 

public void run(){

              if(vechicles.size()>0){

              boolean ligthed = true;

              

if(lighted){

              System.out.println(vechicles.remove(0) + "is traversing !");

              }

              }

              }

              }, 

              1,

              1,

              TimeUnit.SECONDS);

              

              }

              

}

 

资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值