We are going to program traffic lights (again). If you have already done the TrafficLight Challenge from a previous chapter, especially if you copied the example code (with the errors in it) you will have noticed that using a String for the state (color) of a TrafficLight quickly leads to errors! For example, Orange, Orange and orange all accepted by the compiler but the values are different: this can cause all kinds of bugs: For example, take 2 TrafficLights, say trafficLight1 and trafficLight2. If the color of trafficLight1 has value Orange and the color of trafficLight2 has value Orange then an equation like
if (trafficLight1.color == trafficLight2.color) {
...
}
return false while the programmer expects true:
A typo that can have major consequences for the behavior of the software.
To avoid these bugs, we are going to use an Enum with them this time:
By using an Enum, the compiler can help detect and
detect and prevent them!
For the color (state) of the TrafficLight, we will create an Enum:
public enum TrafficlightColors {
Red,
Orange,
Green
}
objects of type TrafficLight, but I want to program it only once.Green, Orange, Red: use English names for these states.class TrafficLight.private Field color of type TrafficlightColors.method NextState() that gives the TrafficLight the next value of color.public TrafficlightColors GetCurrentColor()method NextState returns the color after changing the color: public TrafficlightColors NextState() {...}A Console app has a main-method (public static void Main(string [] args))
in which you can put code like:
TrafficLight trafficLight = new TrafficLight();
// color has to be TrafficlightColors.Red.
Console.WriteLine(trafficLight.GetCurrentcolor());
trafficLight.NextState();
// color has to be TrafficlightColors.Green.
Console.WriteLine(trafficLight.GetCurrentColor());
trafficLight.NextState();
// color has to be TrafficlightColors.Orange.
Console.WriteLine(trafficLight.GetCurentColor());
trafficLight.NextState();
// and TrafficlightColors.Red again!
Console.WriteLine(trafficLight.GetCurentColor());
Is this code sloppily put together? Look carefully for errors! Correct them if necessary and test the program. Can you think of any improvements to the program?