You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
package com.mathlearning.model;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
public abstract class AbstractQuestionGenerator implements QuestionGenerator {
|
|
|
|
@Override
|
|
public List<Question> generateQuestions(String level, int count) {
|
|
List<Question> questions = new ArrayList<>();
|
|
Set<String> questionTexts = new HashSet<>();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
Question question = generateUniqueQuestion(level, i, questionTexts);
|
|
if (question != null) {
|
|
questions.add(question);
|
|
}
|
|
}
|
|
|
|
return questions;
|
|
}
|
|
|
|
private Question generateUniqueQuestion(String level, int index, Set<String> questionTexts) {
|
|
Question question;
|
|
int attempt = 0;
|
|
do {
|
|
question = generateQuestion(level, index);
|
|
attempt++;
|
|
} while (question != null && questionTexts.contains(question.getQuestionText()) && attempt < 10);
|
|
|
|
if (question != null) {
|
|
questionTexts.add(question.getQuestionText());
|
|
}
|
|
|
|
return question;
|
|
}
|
|
|
|
protected abstract Question generateQuestion(String level, int index);
|
|
} |