Skip to content

Commit

Permalink
Complete implementation of feature #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Lindgren committed Apr 13, 2017
1 parent 938f0ea commit 3837f43
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class StringCalculator {
private static final String DEFAULT_DELIMITER = ",";
public static final int VALID_NUMBER_LOWER_BOUND = 0;
public static final int VALID_NUMBER_UPPER_BOUND = 1000;

public int add(String numbers) {
if(numbers.isEmpty()) {
Expand Down Expand Up @@ -78,7 +80,7 @@ private void reportNegativeNumber(List<Integer> errors, int num) {
}

private int calculateNewTotal(int sum, int num) {
if(num > 0) {
if(num >= VALID_NUMBER_LOWER_BOUND && num <= VALID_NUMBER_UPPER_BOUND) {
sum +=num;
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/groovy/StringCalculatorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class StringCalculatorTest extends Specification {
def setup() {
stringCalculator = new StringCalculator()
}

@Unroll
def "should calculate the sum of numbers"() {
expect:
Expand All @@ -33,4 +34,15 @@ class StringCalculatorTest extends Specification {
Exception exception = thrown(IllegalArgumentException)
exception.message == '-1,-2,-3'
}

def "should ignore numbers bigger than 1000"() {
given:
String numbers = '2,1000,1001'

when:
int sum = stringCalculator.add(numbers)

then:
sum == 1002
}
}

0 comments on commit 3837f43

Please sign in to comment.