From 54c7055a74e1224139fad1ca1a8c2090c017ae77 Mon Sep 17 00:00:00 2001 From: Thomas Papendieck Date: Wed, 28 Nov 2018 18:32:50 +0100 Subject: [PATCH] production code refactored --- .../hsfulda/ciip/tdd/BownlingCalculator.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/TDD-pure-function/src/main/java/de/edu/hsfulda/ciip/tdd/BownlingCalculator.java b/TDD-pure-function/src/main/java/de/edu/hsfulda/ciip/tdd/BownlingCalculator.java index 39f132a..197724e 100644 --- a/TDD-pure-function/src/main/java/de/edu/hsfulda/ciip/tdd/BownlingCalculator.java +++ b/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; 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 String SPARE = "(\\d)/ (\\d)"; private static final String INCOMPLETE_FRAME = "\\d"; @@ -18,16 +22,23 @@ public class BownlingCalculator { public int evaluate(String listOfThrows) { 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; } + private int convertMatchToInt(Matcher strike, int groupIndex) { + return Integer.parseInt(strike.group(groupIndex)); + } + private int evaluateFrames(String listOfThrows, String frameStructurePattern, FrameCalculator frameCalculator) { Matcher singleDigit = Pattern.compile(frameStructurePattern).matcher(listOfThrows); int frameValue = 0;