-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
50 lines (31 loc) · 1.23 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package TrafficLight;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws InterruptedException {
// this is a Queue, where the cars are being added
// used a Queue because it removes items, on a first in first out basis.
Queue<Integer> randomCar = new LinkedList<>();
// cars object created
Car cars = new Car();
cars.addCar(randomCar);
// colour object created.
Light colour = new Light();
// while the randomCar Queue is not empty, the program continues to run.
try{
while (!randomCar.isEmpty()){
// a 2 second delay is needed before the light changes colour
colour.isRed();
TimeUnit.SECONDS.sleep(2);
colour.isYellow(randomCar);
TimeUnit.SECONDS.sleep(2);
colour.isGreen(randomCar);
TimeUnit.SECONDS.sleep(2);
}}
// when the queue is empty it returns the below message instead of returning an Error message
catch(Exception e){
System.out.println("THE QUEUE IS EMPTY. ALL CARS HAVE PASSED THROUGH!");
}
}
}