Skip to content

Commit

Permalink
GH-1 added and implemented fibonacci question factory and did some re…
Browse files Browse the repository at this point in the history
…factoring
  • Loading branch information
georglundesgaard committed May 9, 2020
1 parent 00f0de7 commit a190a09
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.lundesgaard.startup.extreme.question.service;

import static no.lundesgaard.startup.extreme.question.model.QuestionType.ADDITION;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.FIBONACCI;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.MULTIPLICATION;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.POWER;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.SUBTRACTION;
Expand All @@ -14,7 +15,7 @@

@Component
public class Randomizer {
private static final QuestionType[] QUESTION_TYPES = { ADDITION, SUBTRACTION, MULTIPLICATION, POWER };
private static final QuestionType[] QUESTION_TYPES = { ADDITION, SUBTRACTION, MULTIPLICATION, POWER, FIBONACCI };

public int nextInt(int bound) {
return new Random().nextInt(bound);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.lundesgaard.startup.extreme.question.service.factory;

import static java.lang.String.format;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.ADDITION;

import no.lundesgaard.startup.extreme.question.service.Randomizer;
Expand All @@ -9,6 +10,6 @@
@Component
public class AdditionQuestionFactory extends BinaryMathsQuestionFactory {
public AdditionQuestionFactory(Randomizer randomizer) {
super(randomizer, ADDITION, "what is %d plus %d", (n1, n2) -> String.valueOf(n1 + n2));
super(randomizer, ADDITION, (n1, n2) -> format("what is %d plus %d", n1, n2), (n1, n2) -> String.valueOf(n1 + n2));
}
}
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));
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.lundesgaard.startup.extreme.question.service.factory;

import static java.lang.String.format;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.MULTIPLICATION;

import no.lundesgaard.startup.extreme.question.service.Randomizer;
Expand All @@ -9,6 +10,6 @@
@Component
public class MultiplicationQuestionFactory extends BinaryMathsQuestionFactory {
public MultiplicationQuestionFactory(Randomizer randomizer) {
super(randomizer, MULTIPLICATION, "what is %d multiplied by %d", (n1, n2) -> String.valueOf(n1 * n2));
super(randomizer, MULTIPLICATION, (n1, n2) -> format("what is %d multiplied by %d", n1, n2), (n1, n2) -> String.valueOf(n1 * n2));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.lundesgaard.startup.extreme.question.service.factory;

import static java.lang.String.format;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.POWER;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.SUBTRACTION;

Expand All @@ -10,6 +11,6 @@
@Component
public class PowerQuestionFactory extends BinaryMathsQuestionFactory {
public PowerQuestionFactory(Randomizer randomizer) {
super(randomizer, POWER, 20, "what is %d to the power of %d", (n1, n2) -> String.valueOf((long) Math.pow(n1, n2)));
super(randomizer, POWER, 20, (n1, n2) -> format("what is %d to the power of %d", n1, n2), (n1, n2) -> String.valueOf((long) Math.pow(n1, n2)));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.lundesgaard.startup.extreme.question.service.factory;

import static java.lang.String.format;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.SUBTRACTION;

import no.lundesgaard.startup.extreme.question.service.Randomizer;
Expand All @@ -9,6 +10,6 @@
@Component
public class SubtractionQuestionFactory extends BinaryMathsQuestionFactory {
public SubtractionQuestionFactory(Randomizer randomizer) {
super(randomizer, SUBTRACTION, "what is %d minus %d", (n1, n2) -> String.valueOf(n1 - n2));
super(randomizer, SUBTRACTION, (n1, n2) -> format("what is %d minus %d", n1, n2), (n1, n2) -> String.valueOf(n1 - n2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class QuestionApplicationTest {
private int serverPort;

@ParameterizedTest
@ValueSource(strings = {"ADDITION", "SUBTRACTION", "MULTIPLICATION", "POWER"})
@ValueSource(strings = {"ADDITION", "SUBTRACTION", "MULTIPLICATION", "POWER", "FIBONACCI"})
@DisplayName("generate random question for each question type")
void generateRandomQuestion(QuestionType questionType) {
when(randomizer.nextQuestionType(1)).thenReturn(questionType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.lundesgaard.startup.extreme.question.service;

import static no.lundesgaard.startup.extreme.question.model.QuestionType.ADDITION;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.FIBONACCI;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.MULTIPLICATION;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.POWER;
import static no.lundesgaard.startup.extreme.question.model.QuestionType.SUBTRACTION;
Expand All @@ -22,7 +23,7 @@
@ExtendWith(SpringExtension.class)
@DisplayName("randomizer tests")
class RandomizerTest {
private static final QuestionType[] QUESTION_TYPES = { ADDITION, SUBTRACTION, MULTIPLICATION, POWER };
private static final QuestionType[] QUESTION_TYPES = { ADDITION, SUBTRACTION, MULTIPLICATION, POWER, FIBONACCI };

@MockBean(answer = CALLS_REAL_METHODS)
private Randomizer randomizer;
Expand All @@ -45,7 +46,7 @@ void nextQuestionId() {
}

@ParameterizedTest
@ValueSource(strings = {"0", "1", "2", "3"})
@ValueSource(strings = {"0", "1", "2", "3", "4"})
@DisplayName("next question type can return all question types")
void nextQuestionType(int index) {
when(randomizer.nextInt(QUESTION_TYPES.length)).thenReturn(index);
Expand Down
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;
}
}

0 comments on commit a190a09

Please sign in to comment.