Java Reference
In-Depth Information
public
Controller(String name, TrafficLight tLight,
PhotoCell inPhC, PhotoCell outPhC, StoppingBar
bar, TicketDispenser dispenser) {
super
(name);
trafficLight
#
tLight;
inPhotoCell
#
inPhC;
outPhotoCell
#
outPhC;
stoppingBar
#
bar;
ticketDispenser
#
dispenser;
// attaches to the events raised by the physical devices
this
.attachTo(inPhC);
this
.attachTo(outPhC);
this
.attachTo(bar);
this
.attachTo(dispenser);
}
public void
run() {
trafficLight.setGreen();
while
(alive) {
switch
(phase) {
case
1:
// waits for a new car to cross the first photocell
waitEvent(inPhotoCell, "OCCUPIED");
phase
#
2;
break
;
case
2:
// waits for a new car to clear the first photocell
waitEvent(inPhotoCell, "CLEARED");
// set the trafficLight to red
trafficLight.setRed();
// a new car proceeds to the ticket dispenser
ticketDispenser.deliver();
phase
#
3;
break
;
case
3:
// waits for the ticket dispenser to deliver the
// ticket
waitEvent(ticketDispenser, "DELIVERED");
phase
#
4;
break
;
case
4:
// waits for the car to withdraw the ticket
waitEvent(ticketDispenser, "WITHDRAWN");
// the car has withdrawn the ticket; it raises the
// stopping bar
stoppingBar.raise();
phase
#
5;
break
;
case
5:
// the controller waits for the stopping bar to be up