diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..7dc706f 100644 Binary files a/bin/StringCalculator.class and b/bin/StringCalculator.class differ diff --git a/bin/StringCalculatorException.class b/bin/StringCalculatorException.class index 4bd9894..83a4b8e 100644 Binary files a/bin/StringCalculatorException.class and b/bin/StringCalculatorException.class differ diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f..2df168e 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..b69f21a 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,112 @@ +//import java.text.ParseException; public class StringCalculator { - public int add(String numbersStr) { + + + + public int add(String numbersStr) throws StringCalculatorException { + // Returns the sum of the numbers given in numbersStr - // not yet implemented - return 0; + numbersStr=checkIfEmpty(numbersStr); + numbersStr=LinesToCommas(numbersStr); //replaces "\n" with "," + checkInvalidCommas(numbersStr); //Checks if there is more than 1 comma in a row + + String [] StrArray=removeCommas(numbersStr); //removes commas from the string + + checkForNumeric(StrArray);//checks if is numeric, if not, throws exception + int[] intArray=ChangeToInt(StrArray);//Strings to integer + checkForNegatives(intArray);//Checks that numbers are positive, if not, throws exception + checkNumberCount(intArray);//Checks how many numbers given, if more than 2, throws exception + + int sum=0; + for (int i : intArray) { + sum=sum+i; + } + System.out.print("Sum is: "); + System.out.print(sum); + + return sum; + } + + public String checkIfEmpty(String numbersStr) { + if (numbersStr=="") { + numbersStr="0"; + } + return numbersStr; + } + + public void checkNumberCount(int[] intArray) throws StringCalculatorException { + + if (intArray.length>2) { + throw new StringCalculatorException(""); + + } + } + + public int[] ChangeToInt(String[] strArray) { + String[] arrOfStr =strArray; + + int[] arrOfInt = new int[arrOfStr.length]; + + for (int i = 0; i < arrOfStr.length; i++) { + + arrOfInt[i] = Integer.parseInt(arrOfStr[i]); + } + + return arrOfInt; + } + + public void checkForNegatives(int[] intArray) throws StringCalculatorException { + int[] array = intArray; + for (int i=0; i=0) { + throw new StringCalculatorException(""); + } + } + + + } diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..7d840a9 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,8 @@ public class StringCalculatorException extends Exception { + public StringCalculatorException(String string) { + System.out.println(string); + } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..0899865 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,87 @@ import static org.junit.Assert.*; +import java.util.Arrays; + import org.junit.Test; +import junit.framework.Assert; + public class StringCalculatorTest { + + @Test - public void test() { - fail("Not yet implemented"); + public void TestStringCalculator_ChangeLinesToCommas() throws StringCalculatorException{ + //Arrange + StringCalculator calculator = new StringCalculator(); + //act + assertEquals("Fail","1,2,66",calculator.LinesToCommas("1\n2,66"));// + } + + @Test(expected = StringCalculatorException.class) + public void TestStringCalculator_CommasInaRowShouldThrowException() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + calculator.checkInvalidCommas("1,,2,3"); } + + @Test + public void TestStringCalculator_RemoveCommas() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + + String input=("1,2,3"); + String[] expected= {"1","2","3"}; + assertArrayEquals(expected,calculator.removeCommas(input)); + } + + + @Test(expected=StringCalculatorException.class) + public void TestStringCalculator_NotNumericThrowException() throws StringCalculatorException{ + //Arrange + StringCalculator calculator = new StringCalculator(); + //act + String[] input= {"a","2"}; + calculator.checkForNumeric(input); + } + + @Test + public void TestStringCalculator_ChangeToIntegerArray() throws StringCalculatorException { + //Arrange + StringCalculator calculator = new StringCalculator(); + + String[] input= {"1","2","3"}; + int[] expected= {1,2,3}; + + assertArrayEquals(expected,calculator.ChangeToInt(input)); + } + + @Test(expected = StringCalculatorException.class) + public void TestStringCalculator_NegativeThrowsException() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + int[] input= {-2,3,5}; + calculator.checkForNegatives(input); + } + + @Test (expected=StringCalculatorException.class) + public void TestStringCalculator_TooManyNumbersThrowException() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + int[] input= {1,55,5}; + calculator.checkNumberCount(input); + } + + + @Test + public void TestStringCalculator_NoNumbersReturnsZero() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + //act + assertEquals("Fail", 0, calculator.add("")); + } + @Test + public void TestStringCalculator_AddsGivenNumbers() throws StringCalculatorException { + StringCalculator calculator = new StringCalculator(); + //act + assertEquals("Fail", 20, calculator.add("14,6")); + } } +