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 Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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;
}
};