diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..d24395c 100644 Binary files a/bin/StringCalculator.class and b/bin/StringCalculator.class differ diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f..cdc59ec 100644 Binary files a/bin/StringCalculatorTest.class and b/bin/StringCalculatorTest.class differ diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..4f0f4e0 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,27 @@ +import java.util.ArrayList; +import java.util.List; public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr - - // not yet implemented - return 0; + + public static int add(final String numbers) { + int returnValue = 0; + String[] numbersArray = numbers.split(",|n");; + List negativeNumbers = new ArrayList(); + for (String number : numbersArray) { + if (!number.trim().isEmpty()) { + int numberInt = Integer.parseInt(number.trim()); + if (numberInt < 0) { + negativeNumbers.add(numberInt); + } + returnValue += numberInt; + } + } + if (negativeNumbers.size() > 0) { + throw new RuntimeException("Negative Numbers are not allowed: " + negativeNumbers.toString()); + } + return returnValue; } } + + + diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..4b36623 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,71 @@ import static org.junit.Assert.*; +import org.junit.Assert; import org.junit.Test; public class StringCalculatorTest { - - @Test - public void test() { - fail("Not yet implemented"); - } - + //when more than 2 numbers are used (Failure case) +// @Test(expected = RuntimeException.class) +// public final void whenMoreThanTwoNumbersAreUsedThenThrowAnException() { +// StringCalculator.add("9,8,7"); +// } + //when 2 numbers are input (Success case) + @Test + public final void whenTwoNumbersAreInputThenNoExceptionIsThrown() { + StringCalculator.add("4,10"); + Assert.assertTrue(true); + } + //when non number is input (Failure case) + @Test(expected = RuntimeException.class) + public final void whenNonNumberIsInputThenThrowAnException() { + StringCalculator.add("6,a"); + } + //when empty string is input (Success case) + @Test + public final void whenEmptyStringInputIsUsedThenReturnOutputIsZero() { + Assert.assertEquals(0, StringCalculator.add("")); + } + + //when one number is used as input then output is that same number + @Test + public final void whenOneNumberInputIsUsedThenReturnedOutputIsThatSameNumber() { + Assert.assertEquals(1, StringCalculator.add("1")); + } + //when two numbers are input then output is their sum + @Test + public final void whenTwoNumbersAreInputThenOutputIsTheirSum() { + Assert.assertEquals(5+2, StringCalculator.add("5,2")); + } + + //when more than 2 numbers are used (Failure case) + //when multiple number of inputs are used then output is their sum + @Test + public final void whenMultipleNumberOfInputsAreUsedThenOutputIsTheirSum() { + Assert.assertEquals(10+20+3+40+50, StringCalculator.add("10,20,3,40,50")); + } + + //when new line is used between input numbers then output is their sum + @Test + public final void whenNewLineIsUsedBetweenInputNumbersThenOutputIsTheirSum() { + Assert.assertEquals(4+1+22, StringCalculator.add("4,1n22")); + } + //when negative number is in input (Failure case) + @Test(expected = RuntimeException.class) + public final void whenNegativeNumberIsInInputThenThrowRuntimeException() { + StringCalculator.add("10,-20,3,40,50"); + } + //when multiple negative numbers are in input (Failure case) + @Test + public final void whenMultipleNegativeNumbersAreInInputThenThrowRuntimeException() { + RuntimeException exception = null; + try { + StringCalculator.add("10,-20,3,-40,50"); + } catch (RuntimeException e) { + exception = e; + } + Assert.assertNotNull(exception); + Assert.assertEquals("Negative Numbers are not allowed: [-20, -40]", exception.getMessage()); + } } + +