diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..9a09888 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..e9eabbc 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..9d59a7b 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,59 @@ +import java.util.Scanner; +import java.util.stream.IntStream; public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr - - // not yet implemented - return 0; + + public int add(String numbersStr) throws StringCalculatorException { + String[] strArray = toStringArray(numbersStr); + int[] intArray = toIntArray(strArray); + int sum = IntStream.of(intArray).sum(); + return sum; } -} + + public String[] toStringArray (String numbersStr) throws StringCalculatorException { + String text1 = handleUnknownNumbers(numbersStr); + String text2 = handleEmptyString(text1); + String text3 = handleLineBreakAndComma(text2); + String text4 = handleAlphabeticAndNegative(text3); + String[] split = text4.split(","); + return split; + } + + public int[] toIntArray (String[] arr) { + int[] ints = new int [arr.length]; + for (int i = 0; i < arr.length; i++) { + ints[i] = Integer.parseInt(arr[i]); + } + return ints; + } + + public String handleLineBreakAndComma (String numbersStr) throws StringCalculatorException{ + if (numbersStr.contains(",\n")) { + throw new StringCalculatorException(); + }else{ + String text = numbersStr.replace("\n", ","); + return text; + } + } + public String handleEmptyString (String numbersStr) { + if (numbersStr == "0") { + return ""; + }else { + return numbersStr; + } + } + public String handleUnknownNumbers(String numbersStr) throws StringCalculatorException { + if (numbersStr.length() > 3 && numbersStr.contains("\n") == false) { + throw new StringCalculatorException(); + }else { + return numbersStr; + } + } + public String handleAlphabeticAndNegative(String numbersStr) throws StringCalculatorException { + if (numbersStr.matches("[0-9,]+") == false) { + throw new StringCalculatorException(); + }else { + return numbersStr; + } + } +} \ No newline at end of file diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..0856125 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,4 @@ public class StringCalculatorException extends Exception { - + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..619b07b 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -5,8 +5,158 @@ public class StringCalculatorTest { @Test - public void test() { - fail("Not yet implemented"); + public void testOneNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String[] result = {"1"}; + int[] intArray = calculator.toIntArray(result); + int[] ints2 = {1}; + //Assert + assertArrayEquals(intArray, ints2); + } + + @Test + public void testTwoIntNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String[] result = {"1","2"}; + int[] intArray = calculator.toIntArray(result); + int[] ints2 = {1,2}; + //Assert + assertArrayEquals(intArray, ints2); + } + + @Test + public void testTwoStringNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,2"; + String[] stringArray = calculator.toStringArray(result); + String[] stringArray2 = {"1", "2"}; + //Assert + assertArrayEquals(stringArray, stringArray2); + } + + @Test + public void testThreeStringNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1\n2,3"; + String[] stringArray = calculator.toStringArray(result); + String[] stringArray2 = {"1","2","3"}; + //Assert + assertArrayEquals(stringArray, stringArray2); + } + + @Test + public void testLineBreak() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1\n2,3"; + String string = calculator.handleLineBreakAndComma(result); + String string2 = "1,2,3"; + //Assert + assertEquals(string, string2); + } + + @Test (expected = StringCalculatorException.class) + public void testComma() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,\n2,3"; + // This statement should cause an exception + calculator.handleLineBreakAndComma(result); + } + @Test + public void testEmptyString() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "0"; + String string = calculator.handleEmptyString(result); + String string2 = ""; + //Assert + assertEquals(string, string2); + System.out.println(string); + } + + @Test (expected = StringCalculatorException.class) + public void testUnknowNumbers() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,2,3"; + // This statement should cause an exception + calculator.handleUnknownNumbers(result); + } + @Test (expected = StringCalculatorException.class) + public void testAlphapeticAndNegative() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "A"; + // This statement should cause an exception + calculator.handleAlphabeticAndNegative(result); + } + @Test (expected = StringCalculatorException.class) + public void testAlphapeticAndNegative2() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "-2"; + // This statement should cause an exception + calculator.handleAlphabeticAndNegative(result); + System.out.println(result); + } + + @Test + public void testAddLineBreak() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1\n2,3"; + int toInt = calculator.add(result); + int sum = 6; + //Assert + assertEquals(toInt, sum); + } + @Test + public void testAddTwoNumbers() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "1,2"; + int toInt = calculator.add(result); + int sum = 3; + //Assert + assertEquals(toInt, sum); + } + @Test + public void testAddZeroAndNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "0,2"; + int toInt = calculator.add(result); + int sum = 2; + //Assert + assertEquals(toInt, sum); + } + @Test + public void testAddOneNumber() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + //Act + String result = "2"; + int toInt = calculator.add(result); + int sum = 2; + //Assert + assertEquals(toInt, sum); } - }