diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..f9fbf38 100644 Binary files a/bin/StringCalculator.class and b/bin/StringCalculator.class differ diff --git a/bin/StringCalculatorException.class b/bin/StringCalculatorException.class index 4bd9894..5276588 100644 Binary files a/bin/StringCalculatorException.class and b/bin/StringCalculatorException.class differ diff --git a/bin/StringCalculatorTest.class b/bin/StringCalculatorTest.class index d44b83f..08011ec 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..59f5912 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,113 @@ +import java.util.StringTokenizer; + public class StringCalculator { - public int add(String numbersStr) { - // Returns the sum of the numbers given in numbersStr + + public int add(String numbersStr) throws StringCalculatorException { + int result= 0; + try { + int LineCounter = countLines(numbersStr); + StringTokenizer Line = new StringTokenizer(numbersStr, "\n"); + while(LineCounter != 0) + { + String CurrentLine = Line.nextToken(); + switch(countTokensPerLine(CurrentLine)) { + case(0): + result = result + 0; + break; + case(1): + if(checkDigit(CurrentLine,1) == 1) + result = result + Integer.parseInt(CurrentLine); + else + throw new StringCalculatorException(); + break; + case(2): + if(checkDigit(CurrentLine,2) == 1) { + result = result + sumTwoNumbers(CurrentLine); + } + else + throw new StringCalculatorException(); + break; + default: + if(checkDigit(CurrentLine,countTokensPerLine(CurrentLine))==1) + result = result + sumUnknownNumbers(CurrentLine); + else + throw new StringCalculatorException(); + break; + } + + LineCounter--; + } + }catch(StringCalculatorException e) { + e.PrintError(); + } - // not yet implemented - return 0; + return result; + } + + public int countLines(String numbersStr) { + StringTokenizer st = new StringTokenizer(numbersStr, "\n"); + int i=0; + if(st.hasMoreTokens()) + { + while(st.hasMoreTokens()) { + i++; + st.nextToken(); + } + } + else + i = 1; + return i; + } + + public int countTokensPerLine(String numbersStr) { + StringTokenizer st = new StringTokenizer(numbersStr, ","); + int i=0; + while(st.hasMoreTokens()) { + i++; + st.nextToken(); + } + return i; + } + + //If the tokens are all digits then the method will return 1 otherwise 0 + public int checkDigit(String numbersStr, int TokensNum) { + int flag = 1; //Default value + try { + switch(TokensNum) { + case(1): + Integer.parseInt(numbersStr); + if(Integer.parseInt(numbersStr) < 0) + flag = 0; + break; + default: + StringTokenizer st = new StringTokenizer(numbersStr, ","); + while(st.hasMoreTokens()) { + if(Integer.parseInt(st.nextToken()) < 0) + flag = 0; + } + } + }catch(NumberFormatException e) { + flag = 0; + } + return flag; + } + + public int sumTwoNumbers(String numbersStr) { + StringTokenizer st = new StringTokenizer(numbersStr, ","); + int sum; + int value1 = Integer.parseInt(st.nextToken()); + int value2 = Integer.parseInt(st.nextToken()); + sum = value1 + value2; + return sum; + } + + public int sumUnknownNumbers(String numbersStr) { + StringTokenizer st = new StringTokenizer(numbersStr, ","); + int sum = 0; + while(st.hasMoreTokens()) { + sum = sum + Integer.parseInt(st.nextToken()); + } + return sum; } } diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..f27d719 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,8 @@ public class StringCalculatorException extends Exception { + public int PrintError() { + System.out.println("Error: Wrong format input!!!"); + return 0; + } } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..9b598fd 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -3,10 +3,186 @@ import org.junit.Test; public class StringCalculatorTest { - + + //Tests for countTokens method + @Test + public void countTokensPerLine_0_Numbers() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countTokensPerLine(""); + assertEquals(0, test); + } + + @Test + public void countTokensPerLine_1_Number() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countTokensPerLine("1"); + assertEquals(1, test); + } + + @Test + public void countTokensPerLine_2_Numbers_Space() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countTokensPerLine("1,2"); + assertEquals(2, test); + } + + + @Test + public void countTokensPerLine_4_Numbers_Space() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countTokensPerLine("1,2,3,4"); + assertEquals(4, test); + } + + //Tests for countLines + @Test + public void countLines_2_Lines() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countLines("1\n2"); + assertEquals(2, test); + } + + @Test + public void countLines_EmptyString() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countLines(""); + assertEquals(1, test); + } + + @Test + public void countLines_1_Line() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.countLines("2"); + assertEquals(1, test); + } + + //Tests for checkDigit method + @Test + public void checkDigit_1() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.checkDigit("1", 1); + assertEquals(1, test); + } + + @Test + public void checkDigit_a() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.checkDigit("a", 1); + assertEquals(0, test); + } + + @Test + public void checkDigit_1SPACE2() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.checkDigit("1,2", 2); + assertEquals(1, test); + } + + @Test - public void test() { - fail("Not yet implemented"); + public void checkDigit_1SPACEa() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.checkDigit("1,a", 2); + assertEquals(0, test); + } + + @Test + public void checkDigit_aSPACE1() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.checkDigit("a,1", 2); + assertEquals(0, test); + } + + @Test + public void checkDigit_aSPACEa() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.checkDigit("a,a", 2); + assertEquals(0, test); + } + + //Test for sumTwoNumbers method + @Test + public void sumTwoNumbers_1_2() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.sumTwoNumbers("1,2"); + assertEquals(3, test); + } + + //Test for sumUnknownNumbers method + @Test + public void sumUnknownNumbers_1_2_3() { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.sumUnknownNumbers("1,2,3"); + assertEquals(6, test); + } + + //Test for add method + @Test + public void add_1() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("1"); + assertEquals(1, test); + } + + @Test + public void add_1_1() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("1,1"); + assertEquals(2, test); + } + + @Test + public void add_2_2_2() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("2,2,2"); + assertEquals(6, test); + } + + @Test + public void add_2_2_HEAD_2_2() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("2,2\n2,2"); + assertEquals(8, test); } + @Test + public void add_2_2_4_HEAD_2_1() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("2,2,4\n2,1"); + assertEquals(11, test); + } + + @Test + public void add_2_2_4_HEAD_2_1_HEAD_1() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("2,2,4\n2,1\n1"); + assertEquals(12, test); + } + + @Test + public void add_1_HEAD_2_3() throws StringCalculatorException { + StringCalculator sc1 = new StringCalculator(); + int test = sc1.add("1\n2,3"); + assertEquals(6, test); + } + + @Test + public void add_NEGATIVE_NUMBER() throws StringCalculatorException { + try { + StringCalculator sc1 = new StringCalculator(); + sc1.add("-1,2,3"); + }catch(StringCalculatorException e) { + assert(e.PrintError() == 0); + } + } + + @Test + public void add_Failure() throws StringCalculatorException { + try { + StringCalculator sc1 = new StringCalculator(); + sc1.add("1,\n"); + }catch(StringCalculatorException e) { + assert(e.PrintError() == 0); + } + } }