diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c..c921184 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..1f94b50 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..2e77640 100644 --- a/src/StringCalculator.java +++ b/src/StringCalculator.java @@ -1,9 +1,75 @@ public class StringCalculator { - public int add(String numbersStr) { + public int add(String numbersStr) throws StringCalculatorException { // Returns the sum of the numbers given in numbersStr - + int sum = 0; + if (numbersStr.isEmpty()) { + return 0; + } else { + int numericArray[] = getIntArray(numbersStr); + for (int i = 0; i < numericArray.length; i++) { + sum = sum + numericArray[i]; + } + return sum; + } // not yet implemented - return 0; + + } + + public int getLength(String str) { + return str.length(); + + } + + public boolean handleLength(String str) { + if (str.length() < 20) { + return true; + } else { + return false; + } + + } + + public int[] getIntArray(String str) { + int arr[] = new int[str.length()]; + int index = 0; + // System.out.println(str.length() + " "); + // if (isValid(str)) { + for (int i = 0; i < str.length(); i++) { + // if ((str.charAt(i)+str.charAt(i+1)+"") != "\n" || + // (str.charAt(i) + // + "") != ",") { + if (!(str.charAt(i) + "").equals(",")) { + if (!(str.charAt(i) + "").equals("\n")) { + // System.out.println(str.charAt(i) + ""); + arr[index++] = Integer.parseInt(str.charAt(i) + ""); + + } + // System.out.println(str.length() + " " + str.charAt(i) + + // ""); + } + } + // } + return arr; + + } + + public boolean isValid(String str) { + + return str.matches("(.*),\n(.*)") || str.matches("(.*)-(.*)") || str.matches("(.*)[a-z](.*)"); + // return str.matches(",\n") || str.matches("-"); + // boolean isvalid = true; + // System.out.println(str.length() + ""); + /* + * if (str.length() >= 2) { for (int i = 0; i < str.length() - 1; i++) { + * if (((char)str.charAt(i) + (char)str.charAt(i + 1) + + * "").equals(",\n")) { //System.out.println(str.charAt(i) + + * str.charAt(i + 1) + ""); isvalid = false; break; } + * + * } } + */ + + // return isvalid; + } } diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..48c29de 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -9,4 +9,70 @@ public void test() { fail("Not yet implemented"); } + @Test + public void simpleAddition() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("output is correct", 12, st1.add("1236")); + } + + @Test + public void additionWithComma() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("output is correct", 12, st1.add("1,2,36")); + } + + @Test + public void additionWithNewLine() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("output is correct", 12, st1.add("1,2\n36")); + } + + @Test + public void checkInValidAgainstnewlineWithcomma() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean res = st1.isValid("45,\n67"); + assertTrue("String is Invalid", res); + } + + @Test + public void checkInValidAgainstNegativeNumber() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean res = st1.isValid("45-67"); + assertTrue("String is Invalid", res); + } + + @Test + public void checkInValidAgainstAnyInvalidInput() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean res = st1.isValid("457bcb"); + assertTrue("String is Invalid", res); + } + + @Test + public void isLenghtCorrect() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("Length is correct", 5, st1.getLength("56556")); + } + @Test + public void testLenghtLimit() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + boolean range=st1.handleLength("0463563465456760000"); + assertTrue("Length is within range", range); + } + + @Test + public void testEmptyResult() throws StringCalculatorException { + + StringCalculator st1 = new StringCalculator(); + assertEquals("Empty output", 0, st1.add("")); + } + }