diff --git a/.gitignore b/.gitignore index cd2946a..3aa82af 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,25 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +## IDE Specific +.idea +.settings +.project +.classpath + +## Excludes the generated files +bin/ \ No newline at end of file diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..ab48ed2 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,66 @@ +import java.util.Arrays; +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; +class StringCalculator { + + /** + * add(String numbersStr) + * + * Adds the given numbers together. + * + * @param numbersStr - The numbers in a string + * @return The sum of the numbers + */ + int add(String numbersStr) throws StringCalculatorException { + + final int[] sum = {0}; + + // Check that the string is not null or empty + if (isStringNotNullAndNotEmpty(numbersStr)) { + String preparedStr = prepareStringForCalculation(numbersStr); // Prepare the string for calculation + + List numbersList = Arrays.asList(preparedStr.split(",")); // Assign the expected numbers to an array + numbersList.forEach((currentItem) -> { + if(!currentItem.isEmpty()) { + sum[0] = sum[0] + Integer.parseInt(currentItem); + } + }); + } + + return sum[0]; // Return the sum of the items } + + /** + * private boolean isStringNotNullAndNotEmpty + * Checks if the given String is null or empty. The null check is performed first to ensure no NPE is thrown. + * + * @param stringToBeChecked - The String that needs to be checked + * @return true/false depending on if the String was null/empty or not + */ + private boolean isStringNotNullAndNotEmpty(String stringToBeChecked) { + return (stringToBeChecked != null && !stringToBeChecked.isEmpty()); + } + + /** + * private String prepareStringForCalculation + * Prepares the user input for calculation. + * + * @param originalStr - The original String that needs to be prepared + * @return preparedStr - The prepared String + * @throws StringCalculatorException - Throw if the regex doesn't match + */ + private String prepareStringForCalculation(String originalStr) throws StringCalculatorException{ + String regex = "[0-9, /,]+"; //The regex that the check will be performed against + + // Replace whitespace with comma + String preparedStr = originalStr.replaceAll("\\s", ","); + + // Abort execution if string contains characters other than numbers [0-9] or comma [/,]. + if (!preparedStr.matches(regex)) { + throw new StringCalculatorException(); + } + + return preparedStr; + } + } diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..cf231ec 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,4 @@ -public class StringCalculatorException extends Exception { +class StringCalculatorException extends Exception { } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..87fa276 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,101 @@ import static org.junit.Assert.*; +import org.junit.Before; import org.junit.Test; public class StringCalculatorTest { + private StringCalculator stringCalculator; + + @Before + public void initializeStringCalculator() { + stringCalculator = new StringCalculator(); + } + @Test - public void test() { - fail("Not yet implemented"); + public void testAdd_2NumbersString() throws StringCalculatorException { + //Arrange + String twoNumberString = "42,42"; + //Act + int returnedNumber = stringCalculator.add(twoNumberString); + //Assert + assertEquals("Returned number should be equal to the sum of the numbers in the string", 84, returnedNumber); + } + + @Test + public void testAdd_2NumbersStringWithWhiteSpace() throws StringCalculatorException { + //Arrange + String twoNumberString = "42, 42"; + //Act + int returnedNumber = stringCalculator.add(twoNumberString); + //Assert + assertEquals("Returned number should be equal to the sum of the numbers in the string", 84, returnedNumber); + } + + @Test + public void testAdd_1NumberString() throws StringCalculatorException { + //Arrange + String singleNumberString = "42"; + //Act + int returnedNumber = stringCalculator.add(singleNumberString); + //Assert + assertEquals("Returned number should be equal to given number.", Integer.parseInt(singleNumberString), returnedNumber); + } + + @Test + public void testAdd_NullString() throws StringCalculatorException { + //Arrange + String nullString = null; + //Act + int returnedNumber = stringCalculator.add(nullString); + //Assert + assertEquals("Returned number should be 0", 0, returnedNumber); + } + + @Test + public void testAdd_EmptyString() throws StringCalculatorException { + //Arrange + String emptyString = ""; + //Act + int returnedNumber = stringCalculator.add(emptyString); + //Assert + assertEquals("Returned number should be 0", 0, returnedNumber); + } + + @Test + public void testAdd_3NumbersStringWithWhitespace() throws StringCalculatorException { + //Arrange + String threeNumberString = "42,42, 42"; + //Act + int returnedNumber = stringCalculator.add(threeNumberString); + //Assert + assertEquals("Returned number should be equal to the sum of the numbers in the string", 126, returnedNumber); + } + + @Test + public void testAdd_stringWithNewLines() throws StringCalculatorException { + //Arrange + String threeNumberString = "42,42\n42"; + //Act + int returnedNumber = stringCalculator.add(threeNumberString); + //Assert + assertEquals("Returned number should be equal to the sum of the numbers in the string", 126, returnedNumber); + } + + @Test (expected = StringCalculatorException.class) + public void testAdd_NegativeNumber() throws StringCalculatorException { + //Arrange + String negativeNumberString = "-1"; + //Act + int returnedNumber = stringCalculator.add(negativeNumberString); + } + + @Test (expected = StringCalculatorException.class) + public void testAdd_InvalidInput() throws StringCalculatorException { + //Arrange + String invalidNumberString = "Hello there!"; + //Act + int returnedNumber = stringCalculator.add(invalidNumberString); } }