diff --git a/mfdez1982/StringCalculator.groovy b/mfdez1982/StringCalculator.groovy new file mode 100644 index 0000000..6b10edd --- /dev/null +++ b/mfdez1982/StringCalculator.groovy @@ -0,0 +1,47 @@ +class StringCalculator +{ + def input + def separatorSet = '\n,' + def SEP_HEADER_PATTERN = '//(.+)\n' + + Integer add(String stringNumbers) + { + input = stringNumbers + parseAndRemoveSeparatorHeader() + if (input.contains('-')) + parseNegativeNumbersAndThrowException(); + else + { + replaceSeparatorsWithSpaces() + return add() + } + } + + private Integer add() + { + Integer total = 0; + for (String number : input.split()) + total += number.toInteger()<1000 ? number.toInteger() : 0 + return total + } + + private void parseAndRemoveSeparatorHeader() + { + separatorSet += input.find(SEP_HEADER_PATTERN){ match, sep -> return sep}; + input = input - ~SEP_HEADER_PATTERN + } + + private void replaceSeparatorsWithSpaces() + { + input = input.tr(separatorSet, ' ') + } + + private void parseNegativeNumbersAndThrowException() + { + def exceptionMessage = 'negatives not allowed: ' + def matcher = input =~ /(-\d*)/ + for(String[] match : matcher) + exceptionMessage += ' ' + match[0] + throw new Exception(exceptionMessage) + } +} \ No newline at end of file diff --git a/mfdez1982/StringCalculatorTest.groovy b/mfdez1982/StringCalculatorTest.groovy new file mode 100644 index 0000000..9beb8d3 --- /dev/null +++ b/mfdez1982/StringCalculatorTest.groovy @@ -0,0 +1 @@ +import groovy.util.GroovyTestCase class StringCalculatorTest extends GroovyTestCase { def calculator = new StringCalculator() void testEmptyString() { assertEquals 0, calculator.add("") } void testZero() { assertEquals 0, calculator.add("0") } void testOne() { assertEquals 1, calculator.add("1") } void testZeroPlusOne() { assertEquals 1, calculator.add("0,1") } void testOnePlusTwo() { assertEquals 3, calculator.add("1,2") } void testWithThreeNumbers() { assertEquals 6, calculator.add("1,2,3") } void testWithUndeterminedNumberOfNumbers() { assertEquals 15, calculator.add("0,1,2,3,4,5") } void testNewLineSeparator() { assertEquals 3, calculator.add("1\n2") } void testComaAndNewLineSeparatorMixed() { assertEquals 15, calculator.add("1\n2,3,4\n5") } void testCustomSeparator() { assertEquals 3, calculator.add("//;\n1;2") } void testOnlyNegativeNumberNotAllowed() { def msg = shouldFail {calculator.add("-1")} println msg } void testNegativeNumberNotAllowed() { def msg = shouldFail {calculator.add("8,-6,9")} println msg } void testSeveralNegativeNumberNotAllowed() { def msg = shouldFail {calculator.add("8,-6,9,-3")} println msg } void testNumbersBiggerThanThousandShouldBeIgnored() { assertEquals 4, calculator.add("1,2000,3") } void testCustomSeparatorOfVariableLength() { assertEquals 4, calculator.add("//[***]\n1***2000***3") } void testMultipleSeparators() { assertEquals 10, calculator.add("//[*][%][+]\n1*2%3+4") } void testMultipleSeparatorsWithVariableLength() { assertEquals 10, calculator.add("//[***][%][+]\n1***2%3+4") } void testMultipleSeparatorsWithVariableLengthAndNumbersBiggerThanThousand() { assertEquals 8, calculator.add("//[***][%%][++++]\n1***2000%%3++++4") } } \ No newline at end of file