-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatisticsDisplay.h
44 lines (39 loc) · 1.06 KB
/
StatisticsDisplay.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef STATISTICS_DISPLAY_H
#define STATISTICS_DISPLAY_H
#include "Subject.h"
#include "Observer.h"
#include "DisplayElement.h"
#include "WeatherData.h"
#include <iostream>
class StatisticsDisplay : public Observer, public DisplayElement {
public:
StatisticsDisplay() = default;
StatisticsDisplay(Subject *wd) : weatherData(wd)
{ weatherData->registerObserver(this); }
void update(double t, double h, double p) override;
void display() const override;
private:
double minTemp = 200.0f;
double maxTemp = 0.0f;
double tempSum = 0.0f;
int numberOfReadings = 0;
Subject *weatherData = nullptr;
};
inline
void
StatisticsDisplay::update(double t, [[maybe_unused]] double h, [[maybe_unused]] double p) {
tempSum += t;
++numberOfReadings;
if (t < minTemp)
minTemp = t;
if (t > maxTemp)
maxTemp = t;
display();
}
inline
void
StatisticsDisplay::display() const {
std::cout << "Avg/Max/Min Temperature = " <<
tempSum / numberOfReadings << "/" << maxTemp << "/" << minTemp << '\n';
}
#endif /* ifndef STATISTICS_DISPLAY_H */