Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
69 changes: 63 additions & 6 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
2 changes: 1 addition & 1 deletion src/StringCalculatorException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

public class StringCalculatorException extends Exception {
class StringCalculatorException extends Exception {

}
93 changes: 91 additions & 2 deletions tests/StringCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}