黑马程序员技术交流社区
标题:
看张孝祥老师视频前,自己写了个交通灯管理系统
[打印本页]
作者:
风悠悠
时间:
2013-10-19 20:55
标题:
看张孝祥老师视频前,自己写了个交通灯管理系统
本帖最后由 风悠悠 于 2013-10-19 21:26 编辑
现在开始看视频
//主函数
package cn.itcast.traffic;
public class MyTraffic {
public static void main(String[] args) throws Exception {
EastAndWest EW=new EastAndWest();
NorthAndSouth NS=new NorthAndSouth();
Dextroversion Dex=new Dextroversion();</P>
String[] directions={"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"} ;
Thread EW_thread=new Thread(EW);
Thread NS_thread=new Thread(NS);
NS_thread.start();
EW_thread.start();
for(String direction:directions){
new Thread(new Road(direction,EW,NS,Dex)).start();
}
new Control(EW,NS).run();
}
复制代码
作者:
风悠悠
时间:
2013-10-19 20:56
本帖最后由 风悠悠 于 2013-10-19 21:17 编辑
//随机生成12条方向的道路上出现的车辆
package cn.itcast.traffic;
import java.util.Random;
public class Road implements Runnable{
private String direction;
private Random random=new Random();
private int time;
private int num=0;
private EastAndWest EW;
private NorthAndSouth NS;
private Dextroversion Dex;
private String NumOfCars;</P>
public Road(String direction,EastAndWest EW,NorthAndSouth NS,Dextroversion Dex) {
this.direction=direction;
this.EW=EW;
this.NS=NS;
this.Dex=Dex;
}
public void run() {
while(true){
time=random.nextInt(5000)+2000;
try {
Thread.sleep(time);
num++;
NumOfCars=direction+"_"+num;
if(direction=="E2N"||direction=="N2W"||direction=="W2S"||direction=="S2E")
Dex.getNumOfCars(NumOfCars);
if(direction=="W2E"||direction=="E2W"||direction=="W2N"||direction=="E2S")
EW.getNumOfCars(NumOfCars);
NS.getNumOfCars(NumOfCars);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
复制代码
作者:
风悠悠
时间:
2013-10-19 20:57
本帖最后由 风悠悠 于 2013-10-19 21:10 编辑
public void run() throws Exception{
while(true){
synchronized(EW){
System.out.println("东——西直行绿灯亮,【直行】车辆---允许通行---");
EW.setSign(false,true);
EW.notify();
}
Thread.sleep(10000);
System.out.println("东——西直行红灯亮,【直行】车辆 !!! 停止通行!!!");
System.out.println("东——西左转绿灯亮,【左转】车辆---允许通行---");
EW.setSign(false,false);
Thread.sleep(5000);
EW.setSign(true,true);
System.out.println("东——西左转红灯亮,【左转】车辆---停止通行---");
synchronized(NS){
System.out.println("南——北直行绿灯亮,【直行】车辆---允许通行---");
NS.setSign(false,true);
NS.notify();
}
Thread.sleep(10000);
System.out.println("南——北直行红灯亮,【直行】车辆 !!! 停止通行!!!");
System.out.println("南——北左转绿灯亮,【左转】车辆---允许通行---");
NS.setSign(false,false);
Thread.sleep(5000);
NS.setSign(true,true);
System.out.println("南——北左转红灯亮,【左转】车辆---停止通行---");
}
}
}
复制代码
作者:
风悠悠
时间:
2013-10-19 20:59
本帖最后由 风悠悠 于 2013-10-19 21:21 编辑
//南北向红绿灯代码
package cn.itcast.traffic;
import java.util.LinkedList;
public class NorthAndSouth implements Runnable{
private LinkedList<String> Straight=new LinkedList<String>();
private LinkedList<String> TurnLeft=new LinkedList<String>();
private boolean sign=true;
private boolean sign_direction;
public void run() {
while(true){
if(sign){
synchronized(this){
try {
wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if(Straight.size()!=0&&sign_direction&&!sign){
System.out.println(Straight.removeFirst()+"直行 is traversing!");
}
if(TurnLeft.size()!=0&&!sign_direction&&!sign){
System.out.println(TurnLeft.removeFirst()+"左转 is traversing!");
}
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public void getNumOfCars(String NumOfCars) {
if(NumOfCars.contains("S2N")||NumOfCars.contains("N2S"))
Straight.addLast(NumOfCars);
else
TurnLeft.addLast(NumOfCars);
}
public void setSign(boolean sign, boolean sign_direction) {
this.sign_direction=sign_direction;
this.sign=sign;
}
}
复制代码
作者:
风悠悠
时间:
2013-10-19 21:00
本帖最后由 风悠悠 于 2013-10-19 21:25 编辑
//东西向红绿灯代码
package cn.itcast.traffic;
import java.util.LinkedList;
public class EastAndWest implements Runnable{
private LinkedList<String> Straight=new LinkedList<String>();
private LinkedList<String> TurnLeft=new LinkedList<String>();
private boolean sign=true;
private boolean sign_direction;
public void run() {
while(true){
if(sign){
synchronized(this){
try {
wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(Straight.size()!=0&&sign_direction&&!sign){
System.out.println(Straight.removeFirst()+"直行 is traversing!");
}
if(TurnLeft.size()!=0&&!sign_direction&&!sign){
System.out.println(TurnLeft.removeFirst()+"左转 is traversing!");
}
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void getNumOfCars(String NumOfCars) {
if(NumOfCars.contains("E2W")||NumOfCars.contains("W2E"))
Straight.addLast(NumOfCars);
else
TurnLeft.addLast(NumOfCars);
}
public void setSign(boolean sign,boolean sign_direction) {
this.sign_direction=sign_direction;
this.sign=sign;
}
}
复制代码
package cn.itcast.traffic;
public class Dextroversion{
private String NumOfCars;
public void run() {
System.out.println(NumOfCars + "右转 is traversing !");
}
public void getNumOfCars(String NumOfCars) {
this.NumOfCars=NumOfCars;
run();
}
}
复制代码
作者:
毋须繁华
时间:
2013-10-19 21:54
赞,祝你早日进入黑马
作者:
小小鸟
时间:
2013-10-19 22:15
受不了你了,这不就是老张的代码吗
作者:
风悠悠
时间:
2013-10-20 01:12
小小鸟 发表于 2013-10-19 22:15
受不了你了,这不就是老张的代码吗
你没看完代码就不要乱说话,我只不过借鉴了,张孝祥老师代码里的,E2N,S2N这些方向的了名称而已,况且,视频里,写代码的过程那时候我就还没去看过,只是想先自己试试写了一下而已,还有回帖的话,请说些有意义的话,不要连别人的代码还没看完,就在那乱回复。
作者:
李红亮
时间:
2013-10-20 08:40
好东西,正需要尼,参考下,谢谢啦
作者:
To
时间:
2013-10-20 18:59
好的代码,有自己的总结和注释。分享是值得鼓励的。楼主注意哦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2