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/delivery/statiscticdisplay.d

46 lines
1.1 KiB
D
Raw Normal View History

2022-11-11 09:54:25 +00:00
module observer.delivery.statiscticdisplay;
2022-11-11 07:56:59 +00:00
2022-11-11 09:54:25 +00:00
import observer.delivery.displayelement;
import observer.delivery.observer;
import observer.delivery.weatherdata;
2022-11-11 07:56:59 +00:00
import std.stdio : writeln;
import std.format : format;
class StatisticsDisplay : Observer, DisplayElement
{
private:
float maxTemp = 0.0f;
float minTemp = 200;
float tempSum= 0.0f;
int numReadings;
public:
this(WeatherData weatherData)
{
weatherData.registerObserver(this);
}
2022-11-11 09:54:25 +00:00
override void update(float temperature, float humidity, float pressure)
2022-11-11 07:56:59 +00:00
{
2022-11-11 09:54:25 +00:00
tempSum += temperature;
2022-11-11 07:56:59 +00:00
numReadings++;
2022-11-11 09:54:25 +00:00
if (temperature > maxTemp)
2022-11-11 07:56:59 +00:00
{
2022-11-11 09:54:25 +00:00
maxTemp = temperature;
2022-11-11 07:56:59 +00:00
}
2022-11-11 09:54:25 +00:00
if (temperature < minTemp)
2022-11-11 07:56:59 +00:00
{
2022-11-11 09:54:25 +00:00
minTemp = temperature;
2022-11-11 07:56:59 +00:00
}
display();
}
override void display()
{
writeln("Avg/Max/Min temperature = ", (tempSum / numReadings), '/', maxTemp, '/', minTemp);
// writeln("Avg/Max/Min temperature = ".format(temperature), "F degrees and %3.1f%% humidity".format(humidity));
}
}