Minor refactoring on the observer code.

This commit is contained in:
mafagafogigante 2014-10-20 12:48:17 -02:00
parent edc93ea7cf
commit 5109c8f462
3 changed files with 7 additions and 22 deletions

View File

@ -6,8 +6,7 @@ public class Hobbits implements WeatherObserver {
public void update(WeatherType currentWeather) {
switch (currentWeather) {
case COLD:
System.out
.println("The hobbits are shivering in the cold weather.");
System.out.println("The hobbits are shivering in the cold weather.");
break;
case RAINY:
System.out.println("The hobbits look for cover from the rain.");
@ -16,8 +15,7 @@ public class Hobbits implements WeatherObserver {
System.out.println("The happy hobbits bade in the warm sun.");
break;
case WINDY:
System.out
.println("The hobbits hold their hats tightly in the windy weather.");
System.out.println("The hobbits hold their hats tightly in the windy weather.");
break;
default:
break;

View File

@ -28,23 +28,9 @@ public class Weather {
}
public void timePasses() {
switch (currentWeather) {
case COLD:
currentWeather = WeatherType.SUNNY;
break;
case RAINY:
currentWeather = WeatherType.WINDY;
break;
case SUNNY:
currentWeather = WeatherType.RAINY;
break;
case WINDY:
currentWeather = WeatherType.COLD;
break;
default:
break;
}
System.out.println("The weather now changes to " + currentWeather);
WeatherType[] enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
System.out.println("The weather changed to " + currentWeather + ".");
notifyObservers();
}

View File

@ -4,8 +4,9 @@ public enum WeatherType {
SUNNY, RAINY, WINDY, COLD;
@Override
public String toString() {
return this.name().toLowerCase();
};
}
}