diff --git a/.besouro/20160916160212601/actions.txt b/.besouro/20160916160212601/actions.txt new file mode 100644 index 0000000..1644b5b --- /dev/null +++ b/.besouro/20160916160212601/actions.txt @@ -0,0 +1,3 @@ +FileOpenedAction 1474030932817 StringCalculator.java 2409 4 20 0 +UnitTestCaseAction 1474030944931 StringCalculatorTest.java OK +UnitTestSessionAction 1474030944933 StringCalculatorTest.java OK diff --git a/.besouro/20160916160212601/besouroEpisodes.txt b/.besouro/20160916160212601/besouroEpisodes.txt new file mode 100644 index 0000000..8f1db8a --- /dev/null +++ b/.besouro/20160916160212601/besouroEpisodes.txt @@ -0,0 +1 @@ +1474030944933 regression 1 12 true diff --git a/.besouro/20160916160212601/disagreements.txt b/.besouro/20160916160212601/disagreements.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20160916160212601/randomHeuristicEpisodes.txt b/.besouro/20160916160212601/randomHeuristicEpisodes.txt new file mode 100644 index 0000000..8f1db8a --- /dev/null +++ b/.besouro/20160916160212601/randomHeuristicEpisodes.txt @@ -0,0 +1 @@ +1474030944933 regression 1 12 true diff --git a/.besouro/20160916160212601/userComments.txt b/.besouro/20160916160212601/userComments.txt new file mode 100644 index 0000000..e69de29 diff --git a/.besouro/20160916160212601/zorroEpisodes.txt b/.besouro/20160916160212601/zorroEpisodes.txt new file mode 100644 index 0000000..393e16e --- /dev/null +++ b/.besouro/20160916160212601/zorroEpisodes.txt @@ -0,0 +1 @@ +1474030944933 regression 1 12 false diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..249513b --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,3 @@ +/StringCalculator.class +/StringCalculatorException.class +/StringCalculatorTest.class diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..69bee41 100644 Binary files a/bin/StringCalculator.class and b/bin/StringCalculator.class differ diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f..1c2d878 100644 Binary files a/bin/StringCalculatorTest.class and b/bin/StringCalculatorTest.class differ diff --git a/src/StringCalculator.java b/src/StringCalculator.java index 487916b..9b29c8d 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -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 numbers = new ArrayList(); + + // 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; } + } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..e40c53d 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -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. + + + + }