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.
43 lines
1.3 KiB
43 lines
1.3 KiB
package services;
|
|
|
|
import interfaces.QuestionInterface;
|
|
import models.DifficultyLevel;
|
|
import java.util.HashSet;
|
|
import java.util.Random;
|
|
import java.util.Set;
|
|
|
|
public class QuestionGenerator {
|
|
private Random random;
|
|
private FileManager fileManager;
|
|
|
|
public QuestionGenerator() {
|
|
random = new Random();
|
|
fileManager = new FileManager();
|
|
}
|
|
|
|
public String[] generateQuestions(DifficultyLevel level, int count, String username) {
|
|
Set<String> existingQuestions = fileManager.loadExistingQuestions(username);
|
|
Set<String> newQuestions = new HashSet<>();
|
|
String[] questions = new String[count];
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
QuestionInterface question;
|
|
int attempts = 0;
|
|
|
|
do {
|
|
question = new MathQuestion(level);
|
|
attempts++;
|
|
if (attempts > 100) {
|
|
return null;
|
|
}
|
|
} while (existingQuestions.contains(question.getQuestionText()) ||
|
|
newQuestions.contains(question.getQuestionText()) ||
|
|
!question.isValid());
|
|
|
|
newQuestions.add(question.getQuestionText());
|
|
questions[i] = (i + 1) + ". " + question.getQuestionText();
|
|
}
|
|
|
|
return questions;
|
|
}
|
|
} |