diff options
Diffstat (limited to 'src/com/p4square/grow/model/Answer.java')
-rw-r--r-- | src/com/p4square/grow/model/Answer.java | 135 |
1 files changed, 107 insertions, 28 deletions
diff --git a/src/com/p4square/grow/model/Answer.java b/src/com/p4square/grow/model/Answer.java index 4c84060..57a1e5d 100644 --- a/src/com/p4square/grow/model/Answer.java +++ b/src/com/p4square/grow/model/Answer.java @@ -4,7 +4,7 @@ package com.p4square.grow.model; -import java.util.Map; +import org.apache.log4j.Logger; /** * This is the model of an assessment question's answer. @@ -12,52 +12,131 @@ import java.util.Map; * @author Jesse Morgan <jesse@jesterpm.net> */ public class Answer { + private static final Logger LOG = Logger.getLogger(Answer.class); + + /** + * ScoreType determines how the answer will be scored. + * + */ public static enum ScoreType { - NONE, AVERAGE, TRUMP; - } - - private final String mAnswerId; - private final String mAnswerText; - private final ScoreType mType; - private final float mScoreFactor; - private final String mNextQuestionId; - - public Answer(final String id, final Map<String, Object> answer) { - mAnswerId = id; - mAnswerText = (String) answer.get("text"); - final String typeStr = (String) answer.get("type"); - if (typeStr == null) { - mType = ScoreType.AVERAGE; - } else { - mType = ScoreType.valueOf(typeStr.toUpperCase()); - } + /** + * This question has no effect on the score. + */ + NONE, - if (mType != ScoreType.NONE) { - mScoreFactor = Float.valueOf((String) answer.get("score")); - } else { - mScoreFactor = 0; - } + /** + * The score of this question is part of the average. + */ + AVERAGE, - mNextQuestionId = (String) answer.get("nextQuestion"); + /** + * The score of this question is the total score, no other questions + * matter after this point. + */ + TRUMP; + + @Override + public String toString() { + return name().toLowerCase(); + } } - public String getId() { - return mAnswerId; + private String mAnswerText; + private ScoreType mType; + private float mScoreFactor; + private String mNextQuestionId; + + public Answer() { + mType = ScoreType.AVERAGE; } + /** + * @return The text associated with the answer. + */ public String getText() { return mAnswerText; } + /** + * Set the text associated with the answer. + * @param text The new text. + */ + public void setText(String text) { + mAnswerText = text; + } + + /** + * @return the ScoreType for the Answer. + */ public ScoreType getType() { return mType; } - public float getScoreFactor() { + /** + * Set the ScoreType for the answer. + * @param type The new ScoreType. + */ + public void setType(ScoreType type) { + mType = type; + } + + /** + * @return the delta of the score if this answer is selected. + */ + public float getScore() { + if (mType == ScoreType.NONE) { + return 0; + } + return mScoreFactor; } + /** + * Set the score delta for this answer. + * @param score The new delta. + */ + public void setScore(float score) { + mScoreFactor = score; + } + + /** + * @return the id of the next question if this answer is selected, or null + * if selecting this answer has no effect. + */ public String getNextQuestion() { return mNextQuestionId; } + + /** + * Set the id of the next question when this answer is selected. + * @param id The next question id or null to proceed as usual. + */ + public void setNextQuestion(String id) { + mNextQuestionId = id; + } + + /** + * Adjust the running score for the selection of this answer. + * @param score The running score to adjust. + * @return true if scoring should continue, false if this answer trumps all. + */ + public boolean score(final Score score) { + switch (getType()) { + case TRUMP: + score.sum = getScore(); + score.count = 1; + return false; // Quit scoring. + + case AVERAGE: + LOG.error("ScoreType.AVERAGE: { delta: \"" + getScore() + "\" }"); + score.sum += getScore(); + score.count++; + break; + + case NONE: + break; + } + + return true; // Continue scoring + } } |