public CalculatorResult CalculateSum(string input) { var delimiters = _delimiterParser.Parse(input); var values = _dataParser.Parse(input, delimiters); values = _constraints.ApplyConstraints(values); _validationService.Validate(values); var total = values.Sum(); var formula = $"{string.Join("+", values)} = {total}"; var result = new CalculatorResult() { Formula = formula, Value = total }; 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)); }
public int Add(string stringNumbers) { if (string.IsNullOrEmpty(stringNumbers)) { return(0); } _delimiters.AddRange(_delimiterParser.Parse(stringNumbers)); IEnumerable <int> parsedNumbers = ParseStringNumbers(RemoveCustomDelimiterPrefix(stringNumbers)); CheckForNegatives(parsedNumbers); int sum = parsedNumbers.Where(n => n > 0 && n < MAX_VALID_NUMBER).Sum(); WriteToConsole(sum); return(sum); }