1 15 package org.apache.tapestry.wap.quiz; 16 17 import org.apache.tapestry.IRequestCycle; 18 import org.apache.tapestry.form.IPropertySelectionModel; 19 import org.apache.tapestry.form.StringPropertySelectionModel; 20 import org.apache.tapestry.wml.Deck; 21 22 import java.util.Map ; 23 24 31 32 public abstract class Quiz extends Deck 33 { 34 int questionNumber; 35 36 public void fetch() 37 { 38 Visit visit = (Visit) getVisit(); 39 Global global = (Global) getGlobal(); 40 Map question = global.getQuestion(getQuestionNumber(), visit.getQuestionSet()); 41 setQuestion(question); 42 } 43 44 public IPropertySelectionModel getChoiceModel() 45 { 46 return new StringPropertySelectionModel((String [])getQuestion().get(Global.CHOICES_KEY)); 47 } 48 49 public void process(IRequestCycle cycle) 50 { 51 Visit visit = (Visit) getVisit(); 52 int rightAnswer = ((Integer )getQuestion().get(Global.ANSWER_KEY)).intValue(); 53 if (getChoice() == rightAnswer) 54 { 55 int points = visit.getPoints(); 56 points += visit.getQuestionSet(); 57 visit.setPoints(points); 58 } 59 int questionNumber = getQuestionNumber() + 1; 60 if (questionNumber < visit.getNumberOfQuestions()) 61 { 62 setQuestionNumber(questionNumber); 63 fetch(); 64 } 65 else 66 { 67 Scores scoresDeck = (Scores)cycle.getPage("Scores"); 68 Global global = (Global) getGlobal(); 69 boolean isNewHighscore = global.addHighscore(visit.getPoints(), visit.getUsername()) != -1; 70 scoresDeck.setNewHighscore(isNewHighscore); 71 scoresDeck.setHighscores(global.getHighscores()); 72 cycle.activate(scoresDeck); 73 } 74 } 75 76 protected void initialize() 77 { 78 questionNumber = 0; 79 } 80 81 public int getQuestionNumber() 82 { 83 return questionNumber; 84 } 85 86 public void setQuestionNumber(int questionNumber) 87 { 88 this.questionNumber = questionNumber; 89 fireObservedChange("questionNumber", questionNumber); 90 } 91 92 public abstract Map getQuestion(); 93 public abstract void setQuestion(Map question); 94 95 public abstract int getChoice(); 96 97 98 } 99 | Popular Tags |