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
3 changes: 3 additions & 0 deletions .besouro/20160916160212601/actions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FileOpenedAction 1474030932817 StringCalculator.java 2409 4 20 0
UnitTestCaseAction 1474030944931 StringCalculatorTest.java OK
UnitTestSessionAction 1474030944933 StringCalculatorTest.java OK
1 change: 1 addition & 0 deletions .besouro/20160916160212601/besouroEpisodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1474030944933 regression 1 12 true
Empty file.
1 change: 1 addition & 0 deletions .besouro/20160916160212601/randomHeuristicEpisodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1474030944933 regression 1 12 true
Empty file.
1 change: 1 addition & 0 deletions .besouro/20160916160212601/zorroEpisodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1474030944933 regression 1 12 false
3 changes: 3 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/StringCalculator.class
/StringCalculatorException.class
/StringCalculatorTest.class
Binary file modified bin/StringCalculator.class
Binary file not shown.
Binary file modified bin/StringCalculatorTest.class
Binary file not shown.
109 changes: 105 additions & 4 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,110 @@
import java.util.*;

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

// only add two numbers with comma
public int add(String numbersStr) throws StringCalculatorException {

// not yet implemented
return 0;
int sum = 0;

// also throws exception when using negative numbers, because char '-' is not a number.
if(numbersStr == null || numbersStr == "" || !isNumber(numbersStr, '\n', ',')) {
//return 0;
throw new StringCalculatorException();
}

// create an array of our string, splitting it with character ','.
String[] numbers = numbersStr.split(",");

// returns zero if there are more than two numbers.
if(numbers.length > 3) {
throw new StringCalculatorException();
}

for(String s : numbers) {
sum += Integer.parseInt(s);
}

return sum;

}

// add unlimited amount with comma
public int addUnlimited(String numbersStr) {
int sum = 0;

if(numbersStr == null || numbersStr == "" || !isNumber(numbersStr, '\n', ',')) {
return 0;
}

// create an array of our string, splitting it with character ','.
String[] numbers = numbersStr.split(",");

for(String s : numbers) {
sum += Integer.parseInt(s);
}

return sum;
}

// add unlimited + cut with newline and comma.
public int addUsingNewline(String numbersStr) {

int sum = 0;

if(numbersStr == null || numbersStr == "" || !isNumber(numbersStr, '\n', ',')) {
return 0;
}

// From this point forwards our string contains only numbers, newlines and commas.

// create a new list, which contains our integers as characters.
List<Character> numbers = new ArrayList<Character>();

// helper boolean
boolean cutOnce = false;

// cut our string using newline or comma.
for(char c : numbersStr.toCharArray()) {
if(c == '\n' || c == ',') {

// if we have cut once already.
// -> return zero, because it's not allowed to have two
// cuts in a row.
if(cutOnce) {
return 0;
}

cutOnce = true;
continue;
}

cutOnce = false;
numbers.add(c);
}

for(Character c : numbers) {
sum += Integer.parseInt(c.toString());
}

return sum;
}

private boolean isNumber(String s, char cut1, char cut2) {

for(char c : s.toCharArray()) {

// change this to allow different char to split?
if(c == cut1 || c == cut2) {
continue;
}

if(!Character.isDigit(c)) {
return false;
}
}

return true;
}

}
91 changes: 88 additions & 3 deletions tests/StringCalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,94 @@

public class StringCalculatorTest {

@Test
public void test() {
fail("Not yet implemented");
// TEST ADD BEGIN

@Test (expected = StringCalculatorException.class)
public void test_return_zero_null_string() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
//assertEquals("zero", 0, strcalc.add(null));
int a = strcalc.add(null);
}

@Test (expected = StringCalculatorException.class)
public void test_return_zero_empty_string() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
//assertEquals("zero", 0, strcalc.add(""));
int a = strcalc.add("");
}

@Test (expected = StringCalculatorException.class)
public void test_return_zero_invalid_string() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
//assertEquals("zero", 0, strcalc.add("as2"));
int a = strcalc.add("as2");
}

@Test
public void test_return_two_string_1_1() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return two.", 2, strcalc.add("1,1"));
}

@Test
public void test_return_five_string_2_3() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return five.", 5, strcalc.add("2,3"));
}

@Test
public void test_return_three_string_3() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return three.", 3, strcalc.add("3"));
}

@Test(expected = StringCalculatorException.class)
public void test_return_exception_string_negative() throws StringCalculatorException {
StringCalculator strcalc = new StringCalculator();
int a = strcalc.add("-3");
}

// TEST ADD END.

// TEST ADDUNLIMITED BEGIN

@Test
public void test_unlimited_return_5_string_1_1_1_1_1() {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return five.", 5, strcalc.addUnlimited("1,1,1,1,1"));
}

// TEST ADDUNLIMITED END.

// TEST ADDNEWCHAR BEGIN

@Test
public void test_newline_return_5_string_1_1_1_1_1() {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return five.", 5, strcalc.addUsingNewline("1\n1\n1\n1\n1"));
}

@Test
public void test_newline_return_6_string_1_newline_2_3() {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return six.", 6, strcalc.addUsingNewline("1\n2,3"));
}

@Test
public void test_newline_return_0_string_1_newline_comma_3() {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return zero.", 0, strcalc.addUsingNewline("1\n,"));
}

@Test
public void test_newline_return_10_string_1_newline_2_newline_3_comma_3_comma_1() {
StringCalculator strcalc = new StringCalculator();
assertEquals("Should return ten.", 10, strcalc.addUsingNewline("1\n2\n3,3,1"));
}

// TEST ADDNEWCHAR END.




}