static void Main(string[] args) { // create the factory of services WeatherDataServiceFactory factory = new WeatherDataServiceFactory(); // get one service from the factory by requested type IWeatherDataService weatherService = factory.GetWeatherDataService(WeatherDataServiceFactory.ServiceType.OPEN_WEATHER_MAP); // use the weather service Location location = new Location("london", "UK"); // get the current weather WeatherData weatherData = weatherService.GetWeatherData(location); // print the weather data Console.WriteLine(weatherData.ToString()); }
static void Main(string[] args) { // create the factory of services WeatherDataServiceFactory factory = new WeatherDataServiceFactory(); // get one service from the factory by requested type IWeatherDataService weatherService = factory.GetWeatherDataService(WeatherDataServiceFactory.ServiceType.OpenWeatherMap); Console.WriteLine("*************************************************"); try { // create location object to get weather in Location location = new Location("London", "UK"); // get the current weather WeatherData weatherData = weatherService.GetWeatherData(location); // print the weather data Console.WriteLine(weatherData.ToString()); } catch (WeatherDataServiceException e) { Console.WriteLine(e.Message); } Console.WriteLine("*************************************************"); Console.WriteLine("\nDo you want to try get weather in your current location ?"); Console.WriteLine("Press 'C' to continue or 'E' to exit."); string key = Console.ReadLine(); while (key != null && key.Equals("c", StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("Enter your city name : "); string city = Console.ReadLine(); Console.WriteLine("Enter your country name : "); string country = Console.ReadLine(); Console.WriteLine("\n*************************************************\n"); try { WeatherData weatherData = weatherService.GetWeatherData(new Location(city, country)); Console.WriteLine(weatherData.ToString()); } catch (WeatherDataServiceException e) { Console.WriteLine(e.Message); } Console.WriteLine("*************************************************\n"); Console.WriteLine("Press 'C' to continue or 'E' to exit."); key = Console.ReadLine(); } }
/// <summary> /// Get the weather data due to location /// </summary> /// <param name="location"></param> /// <exception cref="WeatherDataServiceException"></exception> /// <returns> The weather data object </returns> public WeatherData GetWeatherData(Location location) { if (location == null) { throw new WeatherDataServiceException("location is null"); } Console.WriteLine("The service got a request to return the current weather in this location - \n" + "City : " + location.City + "\nCountry : " + location.Country); try { // create the url string string url = "http://api.openweathermap.org/data/2.5/weather?q=" + location.City + "," + location.Country + "&mode=xml"; // send the url to parser and get from it the weather data object IParser parser = new XmlParser(url); return parser.ParseDocument(); } catch (WeatherDataServiceException e) { Console.WriteLine(e.Message); throw new WeatherDataServiceException("url not found, try to change location."); } }
public void GetWeatherDataValueTest() { // get the weather data in London using the GetWeatherData function WeatherDataServiceFactory factory = new WeatherDataServiceFactory(); IWeatherDataService weatherService = factory.GetWeatherDataService(WeatherDataServiceFactory.ServiceType.OpenWeatherMap); Location locationTest = new Location("London", "UK"); WeatherData weatherDataToTest = weatherService.GetWeatherData(locationTest); // get the weather data in London from the website string url = "http://api.openweathermap.org/data/2.5/weather?q=" + locationTest.City + "," + locationTest.Country + "&mode=xml"; WeatherData weatherSrc = GetWeatherByUrlForTesting(url); Assert.IsTrue(weatherSrc.Equals(weatherDataToTest)); }