-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.java
92 lines (76 loc) · 3.38 KB
/
Light.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package TrafficLight;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class Light {
String redLight;
String greenLight;
String yellowLight;
// Light class constructor.
public Light() {
redLight = "RED LIGHT";
greenLight = "GREEN LIGHT";
yellowLight = "YELLOW LIGHT";
}
// if light is red, the method simply does a countdown while the cars wait in Queue
public void isRed() throws InterruptedException {
System.out.println("STOP!\nRED LIGHT!\nWAIT FOR 20 SECONDS.");
// enables to schedule tasks for later execution
Timer timer = new Timer();
// implements tasks that can be schedules to run a number of times using the timer class.
TimerTask task = new TimerTask() {
int counter = 20;
@Override
public void run() {
// after every second, the method decrements the counter variable by 1
if (counter > 0) {
System.out.println(counter + " SECONDS");
counter--;
} else {
// when the counter is equal the zero, the below message is printed
// and the timer is cancelled
System.out.println("SWITCHING TO YELLOW LIGHT AFTER TWO SECONDS: ");
System.out.println("CHANGE GEARS!!\n");
System.out.println(" ");
timer.cancel();
}
}
};
// it does the task inside the run function after 1 seconds/1000 milliseconds
timer.scheduleAtFixedRate(task, 0, 1000);
TimeUnit.SECONDS.sleep(20);
}
// the isGreen method removes 30 cars from the Queue.
public void isGreen(Queue<Integer> randomCar) throws InterruptedException {
System.out.println("GREEN LIGHT!\n GO! GO! GO!");
for(int i = 0;i<30; i++){
randomCar.remove();
// the sleep in built function controls the rate at which the cars pass through
// in this case they pass through after every seconds.
TimeUnit.SECONDS.sleep(1);
System.out.println(randomCar);
}
System.out.println(" ");
System.out.println("THERE ARE NOW " + randomCar.size() + " CARS LEFT");
System.out.println(" ");
}
// the isYellow method runs for 10 seconds but each car is supposed to leave the Queue after
// every 2 seconds and hence only 5 cars are allowed to pass through
public void isYellow(Queue<Integer> randomCar) throws InterruptedException {
System.out.println("YELLOW LIGHT!");
System.out.println("MOVE FAST, ONLY 5 CARS CAN PASS THROUGH.");
for(int i = 0;i<5; i++){
randomCar.remove();
// the sleep in built function controls the rate at which the cars leave the Queue
// in this case they leave after every 2 seconds.
TimeUnit.SECONDS.sleep(2);
System.out.println(randomCar);
}
System.out.println(" ");
System.out.println("THERE ARE NOW " + randomCar.size() + " CARS LEFT");
System.out.println("DIDN'T MANAGE TO GET THROUGH?\nDON'T WORRY YOU WILL PASS WHEN THE LIGHT TURNS GREEN");
System.out.println("THE LIGHT WILL TURN GREEN IN 2 SECONDS.\n");
System.out.println(" ");
}
}