Advertisement
Fhernd

StatisticsDisplay.cs

Mar 12th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HeadFirstDesignPatterns.Ch02TheObserverPattern.WeatherStation
  8. {
  9.     class StatisticsDisplay : Observer, DisplayElement
  10.     {
  11.         private float maxTemp = 0.0F;
  12.         private float minTemp = 200;
  13.         private float tempSum = 0.0F;
  14.         private int numReadings;
  15.         private WeatherData weatherData;
  16.  
  17.         public StatisticsDisplay(WeatherData weatherData)
  18.         {
  19.             this.weatherData = weatherData;
  20.             weatherData.RegisterObserver(this);
  21.         }
  22.  
  23.         public void update(float temperature, float humidity, float pressure)
  24.         {
  25.             tempSum += temperature;
  26.             numReadings++;
  27.  
  28.             if (temperature > maxTemp)
  29.             {
  30.                 maxTemp = temperature;
  31.             }
  32.  
  33.             if (temperature < minTemp)
  34.             {
  35.                 minTemp = temperature;
  36.             }
  37.  
  38.             display();
  39.         }
  40.  
  41.         public void display()
  42.         {
  43.             Console.WriteLine(String.Format("Avg/Max/Min temperature = {0}/{1}/{2}", (tempSum / numReadings), maxTemp, minTemp));
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement