private static WeatherDays InputDataFromConsole() { WeatherDays arrDaysParams; Console.WriteLine("Ввод из консоли"); int numberOfDays; while (true) { Console.WriteLine("Введите количество дней:"); string strNumberOfDays = Console.ReadLine(); try { numberOfDays = Convert.ToInt32(strNumberOfDays); break; } catch (Exception ex) { Console.WriteLine($"Вы не правильно ввели данные: {ex.Message}"); continue; } } arrDaysParams = new WeatherDays(numberOfDays); Console.WriteLine("В слудующих строчках вводите параметры погоды дня в формате:"); Console.WriteLine("f1 f2 f3 i s"); Console.WriteLine("f1 = Средняя температура днём - float"); Console.WriteLine("f2 = Средняя температура ночью - float"); Console.WriteLine("f3 = Средняе атмосферное давление - float (мм ртутного столба"); Console.WriteLine("i = Количество осадков - int (мм/день)"); Console.WriteLine("s = Тип погоды в этот день - string (ниже примеры типов погоды, вводить это значение необязательно)"); Console.WriteLine("Типы погоды: не_определено, дождь, кратковременный_дождь, гроза, снег, туман, хмуро, солнечно\n"); int count = 0; while (count != numberOfDays) { while (true) { Console.WriteLine($"Вводите параметры {count + 1} дня:"); string nextLine = Console.ReadLine(); string[] splitLine = nextLine.Split(" "); if (splitLine.Length != 4 & splitLine.Length != 5) { Console.WriteLine($"Данные введены неверно"); continue; } try { float AverageTemperaturePerDay = float.Parse(splitLine[0], System.Globalization.CultureInfo.InvariantCulture); float AverageTemperatureAtNight = float.Parse(splitLine[1], System.Globalization.CultureInfo.InvariantCulture); float AverageAtmosphericPressure = float.Parse(splitLine[2], System.Globalization.CultureInfo.InvariantCulture); int Precipitation = Convert.ToInt32(splitLine[3]); TypeOfWeather TypeOfWeatherAtDay = TypeOfWeather.не_определено; if (splitLine.Length == 5) { TypeOfWeatherAtDay = (TypeOfWeather)Enum.Parse(typeof(TypeOfWeather), splitLine[4], true); } WeatherParametersDay dayParams = new WeatherParametersDay(AverageTemperaturePerDay, AverageTemperatureAtNight, AverageAtmosphericPressure, Precipitation, TypeOfWeatherAtDay); arrDaysParams.insertDaySettings(count, dayParams); break; } catch (Exception ex) { Console.WriteLine($"Данные введены неверно"); Console.WriteLine($"Ошибка: {ex.Message}"); continue; } } count++; } return(arrDaysParams); }
private static WeatherDays InputDataFromFile() { WeatherDays arrDaysParams; Console.WriteLine("Ввод из файла"); Console.WriteLine("В первой строчке введите количество дней."); Console.WriteLine("В слудующих строчках вводите параметры погоды дня в формате:"); Console.WriteLine("f1 f2 f3 i s"); Console.WriteLine("f1 = Средняя температура днём - float"); Console.WriteLine("f2 = Средняя температура ночью - float"); Console.WriteLine("f3 = Средняе атмосферное давление - float (мм ртутного столба"); Console.WriteLine("i = Количество осадков - int (мм/день)"); Console.WriteLine("s = Тип погоды в этот день - string (ниже примеры типов погоды, вводить это значение необязательно)"); Console.WriteLine("Типы погоды: не_определено, дождь, кратковременный_дождь, гроза, снег, туман, хмуро, солнечно\n"); string PathToFile = "D:/OneDrive - ДонНУ/Рабочий стол/Univer/WeatherDays.txt"; //string PathToFile = "C:/Users/Admin/Desktop/Univer/WeatherDays.txt"; while (true) { bool restartRead = false; if (!File.Exists(PathToFile)) { Console.WriteLine($"Файл не найден, создайте файл WeatherDays.txt по пути {PathToFile}"); Console.WriteLine("Нажмите \"Enter\", если создали файл"); Console.ReadLine(); continue; } int numberOfDays; using (StreamReader fileData = new StreamReader(PathToFile)) { //StreamReader fileData = new StreamReader(PathToFile); string firstLine = fileData.ReadLine(); if (firstLine == null) { Console.WriteLine($"Файл {Path.GetFileName(PathToFile)} пустой, введите в него данные!"); fileData.Close(); Console.WriteLine("Нажмите \"Enter\", если изменили файл"); Console.ReadLine(); continue; } try { numberOfDays = Convert.ToInt32(firstLine); } catch (Exception ex) { Console.WriteLine($"Вы не правильно ввели данные: {ex.Message}"); fileData.Close(); Console.WriteLine("Нажмите \"Enter\", если изменили файл"); Console.ReadLine(); continue; } arrDaysParams = new WeatherDays(numberOfDays); int count = 0; while (count != numberOfDays) { string nextLine = fileData.ReadLine(); if (nextLine == null) { Console.WriteLine($"В строке {count + 2} пусто"); fileData.Close(); Console.WriteLine("Нажмите \"Enter\", если исправили строку"); Console.ReadLine(); restartRead = true; break; } string[] splitLine = nextLine.Split(" "); if (splitLine.Length != 4 & splitLine.Length != 5) { Console.WriteLine($"В строке {count + 2} данные введены неверно"); fileData.Close(); //Console.WriteLine($"count: {count}, splitLine.Length {splitLine.Length}"); Console.WriteLine("Нажмите \"Enter\", если исправили строку"); Console.ReadLine(); restartRead = true; break; } try { float AverageTemperaturePerDay = float.Parse(splitLine[0], System.Globalization.CultureInfo.InvariantCulture); float AverageTemperatureAtNight = float.Parse(splitLine[1], System.Globalization.CultureInfo.InvariantCulture); float AverageAtmosphericPressure = float.Parse(splitLine[2], System.Globalization.CultureInfo.InvariantCulture); int Precipitation = Convert.ToInt32(splitLine[3]); TypeOfWeather TypeOfWeatherAtDay = TypeOfWeather.не_определено; if (splitLine.Length == 5) { TypeOfWeatherAtDay = (TypeOfWeather)Enum.Parse(typeof(TypeOfWeather), splitLine[4], true); } WeatherParametersDay dayParams = new WeatherParametersDay(AverageTemperaturePerDay, AverageTemperatureAtNight, AverageAtmosphericPressure, Precipitation, TypeOfWeatherAtDay); arrDaysParams.insertDaySettings(count, dayParams); } catch (Exception ex) { Console.WriteLine($"В строке {count + 2} данные введены неверно"); Console.WriteLine($"Ошибка: {ex.Message}"); fileData.Close(); Console.WriteLine("Нажмите \"Enter\", если исправили строку"); Console.ReadLine(); restartRead = true; break; } count++; if (restartRead) { break; } } if (restartRead) { continue; } } break; } Console.WriteLine("\nФайл принят"); return(arrDaysParams); }