File tree Expand file tree Collapse file tree 3 files changed +7
-22
lines changed
observer/src/main/java/com/iluwatar Expand file tree Collapse file tree 3 files changed +7
-22
lines changed Original file line number Diff line number Diff line change @@ -6,8 +6,7 @@ public class Hobbits implements WeatherObserver {
6
6
public void update (WeatherType currentWeather ) {
7
7
switch (currentWeather ) {
8
8
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." );
11
10
break ;
12
11
case RAINY :
13
12
System .out .println ("The hobbits look for cover from the rain." );
@@ -16,8 +15,7 @@ public void update(WeatherType currentWeather) {
16
15
System .out .println ("The happy hobbits bade in the warm sun." );
17
16
break ;
18
17
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." );
21
19
break ;
22
20
default :
23
21
break ;
Original file line number Diff line number Diff line change @@ -28,23 +28,9 @@ public void removeObserver(WeatherObserver obs) {
28
28
}
29
29
30
30
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 + "." );
48
34
notifyObservers ();
49
35
}
50
36
Original file line number Diff line number Diff line change @@ -4,8 +4,9 @@ public enum WeatherType {
4
4
5
5
SUNNY , RAINY , WINDY , COLD ;
6
6
7
+ @ Override
7
8
public String toString () {
8
9
return this .name ().toLowerCase ();
9
- };
10
+ }
10
11
11
12
}
You can’t perform that action at this time.
0 commit comments