private bool IsCollectionValid(string[] inputArray) { bool collectionIsEmpty = !inputArray.Any(); if (collectionIsEmpty) { throw new ArgumentException(DisplayController.SetMessageColor(ErrorEmptyCollection, DisplayController.Color.DarkRed), nameof(inputArray)); } return(true); }
private bool IsInputValid(IEnumerable <string> inputArray, CultureInfo currentCulture) { foreach (var element in inputArray) { bool canInputBeParsedToDate = TryParseExactToDate(element, currentCulture, out DateTime date); if (!canInputBeParsedToDate) { throw new FormatException(DisplayController.SetMessageColor(ErrorWrongInputFormat(element, currentCulture), DisplayController.Color.DarkRed)); } } return(true); }
private bool IsDatesOrderAscending(IEnumerable <DateTime> dateArray) { DateTime?previousDate = null; foreach (var date in dateArray) { bool previousDateIsLaterThanNextDate = previousDate > date; if (previousDateIsLaterThanNextDate) { throw new ArgumentException(DisplayController.SetMessageColor( ErrorUnexpectedDateOrder(previousDate?.ToShortDateString(), date.ToShortDateString()), DisplayController.Color.DarkRed)); } previousDate = date; } return(true); }
public DateTime[] CheckInputArray(string[] inputArray, CultureInfo currentCulture) { this._validationCriteriaPredicate = stringArray => IsCollectionValid(inputArray); this._validationCriteriaPredicate += stringArray => IsInputValid(inputArray, currentCulture); ConversionController converter = new ConversionController(currentCulture); DateTime[] dateArray = converter.ProcessInputArray(inputArray); this._validationCriteriaPredicate += stringArray => IsDatesOrderAscending(dateArray); bool isValid = IsEntireValidationSucceed(this._validationCriteriaPredicate, inputArray); if (!isValid) { throw new ValidationException(DisplayController.SetMessageColor(ErrorValidationFailed, DisplayController.Color.DarkRed)); } return(dateArray); }
public void Start(string[] inputArray) { CultureInfo currentCulture = CultureInfo.CurrentCulture; ValidationController validator = new ValidationController(); try { DateRangeFactory dateRange = new DateRangeFactory(validator); IDateRange result = dateRange.From(inputArray, currentCulture); DisplayController.Display(DisplayController.SetMessageColor(MonitOperationSucceed, DisplayController.Color.DarkGreen)); DisplayController.Display(result.ToString()); } catch (Exception exception) { DisplayController.Display(exception.Message); } Console.ReadKey(); }
private DateTime[] ConvertStringsToDateTime(IReadOnlyList <string> inputArray) { bool collectionNotExist = Equals(inputArray, null); if (collectionNotExist) { throw new ArgumentNullException(nameof(inputArray), DisplayController.SetMessageColor(ErrorNullCollection, DisplayController.Color.DarkRed)); } int inputArrayLength = inputArray.Count; DateTime[] convertedDateArray = new DateTime[inputArrayLength]; DateTime date; for (int i = 0; i < inputArrayLength; i++) { ValidationController.TryParseExactToDate(inputArray[i], this._currentCulture, out date); convertedDateArray[i] = date; } return(convertedDateArray); }