1   // joi/1/lights/Sequencer.java                         
2   //                                                            
3   //                                                            
4   // Copyright 2003 Bill Campbell and Ethan Bolker                         
5                                                               
6   /**
7    * A Sequencer controls a TrafficLight. It maintains fields 
8    * for the light itself and the current state of the light. 
9    * 
10   * Each time it receives a "next" message, it advances to the 
11   * next state and sends the light an appropriate message.
12   *
13   * @version 1
14   */
15  
16  public class Sequencer
17  {
18      // the TrafficLight this Sequencer controls
19      private TrafficLight light;   
20  
21      // represent the states by ints
22      private final static int GO       = 0; 
23      private final static int CAUTION  = 1;
24      private final static int STOP     = 2;
25      
26      private int currentState;            
27  
28      /**
29       * Construct a sequencer to control a TrafficLight.
30       *
31       * @param light the TrafficLight we wish to control.
32       */
33  
34      public Sequencer( TrafficLight light )
35      {
36          this.light = light;
37          this.currentState = GO;
38          this.light.setGo();
39      }
40  
41      /**
42       * How the light changes when a next Button is pressed 
43       * depends on the current state. The sequence is
44       * GO -> CAUTION -> STOP -> GO.
45       */
46  
47      public void next()
48      {
49          switch ( currentState ) {
50              
51          case GO: 
52              this.currentState = CAUTION;
53              this.light.setCaution();
54              break;
55              
56          case CAUTION:
57              this.currentState = STOP;
58              this.light.setStop();
59              break;
60              
61          case STOP:
62              this.currentState = GO;
63              this.light.setGo();
64              break;
65  
66          default: // This will never happen
67              System.err.println("What color is the light?!");
68          }
69      }
70  }
71