package lianxi;

public class Car {  //Car类
private String name;
private int pos;
private int line;
private int speed;

public Car() {
// TODO Auto-generated constructor stub
}

public Car(String name, int line, int speed) {
this.name = name;
this.line = line;
this.speed = speed;
this.pos = 0;
}

public void run() {
pos += speed;
}

public void stop() {
this.speed = 0;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public String getName() {
return name;
}

public int getPos() {
return pos;
}

public int getLine() {
return line;
}

public int getSpeed() {
return speed;
}

}

//Road类

package lianxi;

import java.util.ArrayList;
import java.util.List;

public class Road {
private int lines;
private int length;
private List<TrafficLight> tra = new ArrayList<TrafficLight>();
private List<Car> cars = new ArrayList<Car>();

public Road(int lines, int length, String hld) {
this.lines = lines;
this.length = length;
// this.red = hld;
}

public void enterCar(String name, int line, int speed) {
Car car = new Car(name, line, speed);
cars.add(car);
}

public void setLight(int line, int index) {
TrafficLight tcl = new TrafficLight(line, index);
tra.add(tcl);
}

private void printLine(int line) {
// int r = (int) (Math.random() * length);
List<Integer> carPoses = new ArrayList<Integer>();
for (TrafficLight t : this.tra) {
if (t.getLine() == line) {
carPoses.add(t.getIndex());
}
}
for (Car car : this.cars) {
if (car.getLine() == line) {
carPoses.add(car.getPos());
}
}
for (int i = 0; i < this.length; i++) {
if (carPoses.contains(i)) {
System.out.print("X");
} else if (carPoses.contains(i)) {
System.out.print("口");
} else {
System.out.print("=");
}
}
System.out.println();
}

public void print() {
for (int i = 0; i < lines; i++) {
printLine(i);
}
}

public void run() {
// List<Integer> carPoses = new ArrayList<Integer>();
// 让所有的车自动开 手动开的话run(int i);
for (int i = cars.size() - 1; i >= 0; i--) {
Car car = this.cars.get(i);
car.run();
// 如果车的位置大于路长度 移除该车辆信息;
// if (car.getPos() > this.length - 1) {
// this.cars.remove(i);
// }
}
}

public void stopEnd() {
for (int i = 0; i < cars.size(); i++) {
if (cars.get(i).getPos() + cars.get(i).getSpeed() > length - 1) {
cars.get(i).setSpeed(1);
}
if (cars.get(i).getPos() == length - 1)
cars.get(i).stop();
}
}

public List<Car> getCars() {
return cars;
}

}

//红绿灯

package lianxi;

public class TrafficLight {
private int index;
private int line;

public TrafficLight(int line, int index) {
this.index = index;
this.line = line;
}

public int getIndex() {
return index;
}

public int getLine() {
return line;
}

public void setIndex(int index) {
this.index = index;
}

public void setLine(int line) {
this.line = line;
}

}

//测试

package lianxi;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

public class MainProg {
public static void main(String[] args) throws IOException,
NoSuchAlgorithmException, InterruptedException {
Road road = new Road(3, 50, "@");
road.enterCar("1号", 1, 1);
road.enterCar("2号", 2, 3);
road.enterCar("3号", 2, 13);
road.setLight(0, 10);
road.setLight(1, 21);
road.setLight(2, 5);
while (true) {
road.print();
System.out.println("\n");
road.run();
road.stopEnd();
Thread.sleep(100);
}
}

}