Created
July 12, 2015 05:17
-
-
Save VegaFromLyra/dc77e4ce070036ae4da4 to your computer and use it in GitHub Desktop.
Simulate traffic light system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace TrafficLights | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var trafficLightController = TrafficLightController.getInstance(); | |
Console.WriteLine("Press Ctrl C to stop"); | |
trafficLightController.Start(); | |
bool done = false; | |
while(!done) { | |
// Let the controller do it's thing | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace TrafficLights { | |
public enum TRAFFIC_LIGHT_STATE { | |
RED, | |
GREEN, | |
ORANGE | |
}; | |
public class TrafficLight { | |
public TrafficLight(TRAFFIC_LIGHT_STATE state, string name) { | |
this.State = state; | |
this.Name = name; | |
} | |
public TRAFFIC_LIGHT_STATE State { get; private set; } | |
public string Name { get; private set; } | |
public void Update(TRAFFIC_LIGHT_STATE newState) { | |
this.State = newState; | |
} | |
public void Display() { | |
switch(State) { | |
case TRAFFIC_LIGHT_STATE.RED: | |
Console.WriteLine("{0}:RED", Name); | |
break; | |
case TRAFFIC_LIGHT_STATE.GREEN: | |
Console.WriteLine("{0}:GREEN", Name); | |
break; | |
case TRAFFIC_LIGHT_STATE.ORANGE: | |
Console.WriteLine("{0}:ORANGE", Name); | |
break; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Timers; | |
using System.Collections.Generic; | |
namespace TrafficLights { | |
public class TrafficLightController { | |
const int TIMER_TICK_IN_SECONDS = 1000; | |
const int SWITCH_TO_GREEN_IN_SECONDS = 5; | |
const int SWITCH_TO_ORANGE_IN_SECONDS = 8; | |
const int SWITCH_TO_RED_IN_SECONDS = 10; | |
TrafficLightController() { | |
counter = 0; | |
timer = new Timer(TIMER_TICK_IN_SECONDS); | |
timer.Elapsed += onOneSecondElapsed; | |
trafficLights = new List<TrafficLight>(); | |
trafficLights.Add(new TrafficLight(TRAFFIC_LIGHT_STATE.RED, "Traffic Light 1")); | |
trafficLights.Add(new TrafficLight(TRAFFIC_LIGHT_STATE.RED, "Traffic Light 2")); | |
} | |
static public TrafficLightController getInstance() { | |
if (instance == null) { | |
instance = new TrafficLightController(); | |
} | |
return instance; | |
} | |
public void Start() { | |
timer.Enabled = true; | |
} | |
public void Stop() { | |
timer.Enabled = false; | |
} | |
static void onOneSecondElapsed(Object source, ElapsedEventArgs e) { | |
displayLights(); | |
counter++; | |
if (counter == SWITCH_TO_GREEN_IN_SECONDS) { | |
switchLights(TRAFFIC_LIGHT_STATE.GREEN); | |
} else if (counter == SWITCH_TO_ORANGE_IN_SECONDS) { | |
switchLights(TRAFFIC_LIGHT_STATE.ORANGE); | |
} else if (counter == SWITCH_TO_RED_IN_SECONDS) { | |
switchLights(TRAFFIC_LIGHT_STATE.RED); | |
counter = 0; | |
} | |
} | |
static void displayLights() { | |
foreach(var light in trafficLights) { | |
light.Display(); | |
} | |
} | |
static void switchLights(TRAFFIC_LIGHT_STATE newState) { | |
foreach(var light in trafficLights) { | |
light.Update(newState); | |
} | |
} | |
static TrafficLightController instance; | |
static Timer timer; | |
static int counter; | |
static List<TrafficLight> trafficLights; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment