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.
99 changes: 92 additions & 7 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,94 @@

public class StringCalculator {
public int add(String numbersStr) {
// Returns the sum of the numbers given in numbersStr

// not yet implemented
return 0;

// public add method to be implemented and tested - main code
// Returns the sum of the numbers given in numbersStr
public int add(String numbersStr) throws StringCalculatorException {
int res;
if (numbersStr.isEmpty()) // Checking if input is empty
return 0; // Yes - return 0
else {
res = getSingleNumber(numbersStr); // Calling private method for single number
if (res != -1) // If ok? -1 is not ok
return res; // Yes - return method's result
else if (numbersStr.contains("\n")) // If new lines in input
return processNewLines(numbersStr); // then call private method to process
else { // Split input into array of numbers, separated by comma
String[] numbers = numbersStr.split(",");
if (numbers.length == 0) // If array's length = 0 - exception
throw new StringCalculatorException();
else if (numbers.length == 2) // exactly 2 numbers
return addTwoNum(numbers);
else
// And at last - private method for unknown amount of numbers in input
return addUnknownAmountNum(numbers);
}
}
} // End of add method

// ------------------------------------------------------------------------------------
// private methods to use from add method
// Note - in case we need to test these methods separately, we should redefine them as public


// Get an integer number from string input. Returns -1, if input is not number
private int getSingleNumber(String str) throws StringCalculatorException {
try {
int res = Integer.parseInt(str); // Convert string to int
if (res < 0) // Negative - exception
throw new StringCalculatorException();
else
return res;
} catch (NumberFormatException ex) { // Not number - return -1
return -1;
}
}

// Process New Lines
private int processNewLines(String numbersStr) throws StringCalculatorException {
String[] numbers = numbersStr.split("[,\n]"); // Split input into string array,
// Separator is comma + new line
if (numbers.length == 0) // Empty array - exception
throw new StringCalculatorException();
else if (numbers.length == 2)
return addTwoNum(numbers);
else
return addUnknownAmountNum(numbers);
}
}


// Returns the sum of two numbers given in string numbers array
private int addTwoNum(String[] numbers) throws StringCalculatorException {
try {
int x1, x2;
// Convert both strings to ints and check for negatives
// For negatives - StringCalculatorException
// For not numbers Integer.parseInt - NumberFormatException
if ((x1 = Integer.parseInt(numbers[0])) < 0)
throw new StringCalculatorException();
else if ((x2 = Integer.parseInt(numbers[1])) < 0)
throw new StringCalculatorException();
else
return x1 + x2; // All ok - return sum of 2 numbers
} catch (NumberFormatException ex) {
throw new StringCalculatorException();
}
}


// Returns the sum of the numbers given in string numbers array
private int addUnknownAmountNum(String[] numbers) throws StringCalculatorException {
int sum = 0; // Initialize sum
for (int i = 0; i < numbers.length; i++) // Loop through...
try {
int n = Integer.parseInt(numbers[i]); // Convert string to int
if (n < 0) // Negative - exception
throw new StringCalculatorException();
else sum += n; // add to sum
} catch (NumberFormatException ex) { // Check for not number - exception
throw new StringCalculatorException();
}
return sum;
}


} // End of class
99 changes: 95 additions & 4 deletions tests/StringCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class StringCalculatorTest {

private StringCalculator stcalc; // Define an object

@Before
public void init() {
stcalc = new StringCalculator(); // and create it
}


// Set of test cases below

@Test
public void test() {
fail("Not yet implemented");
public void test_empty_string() throws StringCalculatorException {
assertEquals(0, stcalc.add(""));
}

}

@Test
public void test_single_number() throws StringCalculatorException {
assertEquals(10, stcalc.add("10"));
}

@Test
public void test_two_numbers() throws StringCalculatorException {
assertEquals(10, stcalc.add("6,4"));
assertEquals(3, stcalc.add("1,2"));
assertEquals(2, stcalc.add("0,2"));
}

@Test
public void test_any_numbers() throws StringCalculatorException {
assertEquals(8, stcalc.add("1,3,4"));
assertEquals(0, stcalc.add("0,0,0"));
assertEquals(10, stcalc.add("1,2,3,4"));
}

@Test(expected = StringCalculatorException.class)
public void test_single_not_number() throws StringCalculatorException {
stcalc.add("abc");

}

@Test(expected = StringCalculatorException.class)
public void test_single_not_number2() throws StringCalculatorException {
stcalc.add(",");
}

@Test(expected = StringCalculatorException.class)
public void test_two_not_number() throws StringCalculatorException {
stcalc.add("abc,def");
}

@Test(expected = StringCalculatorException.class)
public void test_two_not_number2() throws StringCalculatorException {
stcalc.add("10,aaa");
}

@Test(expected = StringCalculatorException.class)
public void test_single_negative_number() throws StringCalculatorException {
stcalc.add("-1");
}

@Test(expected = StringCalculatorException.class)
public void test_two_negative_numbers() throws StringCalculatorException {
stcalc.add("-1,-2");
}

@Test(expected = StringCalculatorException.class)
public void test_two_negative_numbers2() throws StringCalculatorException {
stcalc.add("-2,4");
}

@Test(expected = StringCalculatorException.class)
public void test_two_negative_numbers3() throws StringCalculatorException {
stcalc.add("5,-6");
}

@Test(expected = StringCalculatorException.class)
public void test_more_than_two_negative_numbers() throws StringCalculatorException {
stcalc.add("-5,-8,2,-1");
}

@Test
public void test_two_numbers_line_break() throws StringCalculatorException {
assertEquals(10, stcalc.add("4\n6"));
assertEquals(5, stcalc.add("0\n5"));
}

@Test
public void more_than_two_numbers_line_break() throws StringCalculatorException {
assertEquals(6, stcalc.add("1\n2,3"));
assertEquals(6, stcalc.add("1,2\n3"));
assertEquals(6, stcalc.add("1\n2\n3"));
}

// More tests can go here

} // End of test class