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.
math-learing/src/com/personalproject/model/QuizQuestion.java

73 lines
2.0 KiB

package com.personalproject.model;
import java.util.List;
/**
* Represents a quiz question with multiple choice options.
*/
public final class QuizQuestion {
private final String questionText;
private final List<String> options;
private final int correctAnswerIndex;
/**
* Creates a new quiz question.
*
* @param questionText The text of the question
* @param options The list of answer options
* @param correctAnswerIndex The index of the correct answer in the options list
*/
public QuizQuestion(String questionText, List<String> options, int correctAnswerIndex) {
if (questionText == null || questionText.trim().isEmpty()) {
throw new IllegalArgumentException("Question text cannot be null or empty");
}
if (options == null || options.size() < 2) {
throw new IllegalArgumentException("Options must contain at least 2 choices");
}
if (correctAnswerIndex < 0 || correctAnswerIndex >= options.size()) {
throw new IllegalArgumentException("Correct answer index out of bounds");
}
this.questionText = questionText;
this.options = List.copyOf(options); // Immutable copy
this.correctAnswerIndex = correctAnswerIndex;
}
/**
* Gets the question text.
*
* @return The question text
*/
public String getQuestionText() {
return questionText;
}
/**
* Gets the list of answer options.
*
* @return An unmodifiable list of answer options
*/
public List<String> getOptions() {
return options;
}
/**
* Gets the index of the correct answer in the options list.
*
* @return The index of the correct answer
*/
public int getCorrectAnswerIndex() {
return correctAnswerIndex;
}
/**
* Checks if the given answer index matches the correct answer.
*
* @param answerIndex The index of the user's answer
* @return true if the answer is correct, false otherwise
*/
public boolean isAnswerCorrect(int answerIndex) {
return answerIndex == correctAnswerIndex;
}
}