Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            WeatherData             sender = new WeatherData();
            CurrentConditionsReport currentConditionsReport =
                new CurrentConditionsReport(sender);

            currentConditionsReport.Subscribe(sender);
            StatisticReport statisticReport = new StatisticReport(sender);

            statisticReport.Subscribe(sender);
            WeatherInfo weatherInfo = new WeatherInfo(20, 740, 75);

            sender.Notify(sender, weatherInfo);
            Random rnd = new Random();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(500);
                weatherInfo = new WeatherInfo(
                    rnd.Next(-3, 20),
                    rnd.Next(730, 780),
                    rnd.Next(60, 95));
                sender.Notify(sender, weatherInfo);
                Console.WriteLine("Temperature {0}, Pressure {1}, Humidity {2}",
                                  currentConditionsReport.WthrInfo.Temperature,
                                  currentConditionsReport.WthrInfo.Pressure,
                                  currentConditionsReport.WthrInfo.Humidity);
            }

            Console.WriteLine();
            Console.WriteLine("Stat report:");
            Console.WriteLine("MinTemperature: {0}", statisticReport.GetTemperatureMin());
            Console.WriteLine("MaxTemperature: {0}", statisticReport.GetTemperatureMax());
            Console.WriteLine("MinPressure: {0}", statisticReport.GetPresureMin());
            Console.WriteLine("MaxPressure: {0}", statisticReport.GetPresureMax());
            Console.WriteLine("MinHumidity: {0}", statisticReport.GetHumidityMin());
            Console.WriteLine("MinHumidity: {0}", statisticReport.GetHumidityMax());
            Console.WriteLine("AverageTemperature: {0}", statisticReport.GetTemperatureAverage());
            Console.WriteLine("AveragePressure: {0}", statisticReport.GetPressureAverage());
            Console.WriteLine("AverageHumidity: {0}", statisticReport.GetHumidityAverage());

            currentConditionsReport.UnSubscribe(sender);
            sender.Notify(sender, weatherInfo);
            try
            {
                Console.WriteLine("Temperature: {0}", currentConditionsReport.WthrInfo.Temperature);
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Try again!!!");
            Thread.Sleep(3000);
            currentConditionsReport.Subscribe(sender);
            sender.Notify(sender, weatherInfo);
            Console.WriteLine("Temperature: {0}", currentConditionsReport.WthrInfo.Temperature);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("OpenWeatherAPI Example Application");
            Console.WriteLine();

            Console.WriteLine("Виберіть місто для перегляду погоди:");
            //var city = Console.ReadLine();
            string city1 = "Hrebinka";
            string city2 = "Kiev";
            string city3 = "London";

            Console.WriteLine();

            WeatherCity weather1 = new WeatherCity(city1);
            WeatherCity weather2 = new WeatherCity(city2);
            WeatherCity weather3 = new WeatherCity(city3);

            //створюємо Subject
            WeatherData sinopticUa = new WeatherData();

            //створюємо Observers
            CurrentConditionDisplay display1 = new CurrentConditionDisplay();
            StatisticsDisplay       display2 = new StatisticsDisplay();
            ForecastDisplay         display3 = new ForecastDisplay();

            //підписуємо підписників на sinopticUa
            sinopticUa.EventWeather += display1.OnNext;
            sinopticUa.EventWeather += display2.OnNext;
            sinopticUa.EventWeather += display3.OnNext;
            sinopticUa.EventWeather += new Action <WeatherCity>((new  ForecastDisplay()).OnNext);
            //sinopticUa.EventWeather += x=> {   new WeatherCity("Lviv"); };

            //Змінюємо показники погоди
            sinopticUa.SetMeasuremants(weather1);
            //Розсилаємо погоду
            sinopticUa.Notify();

            sinopticUa.SetMeasuremants(weather2);
            sinopticUa.Notify();
            sinopticUa.SetMeasuremants(weather3);
            sinopticUa.Notify();

            Console.WriteLine();

            display2.canUse          = false;           // display2 відписується від sinopticUa
            sinopticUa.EventWeather -= display1.OnNext; // sinopticUa відписує display1

            sinopticUa.Notify();


            //запишемо погоду в файл
            WeatherCreator creator = new WeatherCreator();

            creator.SaveToFile(weather3 as IWeatherCity, @"D:\WeatherCity.txt");

            List <IWeatherCity> arr = creator.ReadFromFile(@"D:\WeatherCity1.txt");

            Console.ReadLine();

            DAOWeather weather = new DAOWeather("myDB2");

            weather.PrintConnectionInfo();
            Console.WriteLine();
        }