// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package website.quiz;

import java.util.List;
import java.util.ArrayList;

public class Quiz {
    private final int MAX_TRIES = 3;
    private List<QuizQuestion> questions;
    private int questionIndex;
    private boolean wrong;
    private int numTries;
    private Integer answer;
    public Quiz() {
        questions = new ArrayList<QuizQuestion>();
        questions.add(new QuizQuestion("2 + 2", 4));
        questions.add(new QuizQuestion("2 - 2", 0));
        questions.add(new QuizQuestion("2 * 2", 4));
        questions.add(new QuizQuestion("2 / 2", 1));
        questions.add(new QuizQuestion("2 ^ 2", 4));
        questionIndex = 0;
        numTries = 0;
    }
    public int getMaxTries() {
        return MAX_TRIES;
    }
    public int getNumQuestions() {
        return questions.size();
    }
    public QuizQuestion getQuestion() {
        return questions.get(questionIndex);
    }
    public int getQuestionIndex() {
        return questionIndex;
    }
    public boolean getWrong() {
        return wrong;
    }
    public Integer getAnswer() {
        return null;
    }
    public void setAnswer(Integer answer) {
        this.answer = answer;
    }
    public String answerQuestion() {
        if(questions.get(questionIndex).getAnswer() == answer.intValue()) {
            questionIndex++;
            if(questionIndex >= questions.size()) {
                return "completed";
            } else {
                wrong = false;
                return "continue";
            }
        } else {
            numTries++;
            if(numTries >= MAX_TRIES) {
                return "failed";
            } else {
                wrong = true;
                return "continue";
            }
        }
    }
}
