Advertisement
Fhernd

ForecastDisplay.cs

Mar 12th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 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 ForecastDisplay : Observer, DisplayElement
  10.     {
  11.         private float currentPressure = 29.92F;
  12.         private float lastPressure;
  13.         private WeatherData weatherData;
  14.  
  15.         public ForecastDisplay(WeatherData weatherData)
  16.         {
  17.             this.weatherData = weatherData;
  18.             weatherData.RegisterObserver(this);
  19.         }
  20.  
  21.         public void update(float temperature, float humidity, float pressure)
  22.         {
  23.             lastPressure = currentPressure;
  24.             currentPressure = pressure;
  25.  
  26.             display();
  27.         }
  28.  
  29.         public void display()
  30.         {
  31.             Console.WriteLine("Forecast: ");
  32.             if (currentPressure > lastPressure)
  33.             {
  34.                 Console.WriteLine("Improving weather on the way!");
  35.             }
  36.             else if (currentPressure == lastPressure)
  37.             {
  38.                 Console.WriteLine("More of the same");
  39.             }
  40.             else if (currentPressure < lastPressure)
  41.             {
  42.                 Console.WriteLine("Watch out for cooler, rainy weather");
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement