-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1 added and implemented fibonacci question factory and did some re…
…factoring
- Loading branch information
1 parent
00f0de7
commit a190a09
Showing
10 changed files
with
116 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 13 additions & 8 deletions
21
...a/no/lundesgaard/startup/extreme/question/service/factory/BinaryMathsQuestionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
package no.lundesgaard.startup.extreme.question.service.factory; | ||
|
||
import static java.lang.String.format; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
import no.lundesgaard.startup.extreme.question.model.QuestionType; | ||
import no.lundesgaard.startup.extreme.question.service.Randomizer; | ||
|
||
public abstract class BinaryMathsQuestionFactory extends QuestionFactory { | ||
private final String textFormat; | ||
private final BiFunction<Integer, Integer, String> textFunction; | ||
private final BiFunction<Integer, Integer, String> answerFunction; | ||
|
||
public BinaryMathsQuestionFactory(Randomizer randomizer, QuestionType questionType, int points, String textFormat, BiFunction<Integer, Integer, String> answerFunction) { | ||
public BinaryMathsQuestionFactory(Randomizer randomizer, | ||
QuestionType questionType, | ||
int points, | ||
BiFunction<Integer, Integer, String> textFunction, | ||
BiFunction<Integer, Integer, String> answerFunction) { | ||
super(randomizer, questionType, points); | ||
this.textFormat = textFormat; | ||
this.textFunction = textFunction; | ||
this.answerFunction = answerFunction; | ||
} | ||
|
||
public BinaryMathsQuestionFactory(Randomizer randomizer, QuestionType questionType, String textFormat, BiFunction<Integer, Integer, String> answerFunction) { | ||
public BinaryMathsQuestionFactory(Randomizer randomizer, | ||
QuestionType questionType, | ||
BiFunction<Integer, Integer, String> textFunction, | ||
BiFunction<Integer, Integer, String> answerFunction) { | ||
super(randomizer, questionType); | ||
this.textFormat = textFormat; | ||
this.textFunction = textFunction; | ||
this.answerFunction = answerFunction; | ||
} | ||
|
||
@Override | ||
protected TextAndAnswer textAndAnswer() { | ||
int n1 = randomizer.nextInt(20); | ||
int n2 = randomizer.nextInt(20); | ||
return new TextAndAnswer(format(textFormat, n1, n2), answerFunction.apply(n1, n2)); | ||
return new TextAndAnswer(textFunction.apply(n1, n2), answerFunction.apply(n1, n2)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ava/no/lundesgaard/startup/extreme/question/service/factory/FibonacciQuestionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package no.lundesgaard.startup.extreme.question.service.factory; | ||
|
||
import static java.lang.String.format; | ||
import static no.lundesgaard.startup.extreme.question.model.QuestionType.FIBONACCI; | ||
|
||
import no.lundesgaard.startup.extreme.question.service.Randomizer; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class FibonacciQuestionFactory extends BinaryMathsQuestionFactory { | ||
public FibonacciQuestionFactory(Randomizer randomizer) { | ||
super(randomizer, FIBONACCI, 50, FibonacciQuestionFactory::text, FibonacciQuestionFactory::answer); | ||
} | ||
|
||
private static String text(int n1, int n2) { | ||
return format("what is the %s number in the Fibonacci sequence", ordinalize(n1 + 4)); | ||
} | ||
|
||
private static String ordinalize(int number) { | ||
return format("%d%s", number, ordinal(number)); | ||
} | ||
|
||
private static String ordinal(int n) { | ||
return switch (n) { | ||
case 11, 12, 13 -> "th"; | ||
default -> switch (n % 10) { | ||
case 1 -> "st"; | ||
case 2 -> "nd"; | ||
case 3 -> "rd"; | ||
default -> "th"; | ||
}; | ||
}; | ||
} | ||
|
||
private static String answer(int n1, int n2) { | ||
return String.valueOf(fibonacci(n1 + 4, 0, 1)); | ||
} | ||
|
||
private static int fibonacci(int number, int a, int b) { | ||
if (number > 0) | ||
return fibonacci(number - 1, b, a + b); | ||
return a; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...no/lundesgaard/startup/extreme/question/service/factory/FibonacciQuestionFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package no.lundesgaard.startup.extreme.question.service.factory; | ||
|
||
import static no.lundesgaard.startup.extreme.question.model.QuestionType.FIBONACCI; | ||
import static no.lundesgaard.startup.extreme.question.model.QuestionType.POWER; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@ContextConfiguration(classes = FibonacciQuestionFactory.class) | ||
@DisplayName("power question factory tests") | ||
class FibonacciQuestionFactoryTest extends BinaryMathsQuestionFactoryTest { | ||
@Autowired | ||
private FibonacciQuestionFactory factory; | ||
|
||
public FibonacciQuestionFactoryTest() { | ||
super(FIBONACCI, 50); | ||
} | ||
|
||
private static Stream<Arguments> questions() { | ||
return Stream.of( | ||
arguments("cafebabe", 0, 0, "what is the 4th number in the Fibonacci sequence", "3"), | ||
arguments("cafebabe", 7, 0, "what is the 11th number in the Fibonacci sequence", "89"), | ||
arguments("cafebabe", 8, 0, "what is the 12th number in the Fibonacci sequence", "144"), | ||
arguments("cafebabe", 9, 0, "what is the 13th number in the Fibonacci sequence", "233"), | ||
arguments("cafebabe", 10, 0, "what is the 14th number in the Fibonacci sequence", "377"), | ||
arguments("cafebabe", 17, 0, "what is the 21st number in the Fibonacci sequence", "10946"), | ||
arguments("cafebabe", 18, 0, "what is the 22nd number in the Fibonacci sequence", "17711"), | ||
arguments("cafebabe", 19, 0, "what is the 23rd number in the Fibonacci sequence", "28657") | ||
); | ||
} | ||
|
||
@Override | ||
protected QuestionFactory factory() { | ||
return factory; | ||
} | ||
} |