diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..aae354f 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..b48e05b 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..691b08f 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..549d2a4 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,153 @@ - public class StringCalculator { + private StringCalculatorException stringCalculatorException; + public int add(String numbersStr) { // Returns the sum of the numbers given in numbersStr + boolean correctStr = true; + int sum = 0, sum1 = 0, sum2 = 0; + int sum11 = 0, sum12 = 0, sum21 = 0, sum22=0; - // not yet implemented - return 0; + String printStr = numbersStr.replaceAll("\n","\\\\n"); + System.out.println("The input string is: \"" + printStr + "\""); + + if (numbersStr =="") + sum = 0; + else + { + int count = numbersStr.length() - numbersStr.replaceAll(",","").length(); + //check how many "," in the numberStr + switch (count) { + case 0: //one number + try { + sum = Integer.parseInt(numbersStr); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): " + numbersStr); + } + if (sum < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers" + numbersStr); + } + break; + case 1: //two or more numbers + String[] parts = numbersStr.split(","); + String firstnum = parts[0]; + String secondnum = parts[1]; + if (firstnum.equals( "\n") || secondnum.equals( "\n")) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input: \\n"); + } else + { + int count1 = firstnum.length() - firstnum.replaceAll("\n","").length(); + //check how many "\n" in the numberStr + switch (count1) { + case 0: //one number + try { + sum1 = Integer.parseInt(firstnum); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): " + firstnum); + } + if (sum1 < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers: "+ firstnum); + } + break; + case 1: + String[] twoparts = firstnum.split("\n"); + String firstpart = twoparts[0]; + String secondpart = twoparts[1]; + try { + sum11 = Integer.parseInt(firstpart); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): " + firstpart); + } + if (sum11 < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers: "+ firstpart); + } + try { + sum12 = Integer.parseInt(secondpart); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): " + secondpart); + } + if (sum12 < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers:" + secondpart); + } + sum1 = sum11 + sum12; + break; + default: + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input: too many \\n"); + break; + } + int count2 = secondnum.length() - secondnum.replaceAll("\n","").length(); + //check how many "\n" in the numberStr + switch (count2) { + case 0: //one number + try { + sum2 = Integer.parseInt(secondnum); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): "+ secondnum); + } + if (sum2 < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers: " + secondnum); + } + break; + case 1: + String[] twoparts = secondnum.split("\n"); + String firstpart = twoparts[0]; + String secondpart = twoparts[1]; + try { + sum21 = Integer.parseInt(firstpart); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): " + firstpart); + } + if (sum21 < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers: " + firstpart); + } + try { + sum22 = Integer.parseInt(secondpart); + } catch (NumberFormatException e) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input (not numbers): " + secondpart); + } + if (sum22 < 0) { + correctStr = false; + stringCalculatorException = new StringCalculatorException("negative numbers: "+ secondpart); + } + sum2 = sum21 + sum22; + break; + default: + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input: too many \\n"); + break; + } + sum = sum1 + sum2; + } + break; + default: + correctStr = false; + stringCalculatorException = new StringCalculatorException("invalid input: too many ,"); + break; + + } + } + + if (correctStr) { + System.out.println("The sum of the numbers is: " + String.valueOf(sum)); + System.out.println(); + } + + return 0; } + + } diff --git a/src/StringCalculatorException.java b/src/StringCalculatorException.java index da71147..ed567a0 100644 --- a/src/StringCalculatorException.java +++ b/src/StringCalculatorException.java @@ -1,4 +1,8 @@ public class StringCalculatorException extends Exception { -} + public StringCalculatorException(String e) { + System.out.println(e); + } + +} \ No newline at end of file diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..5e42da0 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -6,7 +6,43 @@ public class StringCalculatorTest { @Test public void test() { - fail("Not yet implemented"); + StringCalculator cal = new StringCalculator(); + cal.add(""); + cal.add("2"); + cal.add("-2"); + cal.add("a"); + cal.add("1,2,3"); + cal.add("1,\n"); + cal.add("\n,1"); + cal.add("-1,a"); + cal.add("a,-1"); + + cal.add("1\n2,3"); + cal.add("1\n2,3\n4"); + cal.add("1,3\n4"); + cal.add("1,2"); + + cal.add("a,2"); + cal.add("-1,2"); + cal.add("1,a"); + cal.add("1,-2"); + + cal.add("a\n2,3"); + cal.add("-1\n2,3"); + cal.add("1\na,3"); + cal.add("1\n-2,3"); + cal.add("1,a\n4"); + cal.add("1,-3\n4"); + cal.add("1,3\na"); + cal.add("1,3\n-4"); + + cal.add("1\n2,a"); + cal.add("1\n2,-2"); + cal.add("a,3\n4"); + cal.add("-1,3\n4"); + cal.add("a\nb,c\nd"); + cal.add("-1\n-2,-3\n-4"); + cal.add("-1\n\n,-3\n-4"); } }