示例#1
0
        public int Calculate()
        {
            var numbers = _inputParser1.ParseOrPassToNextInputParserIfNeeded(Input);

            if (!_numberValidator.IsValid(numbers))
            {
                throw new ArgumentException(
                          $"{_numberValidator.ViolatedConditionName}: {string.Join(',', _numberValidator.InvalidNumbers)}");
            }

            ParsedNumbers = _numberFilter.Filter(numbers);

            Result = ParsedNumbers?.Sum() ?? 0;

            return(Result);
        }
        /// <summary>
        /// Returns the sum of a set of delimited numbers.
        /// </summary>
        /// <param name="numbers">A string containing delimited numbers. This should contain one or more lines.</param>
        /// <returns>The sum.</returns>
        public int Add(string numbers)
        {
            var delimiters = _delimiterParser.Parse(numbers);

            var lines = _numberLinesParser.GetLines(numbers);

            var linesOfNumbers = lines.Select(line => _numbersParser.Parse(line, delimiters));

            var filteredNumbers = linesOfNumbers.Select(line => _numberFilter.Filter(line));

            var detectedNumbers = filteredNumbers.Select(nums => _numberDetector.Detect(nums)).SelectMany(arr => arr);

            if (detectedNumbers.Any())
            {
                throw new InvalidNumbersException($"Invalid numbers encountered", detectedNumbers);
            }

            return(_numberAdder.Add(filteredNumbers));
        }