public static DaysWeather ParseFileDataToWeatherData(string weatherFileData) { DaysWeather weatherData = new DaysWeather(); List <string> split = weatherFileData.Split().Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); weatherData.DayOfMonth = Convert.ToInt16(split[Utilities.dayOfMonthIndex]); weatherData.DayOfWeek = split[Utilities.dayOfWeekIndex].Trim(); weatherData.HighCelcius = Convert.ToDouble(split[Utilities.highCelciusIndex]); weatherData.LowCelsius = Convert.ToDouble(split[Utilities.lowCelciusIndex]); weatherData.ChanceOfPrecipitation = Convert.ToInt16(split[Utilities.chanceOfPrecipitationIndex]); return(weatherData); }
public List <String> GetPicnicPlan(int?numberOfDays = null) { List <DaysWeather> normalizedDaysWeather = new List <DaysWeather>(); List <String> picnicPlanDays = new List <string>(); DateTime datevalue; if (_filename != null && File.Exists(_filename)) { try { List <String> lines = File.ReadLines(_filename).ToList(); string headerLine = lines.FirstOrDefault(); if (headerLine != null && headerLine.Contains(Utilities.dayOfMonth)) { List <string> header = headerLine.Split().Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); Utilities.highCelciusIndex = header.IndexOf(Utilities.highCelcius); Utilities.lowCelciusIndex = header.IndexOf(Utilities.lowCelcius); Utilities.chanceOfPrecipitationIndex = header.IndexOf(Utilities.chanceOfPrecipitation); Utilities.dayOfMonthIndex = header.IndexOf(Utilities.dayOfMonth); Utilities.dayOfWeekIndex = header.IndexOf(Utilities.dayOfWeek); foreach (var line in lines.Skip(1)) { DaysWeather dayWeather = Utilities.ParseFileDataToWeatherData(line); if ((dayWeather.HighCelcius >= _lowTemperatureInC) && (dayWeather.HighCelcius <= _highTemperatureInC) && (dayWeather.LowCelsius >= _lowTemperatureInC) && (dayWeather.LowCelsius <= _highTemperatureInC)) { normalizedDaysWeather.Add(dayWeather); datevalue = new DateTime(2017, 11, normalizedDaysWeather.FirstOrDefault().DayOfMonth); } } if (normalizedDaysWeather?.Count > 0) { List <DaysWeather> bestDaysWeather = new List <DaysWeather>(); if (numberOfDays.HasValue && numberOfDays > 0) { bestDaysWeather = PickBestNDaysWeather(normalizedDaysWeather, numberOfDays.GetValueOrDefault()); } else { var groupedDaysWeather = normalizedDaysWeather.OrderBy(x => x.ChanceOfPrecipitation) .GroupBy(x => x.ChanceOfPrecipitation) .Select(grp => grp.ToList()) .ToList(); bestDaysWeather = groupedDaysWeather.FirstOrDefault(); } foreach (DaysWeather dw in bestDaysWeather) { datevalue = new DateTime(2017, 11, dw.DayOfMonth); picnicPlanDays.Add($"{datevalue.ToString("dddd")} the {dw.DayOfMonth}th day of the month is the best day for a picnic."); } } } else { Console.WriteLine($"File header format is not correct to parse the file : {_filename}"); } } catch (Exception ex) { Console.WriteLine($"Error occured while planning the Picnic Plan : {ex.Message} \r\n {ex.InnerException}"); } } else { Console.WriteLine(_filename == null ? "File path is missing in the Config" : $"File does not exist in the path {_filename}"); } return(picnicPlanDays); }