Skip to content

Instantly share code, notes, and snippets.

@artlovan
Created May 22, 2017 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artlovan/59b026ec30475e83ceff2653a975aaa6 to your computer and use it in GitHub Desktop.
Save artlovan/59b026ec30475e83ceff2653a975aaa6 to your computer and use it in GitHub Desktop.
Simple TrafficLight Impl
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TrafficLight {
public static void main(String[] args) throws InterruptedException {
Road north = new Road(Direction.NORTH, Color.RED);
Road south = new Road(Direction.SOUTH, Color.RED);
Road east = new Road(Direction.EAST, Color.RED);
Road west = new Road(Direction.WEST, Color.RED);
Intersection is = new Intersection();
is.addRoad(north);
is.addRoad(south);
is.addRoad(east);
is.addRoad(west);
TrafficLight trafficLight = new TrafficLight(is);
System.out.println("--- Setup & Test Lights ---");
trafficLight.setup();
trafficLight.displayLightStatus();
startOperation();
trafficLight.start();
}
private static void startOperation() {
String st = "Start Operation...";
char[] chars = st.toCharArray();
int timer = 20;
for (char c : chars) {
try { Thread.sleep(timer);
timer = timer + timer/10;
} catch (InterruptedException e) { e.printStackTrace(); }
System.out.print(c);
}
System.out.println();
try { Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace(); }
}
private boolean setup;
private boolean emergency;
private Intersection intersection;
public TrafficLight(Intersection intersection) {
this.intersection = intersection;
}
public void setup() {
List<Road> in = getIntersection();
in.get(Direction.NORTH.getDirection()).setLight(Color.GREEN);
in.get(Direction.SOUTH.getDirection()).setLight(Color.GREEN);
in.get(Direction.WEST.getDirection()).setLight(Color.RED);
in.get(Direction.EAST.getDirection()).setLight(Color.RED);
setup = true;
}
private void stopLights() {
getIntersection().forEach(r -> r.setLight(Color.RED));
setup = false;
}
public void start() {
Runnable runnable = () -> {
while (true) {
handleEmergency();
displayLightStatus();
operateLights(getIntersection());
simulateLatency();
}
};
new Thread(runnable).start();
emergencyCheck();
}
private void simulateLatency() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private List<Road> getIntersection() {
return this.intersection.getIntersection();
}
private void handleEmergency() {
System.out.println("Emergency: " + emergency);
if (emergency) {
stopLights();
return;
}
if (!setup) {
setup();
}
}
private void operateLights(List<Road> in) {
in.forEach(r -> r.setLight(r.getLight() == Color.RED ? Color.GREEN : Color.RED));
}
public void displayLightStatus() {
intersection.light();
System.out.println();
}
public void emergencyCheck() {
Scanner sc = new Scanner(System.in);
String input = "n";
while (!input.equals("q")) {
input = sc.next();
switch (input) {
case "y":
emergency = true;
break;
case "n":
emergency = false;
break;
case "q":
System.out.println("Shutting down the Traffic Light...");
System.exit(0);
}
}
}
}
class Intersection implements Light {
private List<Road> intersection;
public Intersection() {
intersection = new ArrayList<>();
}
public void addRoad(Road r) {
intersection.add(r);
}
public List<Road> getIntersection() {
return intersection;
}
@Override
public void light() {
intersection.forEach(Road::light);
}
}
class Road implements Light {
private Direction roadDirection;
private Color light;
public Road(Direction roadDirection, Color light) {
this.roadDirection = roadDirection;
this.light = light;
}
public void setLight(Color light) {
this.light = light;
}
public Color getLight() {
return light;
}
@Override
public void light() {
System.out.println(this.getRoadDirection() + ": " + this.light);
}
public Direction getRoadDirection() {
return roadDirection;
}
public void setRoadDirection(Direction roadDirection) {
this.roadDirection = roadDirection;
}
}
enum Color {
GREEN(0), YELLOW(1), RED(2);
private int light;
Color(int i) {
light = i;
}
int getLight() {
return light;
}
}
enum Direction {
NORTH(0), SOUTH(1), WEST(2), EAST(3);
private int direction;
Direction(int i) {
direction = i;
}
int getDirection() {
return direction;
}
}
interface Light {
void light();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment