高质量技术免费分享
https://blog.youkuaiyun.com/everyonetimeismoney/article/details/94412711
项目需求
★异步随机生成按照各个线路行驶的车辆。
例如:
由南向而来去往北向的车辆(南--->北)-------直行车辆
由西向而来去往南向的车辆(西--->南)-------直行车辆
由东向而来去往北向的车辆(东--->北)-------直行车辆
★信号灯忽略黄灯,只考虑红灯和绿灯。
★应考虑左转车辆控制信号灯,右转车辆不受信号灯控制
★具体信号灯控制逻辑与现实生活中的普通交通灯控制逻辑相同,不考虑特殊情况下的控制
注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆后,才放行左转车辆。
★每辆车通过路口的时间为1秒(提示:可以通过线程sleep的方式模拟)
★随机生成车辆的时间间隔以及红绿灯交换的时间间隔自定义,可以设置。
★不要求实现GUI,只考虑系统逻辑实现,可通过log方式展现程序运行结果
设计类
找名词:
线路、车辆、信号灯(红、绿)、规则
找动词:
生成车辆(左转车辆等)、红绿灯交换、设置红绿灯变换的时间间隔、设置生成车辆的时间间隔
车辆行驶的总共线路图
规则线路图(有哪些路线是可以一起行驶的)
类图
(由于这次画图的时候没有考虑好,导致理线都花了不少时间1小时30分吧下次得注意)
示例代码:做是做出来了,效果也达到了但是我用到多线程的地方好少,怕不对吧。。。。。。上代码
项目工程结构图比上面的类图多个了类,RulesManger(规则管理者)
★AbstractLine类
package abstractsline;
import java.util.concurrent.CopyOnWriteArrayList;
import vehicle.Vehicle;
import blinker.Blinker;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : AbstractLine.java
// @ Date : 2014/6/7
// @ Author :
//
//
public abstract class AbstractLine{
public AbstractLine(){
}
protected int count = 1;
protected Blinker blinker = new Blinker();
protected String name;
protected CopyOnWriteArrayList<Vehicle> vehicles = new CopyOnWriteArrayList<Vehicle>();
public CopyOnWriteArrayList<Vehicle> getVehicles() {
return vehicles;
}
public void setVehicles(CopyOnWriteArrayList<Vehicle> vehicles) {
this.vehicles = vehicles;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Blinker getBlinker() {
return blinker;
}
public void setBlinker(Blinker blinker) {
this.blinker = blinker;
}
public void generationVehicles(Vehicle vehicle){
// TODO Auto-generated method stub
// try {
//Thread.sleep(timeDifference);
this.vehicles.add(vehicle);
System.out.println(vehicle.getName()+"汽车选择了,"+name+"线路!");
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
};
public Vehicle removeVechides(Vehicle vehicle) throws InterruptedException{
Thread.sleep(1000);
vehicles.remove(vehicle);
System.out.println(this.name+"线路的"+blinker.getColor()+"亮,可以通行,"+"汽车"+vehicle.getName()+"已经离开"+this.name+"线路");
return vehicle;
};
}
★AbstractRule
package abstractsrule;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import abstractsline.AbstractLine;
import blinker.Blinker;
import constant.Constant;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : AbstractRule.java
// @ Date : 2014/6/7
// @ Author :
//
//
public abstract class AbstractRule {
//private AbstractLine[] lines = new AbstractLine[10];//感觉用动态数组好些,可以迭代
protected ArrayList<AbstractLine> lines = new ArrayList<AbstractLine>();
protected String name;
protected static int timeDifference = 5000;
public static int getTimeDifference() {
return timeDifference;
}
public void setTimeDifference(int timeDifference) {
this.timeDifference = timeDifference;
}
public AbstractRule(Constant cons){}
public void letVehicleGo(){
Iterator<AbstractLine> iter = lines.iterator();//迭代设计模式
final CopyOnWriteArrayList<AbstractLine> tempLines = new CopyOnWriteArrayList<AbstractLine>();
while(iter.hasNext()){
tempLines.add(iter.next());
}
for (int i = 0; i < tempLines.size(); i++) {
final int temp = i;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
AbstractLine al = tempLines.get(temp);
Blinker blinker= al.getBlinker();
blinker.changingBlinker();
switch (al.getBlinker().getColor()) {
case READ:
System.out.println(al.getName()+"线路的"+blinker.getColor().toString()+"亮了,"+al.getName()+
"线路的车不可以通行!");
break;
case GREEN:
System.out.println(al.getName()+"线路的"+blinker.getColor().toString()+"亮了,"+al.getName()+
"线路的车可以通行!");
break;
}
}
}).start();
}
// while (iter.hasNext()) {//这里用while循环,如果程序慢点的话,达不到多个方法的灯在同一时间点上更改
// AbstractLine al = iter.next();
// Blinker blinker= al.getBlinker();
// blinker.changingBlinker();
// switch (blinker.getColor()) {
// case READ:
// System.out.println(al.getName()+"线路的"+blinker.getColor().toString()+"亮了,"+al.getName()+
// "线路的车不可以通行!");
// break;
// case GREEN:
// System.out.println(al.getName()+"线路的"+blinker.getColor().toString()+"亮了,"+al.getName()+
// "线路的车可以通行!");
// break;
// }
// }
// System.out.println("--------------------------------------------");
}
public void letVehicleDontGo(){
Iterator<AbstractLine> iter = lines.iterator();
while (iter.hasNext()) {
AbstractLine al = iter.next();
Blinker blinker= al.getBlinker();
blinker.changingBlinker();
System.out.println(al.getName()+"线路的"+blinker.getColor().toString()+"亮了,"+al.getName()+
"线路的车不可以通行!");
}
}
}
★Blinker类
package blinker;
import enumerate.Color;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : Blinker.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class Blinker {
/**
* 改变灯的颜色
*/
private Color color = Color.READ;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void changingBlinker() {
if (color.hashCode()==Color.READ.hashCode()) {
color = Color.GREEN;
}else{
color = Color.READ;
}
}
}
★Constant类
package constant;
import java.util.ArrayList;
import line.EastNorthLine;
import line.EastSouthLine;
import line.EastWestLine;
import line.NorthEastLine;
import line.NorthSouthLine;
import line.NorthWestLine;
import line.SouthEastLine;
import line.SouthNorthLine;
import line.SouthWestLine;
import line.WestEastLine;
import line.WestNorthtLine;
import line.WestSouthLine;
import abstractsline.AbstractLine;
public class Constant {
private ArrayList<AbstractLine> lines = new ArrayList<AbstractLine>();
public ArrayList<AbstractLine> getLines() {
return lines;
}
public void setLines(ArrayList<AbstractLine> lines) {
this.lines = lines;
}
public Constant(){
lines.add(new EastNorthLine());
lines.add(new EastSouthLine());
lines.add(new EastWestLine());
lines.add(new NorthEastLine());
lines.add(new NorthSouthLine());
lines.add(new NorthWestLine());
lines.add(new SouthEastLine());
lines.add(new SouthNorthLine());
lines.add(new SouthWestLine());
lines.add(new WestEastLine());
lines.add(new WestNorthtLine());
lines.add(new WestSouthLine());
}
}
★Color类
package enumerate;
public enum Color {
READ,GREEN;
@Override
public String toString() {
switch (this) {
case READ:
return "红灯";
case GREEN:
return "绿灯";
};
return "异常灯";
}
}
★EastNorthLine类
package line;
import vehicle.Vehicle;
import abstractsline.AbstractLine;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : EastNorthLine.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class EastNorthLine extends AbstractLine {
public EastNorthLine(){
this.name = "东北";
}
}
★EastSouthLine类
package line;
import abstractsline.AbstractLine;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : EastSouthLine.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class EastSouthLine extends AbstractLine {
public EastSouthLine(){
this.name = "东南";
}
}
其他的和这2个都差不多只是赋值不一样
★Main类
package main;
import java.util.Random;
import runle.LinesManager;
import vehicle.Vehicle;
import abstractsline.AbstractLine;
import constant.Constant;
import enumerate.Color;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Constant cons = new Constant();
final LinesManager lm = new LinesManager(cons);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
lm.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 1;
while (true) {
Vehicle vehicle = new Vehicle(i);
int j = new Random().nextInt(12);
//汽车选择车道
AbstractLine al = cons.getLines().get(j);
vehicle.setAbstractLine(al);
al.generationVehicles(vehicle);
try {
Thread.sleep(vehicle.getTimeDifference());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}).start();
/**
* 每个线路上的第一个车辆,看交通灯
*/
for (int i = 0; i < 12; i++) {
final int temp = i;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
AbstractLine al = cons.getLines().get(temp);
while (true) {
try {
Thread.sleep(1000);//12个线程,一起开启,不等待的话,cpu一下次用了%100后果很严重
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(al.getVehicles().size()!=0){
Vehicle vehicle = al.getVehicles().get(0);
AbstractLine vehicleOnLine = vehicle.getAbstractLine();
Color color = vehicleOnLine.getBlinker().getColor();
switch (color) {
case READ:
System.out.println(vehicleOnLine.getName()+"线路的"+color+"亮,不让通行,"+"汽车"+vehicle.getName()+"正在"
+vehicleOnLine.getName()+"线路上等待!");
break;
case GREEN:
try {
vehicleOnLine.removeVechides(vehicle);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(vehicleOnLine+"线路的"+color+"亮,可以通行,"+"汽车"+vehicle+"已经离开"
// +vehicleOnLine+"线路!");
break;
}
}
}
}
}).start();
}
}
}
★LinesManager类
package runle;
import abstractsrule.AbstractRule;
import constant.Constant;
public class LinesManager {
public LinesManager(Constant cons){
ruleOne = new RuleOne(cons);
ruleTwo = new RuleTwo(cons);
ruleThree = new RuleThree(cons);
ruleFour = new RuleFour(cons);
}
AbstractRule ruleOne ;
AbstractRule ruleTwo ;
AbstractRule ruleThree ;
AbstractRule ruleFour;
public void start() throws Exception{
// int time = RuleOne.getTimeDifference();//Cannot make a static reference to the non-static method getTimeDifference()
//from the type AbstractRule这里为什么要用静态的啊
while(true){
ruleOne.letVehicleGo();//放行车辆
Thread.sleep(RuleOne.getTimeDifference());
ruleOne.letVehicleDontGo();//不放行车辆
ruleTwo.letVehicleGo();
Thread.sleep(RuleTwo.getTimeDifference());
ruleTwo.letVehicleDontGo();
ruleThree.letVehicleGo();
Thread.sleep(RuleThree.getTimeDifference());
ruleThree.letVehicleDontGo();
ruleFour.letVehicleGo();
Thread.sleep(RuleFour.getTimeDifference());
ruleFour.letVehicleDontGo();
}
}
}
★RuleFour类
package runle;
import constant.Constant;
import line.EastNorthLine;
import line.NorthEastLine;
import line.SouthEastLine;
import line.WestSouthLine;
import abstractsrule.AbstractRule;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : RuleFour.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class RuleFour extends AbstractRule {
public RuleFour(Constant cons){
super(cons);
name = "规则四";
lines.add(cons.getLines().get(0));
lines.add(cons.getLines().get(11));
}
}
★RuleOne类
package runle;
import java.util.ArrayList;
import constant.Constant;
import abstractsrule.AbstractRule;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : RuleOne.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class RuleOne extends AbstractRule {
public RuleOne(Constant cons){
super(cons);
name = "规则一";
lines.add(cons.getLines().get(9));
lines.add(cons.getLines().get(2));
lines.add(cons.getLines().get(3));
lines.add(cons.getLines().get(8));
}
}
★RuleThree类
package runle;
import constant.Constant;
import line.EastSouthLine;
import line.NorthSouthLine;
import line.SouthNorthLine;
import line.WestNorthtLine;
import abstractsrule.AbstractRule;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : RuleThree.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class RuleThree extends AbstractRule {
public RuleThree(Constant cons){
super(cons);
name = "规则三";
lines.add(cons.getLines().get(10));
lines.add(cons.getLines().get(1));
lines.add(cons.getLines().get(4));
lines.add(cons.getLines().get(7));
}
}
★RuleTwo类
package runle;
import constant.Constant;
import line.NorthEastLine;
import line.NorthWestLine;
import line.SouthEastLine;
import abstractsrule.AbstractRule;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : RuleTwo.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class RuleTwo extends AbstractRule {
public RuleTwo(Constant cons){
super(cons);
name = "规则二";
lines.add(cons.getLines().get(5));
lines.add(cons.getLines().get(6));
}
}
★Vehicle类
package vehicle;
import java.util.Random;
import abstractsline.AbstractLine;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : AbstractVehicle.java
// @ Date : 2014/6/7
// @ Author :
//
//
public class Vehicle {
protected int timeDifference;
private Random random = new Random();
private AbstractLine abstractLine;
public int getTimeDifference() {
// System.out.println(timeDifference);
return timeDifference;
}
public void setTimeDifference(int timeDifference) {
this.timeDifference = timeDifference;
}
public AbstractLine getAbstractLine() {
return abstractLine;
}
public void setAbstractLine(AbstractLine abstractLine) {
this.abstractLine = abstractLine;
}
public Vehicle(Object name){
timeDifference = 1000 + random.nextInt(5000)+1;//1000-60000的数
this.name = name;
}
private Object name;
public Object getName() {
return name;
}
public void setName(Object name) {
this.name = name;
}
// public void lookBlinker() {
//
// }
}
(自己写完了,然后看完了视频,感觉出了不一样的感觉):面向对象可以将复杂事情简单化。
给自己说的话:这个系统花了大概1天半不到的时间完成这样了,我现在的水平大概这样,等过段时间记得回来看的话,就拿来比较比较看自己成长了多少