TestQuestion.java
package symplik.flower.sample;

//~--- non-JDK imports --------------------------------------------------------

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import symplik.flower.Answer;
import symplik.flower.Choice;
import symplik.flower.Helper;
import symplik.flower.Question;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;

public class TestQuestion extends Question {
    private static String QUESTION_NO       = "QUESTION_NO";
    private static String TOTAL_SCORE       = "TOTAL_SCORE";
    private String        qContent          = null;
    private int           qNo               = 0;
    private int           totalNoOfQuestion = 0;
    private int           totalScore        = 0;

    @Override
    public boolean enterQuestion() {
        qNo        = Helper.getInt(Answer.getInstance().getB(QUESTION_NO), 1);
        totalScore = Helper.getInt(Answer.getInstance().getB(TOTAL_SCORE), 0);

        try {
            InputStream is      = this.getClass().getResourceAsStream("/symplik/flower/sample/Test.xml");
            SAXBuilder  builder = new SAXBuilder();
            Document    doc     = builder.build(is);
            XPath       xpath   = XPath.newInstance("//Question");

            totalNoOfQuestion = xpath.selectNodes(doc).size();
            xpath             = XPath.newInstance("//Question[@no='" + qNo + "']");
            qContent          = ((Element) xpath.selectSingleNode(doc)).getValue();
        } catch (JDOMException je) {
            Helper.log(LOG_ERROR, "Malformat XML file");
            System.exit(1);
        } catch (IOException ioe) {
            Helper.log(LOG_ERROR, "Unable to read question XML file");
            System.exit(1);
        }

        return true;
    }

    @Override
    public String getQuestion() {
        return "Question " + qNo + "\n";
    }

    @Override
    public String getExplanation() {
        return qContent;
    }

    @Override
    public ArrayList<choice> choices() {
        ArrayList<choice> al = new ArrayList<choice>();

        al.add(new Choice("1", "Rarely"));
        al.add(new Choice("2", "Occasionally"));
        al.add(new Choice("3", "Frequently"));
        al.add(new Choice("4", "Often"));
        al.add(new Choice("5", "Always"));

        return al;
    }

    @Override
    public boolean leaveQuestion() {
        totalScore = Integer.valueOf(Answer.getInstance().getA(this)) + totalScore;
        Helper.log(LOG_DEBUG, "totalScore = " + totalScore);
        Answer.getInstance().putB(TOTAL_SCORE, String.valueOf(totalScore));
        Answer.getInstance().putB(QUESTION_NO, String.valueOf(qNo));

        return true;
    }

    @Override
    public String nextAction() {
        if (qNo >= totalNoOfQuestion) {
            return "symplik.flower.sample.TestResult";
        } else {
            Answer.getInstance().putB(QUESTION_NO, String.valueOf(qNo + 1));

            return this.getClass().getCanonicalName();
        }
    }

    @Override
    public String getTitle() {
        return "Internet Addiction Test | Question " + qNo;
    }
}