diff options
Diffstat (limited to 'src/com/p4square/grow/model')
-rw-r--r-- | src/com/p4square/grow/model/Answer.java | 63 | ||||
-rw-r--r-- | src/com/p4square/grow/model/Question.java | 101 |
2 files changed, 164 insertions, 0 deletions
diff --git a/src/com/p4square/grow/model/Answer.java b/src/com/p4square/grow/model/Answer.java new file mode 100644 index 0000000..4c84060 --- /dev/null +++ b/src/com/p4square/grow/model/Answer.java @@ -0,0 +1,63 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.util.Map; + +/** + * This is the model of an assessment question's answer. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class Answer { + 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()); + } + + if (mType != ScoreType.NONE) { + mScoreFactor = Float.valueOf((String) answer.get("score")); + } else { + mScoreFactor = 0; + } + + mNextQuestionId = (String) answer.get("nextQuestion"); + } + + public String getId() { + return mAnswerId; + } + + public String getText() { + return mAnswerText; + } + + public ScoreType getType() { + return mType; + } + + public float getScoreFactor() { + return mScoreFactor; + } + + public String getNextQuestion() { + return mNextQuestionId; + } +} diff --git a/src/com/p4square/grow/model/Question.java b/src/com/p4square/grow/model/Question.java new file mode 100644 index 0000000..387d723 --- /dev/null +++ b/src/com/p4square/grow/model/Question.java @@ -0,0 +1,101 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * Model of an assessment question. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class Question { + public static enum QuestionType { + TEXT, IMAGE, SLIDER, QUAD, CIRCLE; + } + + private final Map<String, Object> mMap; + private final String mQuestionId; + private final QuestionType mType; + private final String mQuestionText; + private Map<String, Answer> mAnswers; + + private final String mPreviousQuestionId; + private final String mNextQuestionId; + + public Question(final Map<String, Object> map) { + mMap = map; + mQuestionId = (String) map.get("id"); + mType = QuestionType.valueOf(((String) map.get("type")).toUpperCase()); + + mQuestionText = (String) map.get("text"); + + mPreviousQuestionId = (String) map.get("previousQuestion"); + mNextQuestionId = (String) map.get("nextQuestion"); + + mAnswers = new HashMap<String, Answer>(); + for (Map.Entry<String, Object> answer : + ((Map<String, Object>) map.get("answers")).entrySet()) { + + final String id = answer.getKey(); + final Map<String, Object> answerMap = (Map<String, Object>) answer.getValue(); + final Answer answerObj = new Answer(id, answerMap); + mAnswers.put(id, answerObj); + } + } + + public String getId() { + return mQuestionId; + } + + public QuestionType getType() { + return mType; + } + + public String getText() { + return mQuestionText; + } + + public String getPreviousQuestion() { + return mPreviousQuestionId; + } + + public String getNextQuestion() { + return mNextQuestionId; + } + + public Map<String, Answer> getAnswers() { + return Collections.unmodifiableMap(mAnswers); + } + + public Map<String, Object> getMap() { + return Collections.unmodifiableMap(mMap); + } + + /** + * Determine the id of the next question based on the answer to this + * question. + * + * @param answerid + * The id of the selected answer. + * @return a question id or null if this is the last question. + */ + public String getNextQuestion(String answerid) { + String nextQuestion = null; + + Answer a = mAnswers.get(answerid); + if (a != null) { + nextQuestion = a.getNextQuestion(); + } + + if (nextQuestion == null) { + nextQuestion = mNextQuestionId; + } + + return nextQuestion; + } +} |