// Controller.java: 
//
// A simple traffic signal control program --
// an exercise in class invariant construction. 
//   
// A. Ireland  Software Design
//

import java.io.*;

class Colour {
    public static final Colour RED   = new Colour();
    public static final Colour GREEN = new Colour();
    private Colour() {}
}

class Signals
{
    static PrintWriter screen = 
	new PrintWriter (System.out, true);

    private int cycle;  // Time 

    Colour north;       // Colour signal displayed to north bound traffic 
    Colour west;        // Colour signal displayed to west bound traffic  

    // Initializes internal cycle time and the colour signals
    // for north and south bound traffic
    public Signals()
    {
       cycle = 0; north = Colour.RED; west = Colour.GREEN;
    }

    // Conversion between symbol signal colours and string form
    String convert(Colour sig)
    {
      if (sig == Colour.RED) return "Red  ";
	                else return "Green";
    }
   
    // Display colours of both signals at time t
    void display_signals(int t, Colour n, Colour w)
    {
    screen.println("cycle  = " + t + 
                   " north = " + convert(n) + 
                   " west  = " + convert(w));
    }

    // Allow north bound traffic to proceed
    void proceed_north()
    {
      if (north == Colour.RED){
	  west = north; north = Colour.GREEN;
          display_signals(cycle, north, west);
	  cycle++;
	}
      }

    // Allow west bound traffic to proceed
      void proceed_west()
      { 
	if (north == Colour.GREEN){
	  west  = Colour.GREEN; north = west;
          display_signals(cycle, north, west);
          cycle++;
	}
      }
}

class Controller{

    public static void main(String[] args ) throws IOException
    {
     Signals junction = new Signals();
     while (true){
         junction.proceed_north();
         junction.proceed_west();
     }
    }
}
