Skip to content

Commit 5109c8f

Browse files
author
mafagafogigante
committed
Minor refactoring on the observer code.
1 parent edc93ea commit 5109c8f

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

observer/src/main/java/com/iluwatar/Hobbits.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ public class Hobbits implements WeatherObserver {
66
public void update(WeatherType currentWeather) {
77
switch (currentWeather) {
88
case COLD:
9-
System.out
10-
.println("The hobbits are shivering in the cold weather.");
9+
System.out.println("The hobbits are shivering in the cold weather.");
1110
break;
1211
case RAINY:
1312
System.out.println("The hobbits look for cover from the rain.");
@@ -16,8 +15,7 @@ public void update(WeatherType currentWeather) {
1615
System.out.println("The happy hobbits bade in the warm sun.");
1716
break;
1817
case WINDY:
19-
System.out
20-
.println("The hobbits hold their hats tightly in the windy weather.");
18+
System.out.println("The hobbits hold their hats tightly in the windy weather.");
2119
break;
2220
default:
2321
break;

observer/src/main/java/com/iluwatar/Weather.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,9 @@ public void removeObserver(WeatherObserver obs) {
2828
}
2929

3030
public void timePasses() {
31-
switch (currentWeather) {
32-
case COLD:
33-
currentWeather = WeatherType.SUNNY;
34-
break;
35-
case RAINY:
36-
currentWeather = WeatherType.WINDY;
37-
break;
38-
case SUNNY:
39-
currentWeather = WeatherType.RAINY;
40-
break;
41-
case WINDY:
42-
currentWeather = WeatherType.COLD;
43-
break;
44-
default:
45-
break;
46-
}
47-
System.out.println("The weather now changes to " + currentWeather);
31+
WeatherType[] enumValues = WeatherType.values();
32+
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
33+
System.out.println("The weather changed to " + currentWeather + ".");
4834
notifyObservers();
4935
}
5036

observer/src/main/java/com/iluwatar/WeatherType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ public enum WeatherType {
44

55
SUNNY, RAINY, WINDY, COLD;
66

7+
@Override
78
public String toString() {
89
return this.name().toLowerCase();
9-
};
10+
}
1011

1112
}

0 commit comments

Comments
 (0)