Minor refactoring on the observer code.
This commit is contained in:
		| @@ -6,8 +6,7 @@ public class Hobbits implements WeatherObserver { | |||||||
| 	public void update(WeatherType currentWeather) { | 	public void update(WeatherType currentWeather) { | ||||||
| 		switch (currentWeather) { | 		switch (currentWeather) { | ||||||
| 		case COLD: | 		case COLD: | ||||||
| 			System.out | 			System.out.println("The hobbits are shivering in the cold weather."); | ||||||
| 					.println("The hobbits are shivering in the cold weather."); |  | ||||||
| 			break; | 			break; | ||||||
| 		case RAINY: | 		case RAINY: | ||||||
| 			System.out.println("The hobbits look for cover from the rain."); | 			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."); | 			System.out.println("The happy hobbits bade in the warm sun."); | ||||||
| 			break; | 			break; | ||||||
| 		case WINDY: | 		case WINDY: | ||||||
| 			System.out | 			System.out.println("The hobbits hold their hats tightly in the windy weather."); | ||||||
| 					.println("The hobbits hold their hats tightly in the windy weather."); |  | ||||||
| 			break; | 			break; | ||||||
| 		default: | 		default: | ||||||
| 			break; | 			break; | ||||||
|   | |||||||
| @@ -28,23 +28,9 @@ public class Weather { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public void timePasses() { | 	public void timePasses() { | ||||||
| 		switch (currentWeather) { | 		WeatherType[] enumValues = WeatherType.values(); | ||||||
| 		case COLD: | 		currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length]; | ||||||
| 			currentWeather = WeatherType.SUNNY; | 		System.out.println("The weather changed to " + currentWeather + "."); | ||||||
| 			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); |  | ||||||
| 		notifyObservers(); | 		notifyObservers(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,8 +4,9 @@ public enum WeatherType { | |||||||
|  |  | ||||||
| 	SUNNY, RAINY, WINDY, COLD; | 	SUNNY, RAINY, WINDY, COLD; | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
| 	public String toString() { | 	public String toString() { | ||||||
| 		return this.name().toLowerCase(); | 		return this.name().toLowerCase(); | ||||||
| 	}; | 	} | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user