diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..71e41b2 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,59 @@ public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr + + static final int[] validNumbers = {0, 1, 2}; + + // Returns the sum of the numbers given in numbersStr + public int add(String numbersStr) throws StringCalculatorException { + int [] numbers; + int result; - // not yet implemented - return 0; + if (numbersStr.length() == 0) { + return 0; + } + + numbers = StringArrayToInt(numbersStr.split(",|\\n")); + result = calculateNumbers(numbers); + + return result; + } + + private int[] StringArrayToInt(String[] strNumbers) throws StringCalculatorException { + int[] intNumbers = new int[strNumbers.length]; + int tempInt; + for (int i = 0; i < strNumbers.length; i++) { + + // Try converting part of the string to a integer + try { + tempInt = Integer.parseInt(strNumbers[i]); + } + catch(Exception e) { + throw (new StringCalculatorException()); + } + + validateNumber(tempInt); + intNumbers[i] = tempInt; + + } + + return intNumbers; } + + private void validateNumber(int number) throws StringCalculatorException { + for (int i: validNumbers) { + if (i == number) { + return; + } + } + throw (new StringCalculatorException()); + } + + private int calculateNumbers(int[] numbers) { + int sum = 0; + for (int x: numbers) { + sum += x; + } + return sum; + } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..46c06c9 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -4,9 +4,79 @@ public class StringCalculatorTest { + StringCalculator calc = new StringCalculator(); + + @Test + public void StringCalculatorTest_EmptyString() throws StringCalculatorException { + int result = calc.add(""); + assertEquals("Empty string returns wrong value", 0, result); + } + + @Test + public void StringCalculatorTest_NonEmptyString() throws StringCalculatorException { + int result = calc.add("1,2"); + assertTrue("Non Empty string returns wrong value", 0 != result); + } + + @Test(expected=StringCalculatorException.class) + public void StringCalculatorTest_invalid_string() throws StringCalculatorException { + int result = calc.add("asdasd"); + } + + @Test(expected=StringCalculatorException.class) + public void StringCalculatorTest_invalid_numbers() throws StringCalculatorException { + int result = calc.add("3,2"); + } + + @Test + public void StringCalculatorTest_1_2_3() throws StringCalculatorException { + int result = calc.add("1,2"); + assertTrue("1 + 2 returns a wrong answer " + result, result == 3); + } + @Test - public void test() { - fail("Not yet implemented"); + public void StringCalculatorTest_1_2_2_5() throws StringCalculatorException { + int result = calc.add("1,2,2"); + assertTrue("1 + 2 + 2 returns a wrong answer " + result, result == 5); + } + + @Test + public void StringCalculatorTest_1_0_0_1() throws StringCalculatorException { + int result = calc.add("1,0,0"); + assertTrue("1 + 0 + 0 returns a wrong answer " + result, result == 1); + } + + @Test + public void StringCalculatorTest_0_0_0() throws StringCalculatorException { + int result = calc.add("0,0"); + assertTrue("0 + 0 returns a wrong answer " + result, result == 0); + } + + @Test + public void StringCalculatorTest_0() throws StringCalculatorException { + int result = calc.add("0"); + assertTrue("0 returns a wrong answer " + result, result == 0); + } + + @Test(expected=StringCalculatorException.class) + public void StringCalculatorTest_negative() throws StringCalculatorException { + int result = calc.add("-1,0"); + } + + @Test + public void StringCalculatorTest_1_1_1_1_1_5() throws StringCalculatorException { + int result = calc.add("1,1,1,1,1"); + assertTrue("1+1+1+1+1 returns a wrong answer " + result, result == 5); + } + + @Test + public void StringCalculatorTest_1_2_3_new_line() throws StringCalculatorException { + int result = calc.add("1\n2"); + assertTrue("1 + 2 with new line returns a wrong answer " + result, result == 3); + } + @Test(expected=StringCalculatorException.class) + public void StringCalculatorTest_invalid_input_new_line() throws StringCalculatorException { + int result = calc.add("1,\n"); } }