/// <summary> /// Initializes a new instance of the <see cref="Options"/> class. /// </summary> public Options() { if (int.TryParse(Configurator.GetSetting("DefaultRecordsAmount"), out int amount) && int.TryParse(Configurator.GetSetting("DefaultStartId"), out int id)) { this.RecordsAmount = amount; this.StartId = id; } else { Console.WriteLine(Configurator.GetConstantString("InvalidAppSettingFile")); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); Environment.Exit(-1); } }
private static FileCabinetRecord[] GenerateRandomRecords(int start, int amount) { if (start < 1) { throw new ArgumentOutOfRangeException(nameof(start), Configurator.GetConstantString("IndexLess1")); } IConfigurationRoot validationRules = null; try { validationRules = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile(Configurator.GetSetting("ValidationRulesFileName")) .Build(); } catch (FileNotFoundException) { Console.WriteLine($"{Configurator.GetConstantString("MissingValidation")} {Configurator.GetSetting("ConstantStringsFileName")}"); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); Environment.Exit(-1); } catch (FormatException) { Console.WriteLine(Configurator.GetConstantString("InvalidValidationFile")); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); Environment.Exit(-1); } int minFirstNameLength = int.Parse(validationRules.GetSection("default").GetSection("firstName").GetSection("minLength").Value, CultureInfo.InvariantCulture); int maxFirstNameLength = int.Parse(validationRules.GetSection("default").GetSection("firstName").GetSection("maxLength").Value, CultureInfo.InvariantCulture); int minLastNameLength = int.Parse(validationRules.GetSection("default").GetSection("lastName").GetSection("minLength").Value, CultureInfo.InvariantCulture); int maxLastNameLength = int.Parse(validationRules.GetSection("default").GetSection("lastName").GetSection("maxLength").Value, CultureInfo.InvariantCulture); DateTime fromDateOfBirth = DateTime.ParseExact(validationRules.GetSection("default").GetSection("dateOfBirth").GetSection("from").Value, Configurator.GetConstantString("DateFormatDM"), CultureInfo.InvariantCulture); DateTime toDateOfBirth = DateTime.ParseExact(validationRules.GetSection("default").GetSection("dateOfBirth").GetSection("to").Value, Configurator.GetConstantString("DateFormatDM"), CultureInfo.InvariantCulture); char minPatronymicLetter = char.Parse(validationRules.GetSection("default").GetSection("patronymicLetter").GetSection("from").Value); char maxPatronymicLetter = char.Parse(validationRules.GetSection("default").GetSection("patronymicLetter").GetSection("to").Value); decimal minIncome = decimal.Parse(validationRules.GetSection("default").GetSection("income").GetSection("from").Value, CultureInfo.InvariantCulture); decimal maxIncome = decimal.Parse(validationRules.GetSection("default").GetSection("income").GetSection("to").Value, CultureInfo.InvariantCulture); short minHeight = short.Parse(validationRules.GetSection("default").GetSection("height").GetSection("min").Value, CultureInfo.InvariantCulture); short maxHeight = short.Parse(validationRules.GetSection("default").GetSection("height").GetSection("max").Value, CultureInfo.InvariantCulture); string alphabet = Configurator.GetConstantString("Alphabet"); FileCabinetRecord[] records = new FileCabinetRecord[amount]; Random random = new Random(); for (int i = 0; i < amount; i++) { records[i] = new FileCabinetRecord { Id = start++, FirstName = GetRandomString(alphabet, random.Next(minFirstNameLength, maxFirstNameLength), random), LastName = GetRandomString(alphabet, random.Next(minLastNameLength, maxLastNameLength), random), DateOfBirth = GetRandomDate(fromDateOfBirth, toDateOfBirth, random), PatronymicLetter = (char)random.Next((int)minPatronymicLetter, (int)maxPatronymicLetter), Income = random.Next((int)minIncome, (int)maxIncome - 1) + (((decimal)random.Next(0, 100)) / 100), Height = (short)random.Next(minHeight, maxHeight), }; } return(records); }