Advertisement
Fhernd

StatisticsDisplayWithEventsAndDelegates.cs

Mar 13th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 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.WeatherStationWithEventsAndDelegates
  8. {
  9.     class StatisticsDisplayWithEventsAndDelegates : IObserverWithEventsAndDelegates, DisplayElementWithEventsAndDelegates
  10.     {
  11.         private float maxTemp = 0.0F;
  12.         private float minTemp = 200;
  13.         private float tempSum = 0.0F;
  14.         private int numReadings;
  15.  
  16.         public void Update(float temperature, float humidity, float pressure)
  17.         {
  18.             tempSum += temperature;
  19.             numReadings++;
  20.  
  21.             if (temperature > maxTemp)
  22.             {
  23.                 maxTemp = temperature;
  24.             }
  25.  
  26.             if (temperature < minTemp)
  27.             {
  28.                 minTemp = temperature;
  29.             }
  30.  
  31.             Display();
  32.         }
  33.  
  34.         public void Display()
  35.         {
  36.             System.Console.WriteLine(String.Format("Avg/Max/Min temperature = {0}/{1}/{2}", (tempSum / numReadings), maxTemp, minTemp));
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement