This repository has been archived on 2022-11-09. You can view files and clone it, but cannot push or open issues or pull requests.
patterns-old/lesson_2/StaticDisplay.hpp

49 lines
1.1 KiB
C++
Raw Normal View History

2021-11-01 08:54:58 +00:00
/*
* StaticDisplay.hpp
*
* Created on: 31 окт. 2021 г.
* Author: alexander
*/
#include "Observer.hpp"
#include "DisplayElement.hpp"
#include "WeatherData.hpp"
class StaticDisplay: public Observer, public DisplayElement
{
private:
float maxTemp;
float minTemp;
float tempSum;
int numReadings;
WeatherData &weatherData;
public:
StaticDisplay(WeatherData &weatherData) :
maxTemp(0.0), minTemp(200.0), tempSum(0.0), numReadings(0), weatherData(weatherData)
{
weatherData.registerObserver(*this);
}
void update(float temperature, float humidity, float pressure) override
{
this->tempSum += temperature;
this->numReadings++;
if (temperature > this->maxTemp) {
this->maxTemp = temperature;
}
if (temperature < this->minTemp) {
this->minTemp = temperature;
}
display();
}
void display() const override
{
std::cout << "Avg/Max/Min temperature = " << (tempSum / numReadings)
<< "/" << maxTemp << "/" << minTemp << std::endl;
}
};