This repository has been archived on 2022-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
patterns/observer/request/forecastdisplay.d

41 lines
999 B
D
Raw Normal View History

2022-11-11 09:54:25 +00:00
module observer.request.forecastdisplay;
2022-11-11 07:56:59 +00:00
2022-11-11 09:54:25 +00:00
import observer.request.displayelement;
import observer.request.observer;
import observer.request.weatherdata;
2022-11-11 07:56:59 +00:00
import std.stdio : write, writeln;
import std.format : format;
class ForecastDisplay : Observer, DisplayElement
{
private:
float currentPressure = 29.92f;
float lastPressure;
WeatherData weatherData;
public:
this(WeatherData weatherData)
{
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
override void update()
{
lastPressure = currentPressure;
currentPressure = weatherData.getPressure;
display();
}
override void display()
{
write("Forecast: ");
if (currentPressure > lastPressure) {
writeln("Improving weather on the way!");
} else if (currentPressure == lastPressure) {
writeln("More of the same");
} else if (currentPressure < lastPressure) {
writeln("Watch out for cooler, rainy weather");
}
}
}