patterns/observer/delivery/forecastdisplay.d

39 lines
968 B
D
Raw Normal View History

2022-12-05 07:38:19 +00:00
module observer.delivery.forecastdisplay;
import observer.delivery.displayelement;
import observer.delivery.observer;
import observer.delivery.weatherdata;
import std.stdio : write, writeln;
import std.format : format;
class ForecastDisplay : Observer, DisplayElement
{
private:
float currentPressure = 29.92f;
float lastPressure;
public:
this(WeatherData weatherData)
{
weatherData.registerObserver(this);
}
override void update(float temperature, float humidity, float pressure)
{
lastPressure = currentPressure;
currentPressure = pressure;
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");
}
}
}