/// <summary> /// Validates all parameters. /// </summary> /// <param name="parameters">Input parameters.</param> public void ValidateParameters(FileCabinetServiceContext parameters) { foreach (var validator in this.validators) { validator.ValidateParameters(parameters); } }
/// <summary> /// Get last name from string. /// </summary> /// <param name="record">Input FileCabinetServiceContext object.</param> /// <param name="parameters">Input last name parameter.</param> public static void GetLastName(FileCabinetServiceContext record, string parameters) { if (record == null) { throw new ArgumentNullException(nameof(record)); } record.LastName = parameters; }
/// <summary> /// checks the string for null and empty. /// </summary> /// <param name="parameters">Input parameters.</param> /// <exception cref="ArgumentException">Thrown when string is not correct length.</exception> public void ValidateParameters(FileCabinetServiceContext parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (parameters.FirstName.Length <this.minLength | parameters.FirstName.Length> this.maxLength) { throw new ArgumentException("Invalid length.", $"{nameof(parameters.FirstName)}"); } }
/// <summary> /// Checks age for a negative value. /// </summary> /// <param name="parameters">Input parameters.</param> /// <exception cref="ArgumentException">thrown when <paramref name="parameters.Age"/> less than 0. </exception> public void ValidateParameters(FileCabinetServiceContext parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (parameters.Age < this.min || parameters.Age > this.max) { throw new ArgumentException("Invalid Age", $"{nameof(parameters.Age)}"); } }
/// <summary> /// checks the date of birth range. /// </summary> /// <param name="parameters">Input parameters.</param> /// <exception cref="ArgumentException">thrown when <paramref name="parameters.DateOfBirth"/> is the wrong range.</exception> public void ValidateParameters(FileCabinetServiceContext parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (DateTime.Compare(this.to, parameters.DateOfBirth) < 0 || DateTime.Compare(this.from, parameters.DateOfBirth) > 0) { throw new ArgumentException("Invalid Date", $"{nameof(parameters.DateOfBirth)}"); } }
/// <summary> /// checks gender. /// </summary> /// <param name="parameters">Input parameters.</param> /// <exception cref="ArgumentException">Thrown, when gender not defined.</exception> public void ValidateParameters(FileCabinetServiceContext parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (!this.gender.Contains(parameters.Gender)) { throw new ArgumentException("There is no such gender", $"{nameof(parameters.Gender)}"); } }
/// <summary> /// Checks salary for a negative value. /// </summary> /// <param name="parameters">Input parameters.</param> /// <exception cref="ArgumentException">Thrown, when user entered a negative amount.</exception> public void ValidateParameters(FileCabinetServiceContext parameters) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (parameters.Salary > this.max) { throw new ArgumentException("You entered too much salary", $"{nameof(parameters.Salary)}"); } }
/// <summary> /// Get age from string. /// </summary> /// <param name="record">Input FileCabinetServiceContext object.</param> /// <param name="parameters">Input age parameter.</param> public static void GetAge(FileCabinetServiceContext record, string parameters) { if (record == null) { throw new ArgumentNullException(nameof(record)); } var isConverted = short.TryParse(parameters, out short age); if (!isConverted) { throw new FormatException("Wrong Age format."); } record.Age = age; }
/// <summary> /// Get date of birth from string. /// </summary> /// <param name="record">Input FileCabinetServiceContext object.</param> /// <param name="parameters">Input date of birth parameter.</param> public static void GetDateOfBirth(FileCabinetServiceContext record, string parameters) { if (record == null) { throw new ArgumentNullException(nameof(record)); } var isConverted = DateTime.TryParseExact(parameters, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateOfBirth); if (!isConverted) { throw new FormatException("Wrong Date of Birth format."); } record.DateOfBirth = dateOfBirth; }
/// <summary> /// Get gender from string. /// </summary> /// <param name="record">Input FileCabinetServiceContext object.</param> /// <param name="parameters">Input gender parameter.</param> public static void GetGender(FileCabinetServiceContext record, string parameters) { if (record == null) { throw new ArgumentNullException(nameof(record)); } var isConverted = char.TryParse(parameters, out char gender); if (!isConverted) { throw new FormatException("Wrong Gender format."); } record.Gender = gender; }
/// <summary> /// Get salary from string. /// </summary> /// <param name="record">Input FileCabinetServiceContext object.</param> /// <param name="parameters">Input salary parameter.</param> public static void GetSalary(FileCabinetServiceContext record, string parameters) { if (record == null) { throw new ArgumentNullException(nameof(record)); } var isConverted = decimal.TryParse(parameters, out decimal salary); if (!isConverted) { throw new FormatException("Wrong Salary format."); } record.Salary = salary; }
private void Update(string parameters) { try { if (string.IsNullOrEmpty(parameters)) { throw new ArgumentNullException(parameters); } if (!IsRightCommandSyntax(ParametersTemplate, parameters)) { throw new FormatException(InvalidFormat); } var parametersArray = parameters.Trim().Split(Separator, StringSplitOptions.RemoveEmptyEntries); var keyValuePairs = GetKeyValuePairs(parametersArray[BeforeWhere].Replace("set", string.Empty, StringComparison.OrdinalIgnoreCase).Trim()); foreach (var pair in keyValuePairs) { var index = Array.FindIndex(this.commands, j => j.Item1.Equals(pair.key, StringComparison.InvariantCultureIgnoreCase)); if (index >= 0) { this.commands[index].Item2(this.recordContext, pair.value); } else { throw new FormatException(InvalidFormat); } } if (parametersArray[AfterWhere].Contains(Or, StringComparison.InvariantCultureIgnoreCase) || parametersArray[AfterWhere].Contains(And, StringComparison.InvariantCultureIgnoreCase)) { var separators = new string[] { Or, And }; var isContainsAnd = this.find.ContainsAnd(parametersArray[AfterWhere], And); var parametersArrayAfterWhere = parametersArray[AfterWhere].Trim().Split(' '); var countSeparators = 0; var splitArrayAfterParameters = parametersArrayAfterWhere .Select(item => separators .Contains(item) ? item.Replace(item, ",", StringComparison.InvariantCultureIgnoreCase) : item).ToArray(); for (int i = 0; i < parametersArrayAfterWhere.Length; i++) { if (parametersArrayAfterWhere[i] != splitArrayAfterParameters[i]) { countSeparators++; } } if (countSeparators > 1) { throw new FormatException(InvalidFormat); } var valueAndProperty = string.Join(string.Empty, splitArrayAfterParameters); keyValuePairs = GetKeyValuePairs(valueAndProperty); var parametersAfterWhere = string.Join(string.Empty, parametersArray[AfterWhere]).Trim(); foreach (var pair in keyValuePairs) { this.find.FindRecordByKey(pair.key, pair.value); } if (isContainsAnd) { foreach (var pair in keyValuePairs) { this.list = this.find.FindRecordByMatchingCriteria(pair.key, pair.value); } } } else { keyValuePairs = GetKeyValuePairs(parametersArray[AfterWhere].Trim()); foreach (var pair in keyValuePairs) { this.find.FindRecordByKey(pair.key, pair.value); } } if (this.list.Count == 0) { Console.WriteLine("Records with the specified criteria do not exist"); return; } foreach (var record in this.list) { var context = new FileCabinetServiceContext(); record.Id = record.Id; context.FirstName = this.recordContext.FirstName ?? record.FirstName; context.LastName = this.recordContext.LastName ?? record.LastName; context.DateOfBirth = this.recordContext.DateOfBirth ?? record.DateOfBirth; context.Salary = this.recordContext.Salary ?? record.Salary; context.Age = this.recordContext.Age ?? record.Age; context.Gender = this.recordContext.Gender ?? record.Gender; this.service.EditRecord(record.Id, context); } StringBuilder builder = new StringBuilder(); var sortedRecords = this.list.OrderBy(record => record.Id).ToList(); for (int j = 0; j < sortedRecords.Count; j++) { if (j + 1 == sortedRecords.Count) { builder.Append($"# {sortedRecords[j].Id} "); } else { builder.Append($"# {sortedRecords[j].Id}, "); } } if (sortedRecords.Count == 1) { Console.WriteLine($"Record {builder.ToString()}is updated."); } else { Console.WriteLine($"Records {builder.ToString()}are updated."); } } catch (ArgumentNullException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(ex.Message); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (IndexOutOfRangeException) { Console.WriteLine(InvalidFormat); } }