Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions mfdez1982/StringCalculator.groovy
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions mfdez1982/StringCalculatorTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import groovy.util.GroovyTestCaseclass 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") }}
Expand Down