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
Binary file modified bin/StringCalculator.class
Binary file not shown.
Binary file modified bin/StringCalculatorTest.class
Binary file not shown.
72 changes: 69 additions & 3 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -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;

}
}
66 changes: 66 additions & 0 deletions tests/StringCalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(""));
}

}