All pastes #980328 Raw Edit

idea

public java v1 · immutable
·published 2008-04-10 22:55 UTC
rendered paste body
package trafficlight;public class TrafficLight {	private boolean nsGreen = false;	private boolean nsRed = false;	private boolean ewGreen = false;	private boolean ewRed = false;	private int g = 0;	private int time = 12;	private int counter = 0;	public TrafficLight(int g) {		this.g = g;		counter = g + time;		nsRed = true;		if( g > 0 ) {			ewGreen = true;		}	}	public void tick() {		if (nsRed && counter == 0) {			nsGreen = true;			nsRed = false;			counter = g;		} else if (nsGreen && counter == 0) {			nsGreen = false;			nsRed = true;			counter = g + (2 * time);		}				if( ewRed && counter == 0 ) {			ewGreen = true;			ewRed = false;			counter = g;		} else if( ewGreen && counter == 0 ) {			ewGreen = false;			ewRed = true;			counter = g + (2 * time);		}				if (counter > 0) {			counter--;		}	}	public boolean isNSGreenLit() {		return nsGreen;	}	public boolean isNSRedLit() {		return nsRed;	}		public boolean isEWGreenLit() {		return ewGreen;	}	public boolean isEWRedLit() {		return ewRed;	}	public static void main(String[] args) {		TrafficLight tl = new TrafficLight(10);		for (int i = 0; i < 500; i++) {			tl.tick();			System.out.println(i);			System.out.println( "NS -> G: " + tl.isNSGreenLit() + ", R: " + tl.isNSRedLit());			System.out.println( "EW -> G: " + tl.isEWGreenLit() + ", R: " + tl.isEWRedLit());			if( tl.isNSGreenLit() == tl.isEWGreenLit() ) {				System.err.println( "both green" );			}			if( tl.isNSRedLit() == tl.isEWRedLit() ) {				System.err.println( "both red" );			}		}	}}