Browse Source

production code refactored

how_to_use_Mockito
Thomas Papendieck 6 years ago
parent
commit
54c7055a74
  1. 25
      TDD-pure-function/src/main/java/de/edu/hsfulda/ciip/tdd/BownlingCalculator.java

25
TDD-pure-function/src/main/java/de/edu/hsfulda/ciip/tdd/BownlingCalculator.java

@ -4,6 +4,10 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class BownlingCalculator { public class BownlingCalculator {
private static final int SINGLE_DIGIT = 0;
private static final int STRIKE_SECOND_NEXT_THROW = 2;
private static final int STRIKE_NEXT_THROW = 1;
private static final String STRIKE = "X (\\d)(\\d)";
private static final int PINS_IN_FRAME = 10; private static final int PINS_IN_FRAME = 10;
private static final String SPARE = "(\\d)/ (\\d)"; private static final String SPARE = "(\\d)/ (\\d)";
private static final String INCOMPLETE_FRAME = "\\d"; private static final String INCOMPLETE_FRAME = "\\d";
@ -18,16 +22,23 @@ public class BownlingCalculator {
public int evaluate(String listOfThrows) { public int evaluate(String listOfThrows) {
int sum = 0; int sum = 0;
sum += evaluateFrames(listOfThrows, INCOMPLETE_FRAME, m -> Integer.parseInt(m.group()));
sum += evaluateFrames(listOfThrows, SPARE, m -> PINS_IN_FRAME//
- Integer.parseInt(m.group(SPARE_FRAME_FIRST_THROW))
+ Integer.parseInt(m.group(SPARE_FRAME_FOLLOWING_THROW)));
sum += evaluateFrames(listOfThrows, "X (\\d)(\\d)", m -> PINS_IN_FRAME//
+ Integer.parseInt(m.group(1))
+ Integer.parseInt(m.group(2)));
sum += evaluateFrames(listOfThrows, INCOMPLETE_FRAME,
singleDigit -> convertMatchToInt(singleDigit, SINGLE_DIGIT));
sum += evaluateFrames(listOfThrows, SPARE, // trailing line comments for formatting
spare -> PINS_IN_FRAME //
- convertMatchToInt(spare, SPARE_FRAME_FIRST_THROW)
+ convertMatchToInt(spare, SPARE_FRAME_FOLLOWING_THROW));
sum += evaluateFrames(listOfThrows, STRIKE, //
strike -> PINS_IN_FRAME //
+ convertMatchToInt(strike, STRIKE_NEXT_THROW) //
+ convertMatchToInt(strike, STRIKE_SECOND_NEXT_THROW));
return sum; return sum;
} }
private int convertMatchToInt(Matcher strike, int groupIndex) {
return Integer.parseInt(strike.group(groupIndex));
}
private int evaluateFrames(String listOfThrows, String frameStructurePattern, FrameCalculator frameCalculator) { private int evaluateFrames(String listOfThrows, String frameStructurePattern, FrameCalculator frameCalculator) {
Matcher singleDigit = Pattern.compile(frameStructurePattern).matcher(listOfThrows); Matcher singleDigit = Pattern.compile(frameStructurePattern).matcher(listOfThrows);
int frameValue = 0; int frameValue = 0;

Loading…
Cancel
Save