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
Binary file modified bin/StringCalculator.class
Binary file not shown.
Binary file modified bin/StringCalculatorTest.class
Binary file not shown.
28 changes: 23 additions & 5 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}



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