public static void AddUpdateForecast(Dictionary <string, Forecast> forecasts, Match forecastData) { string cityName = forecastData.Groups[1].ToString(); string temp = forecastData.Groups[2].ToString(); string weather = forecastData.Groups[3].ToString(); if (!forecasts.ContainsKey(cityName)) { forecasts[cityName] = new Forecast(cityName, temp, weather); } else { forecasts[cityName].Temperature = temp; forecasts[cityName].Weather = weather; } }
public static void Main() { var input = Console.ReadLine(); var forecastList = new List <Forecast>(); var regex = new Regex(@"([A-Z]{2})(\d+\.\d+)([A-Za-z]+)\|"); while (input != "end") { var match = regex.Match(input); if (match.Success) { var nameOfCity = match.Groups[1].ToString(); var averageTemperature = double.Parse(match.Groups[2].ToString()); var typeOfWeather = match.Groups[3].ToString(); var tempOrWeatherExists = forecastList .FindIndex(t => t.NameOfCity == nameOfCity); if (tempOrWeatherExists >= 0) { forecastList[tempOrWeatherExists].AverageTemperature = averageTemperature; forecastList[tempOrWeatherExists].NameOfCity = nameOfCity; forecastList[tempOrWeatherExists].TypeOfWeather = typeOfWeather; } else { var forecastClassInfo = new Forecast(nameOfCity, averageTemperature, typeOfWeather); forecastList.Add(forecastClassInfo); } } input = Console.ReadLine(); } PrintForecasts(forecastList); }