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.
51 changes: 48 additions & 3 deletions src/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@

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;
int[] numberArray = new int[3];

// not yet implemented
return 0;
if(!numbersStr.isEmpty()) {

numberArray = splitInput(numbersStr);
for(int i = 0; i < numberArray.length; i++) {
if(numberArray[i] >= 0) {
testOfValue(numberArray[i]);
sum += numberArray[i];
} else throw new StringCalculatorException();
}
}


return sum;
}

private int[] splitInput(String numbersToSplit) {
String [] numbers;
int[] sum = new int[3];
numbers = numbersToSplit.split("[,|\n]");
if(testOfLength(numbers)) {
for(int i = 0; i < numbers.length; i++) {
sum[i] = Integer.parseInt(numbers[i]);
}
}

return sum;
}

private boolean testOfLength(String[] testString) {
boolean test;
if(testString.length >= 0 && testString.length < 3) {
test = true;
} else test = false;
return test;
}

private boolean testOfValue(int value) throws StringCalculatorException {
boolean test;
if(value < 0) {
test = true;
} else throw new StringCalculatorException();
return test;
}

}
2 changes: 1 addition & 1 deletion src/StringCalculatorException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

public class StringCalculatorException extends Exception {

// I expect that this doesn't need to edited as there was only talk of throwing it
}