Browse Source

score implementation refactoring for the incomplete frames test

master
binsky 3 years ago
parent
commit
a57cf9358d
  1. 8
      src/main/java/BowlingGameCalculator.java
  2. 3
      src/test/java/BowlingCalculatorTest.java
  3. BIN
      target/classes/BowlingGameCalculator.class
  4. BIN
      target/test-classes/BowlingCalculatorTest.class

8
src/main/java/BowlingGameCalculator.java

@ -2,9 +2,13 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class BowlingGameCalculator { public class BowlingGameCalculator {
private static final Pattern SINGLE_VALUE_PATTERN = Pattern.compile("\\d");
private static final int INITIAL_SCORE_VALUE = 0;
public int score(String playerResult) { public int score(String playerResult) {
Matcher singleDigit = Pattern.compile("\\d").matcher(playerResult);
int playerScore = 0;
Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(playerResult);
int playerScore = INITIAL_SCORE_VALUE;
while (singleDigit.find()) { while (singleDigit.find()) {
playerScore += Integer.parseInt(singleDigit.group()); playerScore += Integer.parseInt(singleDigit.group());
} }

3
src/test/java/BowlingCalculatorTest.java

@ -21,7 +21,8 @@ public class BowlingCalculatorTest {
private static Stream<Arguments> playerResults() { private static Stream<Arguments> playerResults() {
return Stream.of( return Stream.of(
Arguments.of("worst game", "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", 0), // 20x 0 Arguments.of("worst game", "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", 0), // 20x 0
Arguments.of("some incomplete frames", "0,1,2,3,0,0,4,0,0,5,0,0,6,0,0,0,0,0,0,0", 21)
Arguments.of("some incomplete frames", "0,1,2,3,0,0,4,0,0,5,0,0,6,0,0,0,0,0,0,0", 21),
Arguments.of("some other incomplete frames", "9- 9- 9- 9- 9- 9- 9- 9- 9- 9-", 90)
); );
} }
} }

BIN
target/classes/BowlingGameCalculator.class

BIN
target/test-classes/BowlingCalculatorTest.class

Loading…
Cancel
Save