KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > wap > quiz > Quiz


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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 JavaDoc;
23
24 /**
25  * Quiz deck for the Quiz application.
26  *
27  * @version $Id: Quiz.java,v 1.3 2004/02/19 17:38:02 hlship Exp $
28  * @author David Solis
29  *
30  **/

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 JavaDoc question = global.getQuestion(getQuestionNumber(), visit.getQuestionSet());
41         setQuestion(question);
42     }
43
44     public IPropertySelectionModel getChoiceModel()
45     {
46         return new StringPropertySelectionModel((String JavaDoc[])getQuestion().get(Global.CHOICES_KEY));
47     }
48
49     public void process(IRequestCycle cycle)
50     {
51         Visit visit = (Visit) getVisit();
52         int rightAnswer = ((Integer JavaDoc)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 JavaDoc getQuestion();
93     public abstract void setQuestion(Map JavaDoc question);
94
95     public abstract int getChoice();
96
97
98 }
99
Popular Tags