KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedReader JavaDoc;
18 import java.io.FileReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.ServletContext JavaDoc;
28
29 import org.apache.hivemind.Resource;
30 import org.apache.tapestry.ApplicationServlet;
31 import org.apache.tapestry.IRequestCycle;
32 import org.apache.tapestry.engine.IPropertySource;
33 import org.apache.tapestry.request.RequestContext;
34 import org.apache.tapestry.resource.ContextResource;
35 import org.apache.tapestry.util.StringSplitter;
36
37 /**
38  * Global object for the Quiz application.
39  * This class has the following responsabilities:
40  * <ul>
41  * <li>Read questions from the specified files</li>
42  * <li>Provide questions by index</li>
43  * <li>Keep high scores in memory</li>
44  * </ul>
45  *
46  * @version $Id: Global.java,v 1.6 2004/06/21 19:11:52 hlship Exp $
47  * @author David Solis
48  *
49  **/

50
51 public class Global implements Serializable JavaDoc
52 {
53     private boolean init = false;
54
55     private int points[];
56     private String JavaDoc names[];
57
58     private static final List JavaDoc easyQuestions = new ArrayList JavaDoc();
59     private static final List JavaDoc mediumQuestions = new ArrayList JavaDoc();
60     private static final List JavaDoc hardQuestions = new ArrayList JavaDoc();
61
62     public static final int EASY_QUESTIONS = 1;
63     public static final int MEDIUM_QUESTIONS = 2;
64     public static final int HARD_QUESTIONS = 3;
65
66     public static final String JavaDoc EASY_LEVEL = "easy";
67     public static final String JavaDoc MEDIUM_LEVEL = "medium";
68     public static final String JavaDoc HARD_LEVEL = "hard";
69
70     public static final String JavaDoc QUESTION_KEY = "question";
71     public static final String JavaDoc ANSWER_KEY = "answer";
72     public static final String JavaDoc CHOICES_KEY = "choices";
73
74     public List JavaDoc getHighscores()
75     {
76         List JavaDoc result = new ArrayList JavaDoc();
77         for (int i = 0; i < 9; i++)
78         {
79             String JavaDoc name = names[i];
80             if (!"nobody".equals(name))
81             {
82                 Map JavaDoc map = new HashMap JavaDoc();
83                 map.put("name", name);
84                 map.put("score", new Integer JavaDoc(points[i]));
85                 result.add(map);
86             }
87         }
88         return result;
89     }
90
91     /**
92      * Checks if score is high scores
93      **/

94     public boolean isHighscore(int score)
95     {
96         return (score >= points[9]);
97     }
98
99     /**
100      * Checks if score is a new high score
101      **/

102     public boolean isNewHighscore(int score)
103     {
104         return (score > points[9]);
105     }
106
107     /**
108      * Adds score/name to the highscore table,
109      * returns position in highscore table or -1 if no highscore
110      **/

111     public int addHighscore(int score, String JavaDoc name)
112     {
113         int i = -1;
114         if (isNewHighscore(score))
115         {
116             for (i = 0; i <= 9; i++)
117             {
118                 if (score > points[i])
119                     break;
120             }
121             for (int j = 9; j >= i + 1; j--)
122             {
123                 points[j] = points[j - 1];
124                 names[j] = names[j - 1];
125             }
126             points[i] = score;
127             names[i] = name;
128         }
129         return i;
130     }
131
132     /**
133      * "getQuestionSet" method returns a number representing the right difficulty setting.
134      *
135      * @param level an <code>String</code> value
136      * @return an <code>int</code> value
137      */

138     public int getQuestionSet(String JavaDoc level)
139     {
140         if (HARD_LEVEL.equals(level))
141             return HARD_QUESTIONS;
142         if (MEDIUM_LEVEL.equals(level))
143             return MEDIUM_QUESTIONS;
144         return EASY_QUESTIONS;
145     }
146
147     /**
148      * "getQuestion" method returns a String that contains the question, the alternatives and the correct answer.
149      *
150      * @param questionToBeAsked an <code>int</code> value
151      * @param questionSet an <code>int</code> value
152      * @return a <code>String</code> value
153      */

154     public Map JavaDoc getQuestion(int questionToBeAsked, int questionSet)
155     {
156         String JavaDoc line;
157         List JavaDoc list;
158
159         switch (questionSet)
160         {
161             case EASY_QUESTIONS :
162                 list = easyQuestions;
163                 break;
164             case MEDIUM_QUESTIONS :
165                 list = mediumQuestions;
166                 break;
167             default :
168                 list = hardQuestions;
169                 break;
170         }
171         line = (String JavaDoc) list.get(questionToBeAsked);
172         StringSplitter splitter = new StringSplitter(';');
173         String JavaDoc[] result = splitter.splitToArray(line);
174         HashMap JavaDoc map = new HashMap JavaDoc();
175         map.put(QUESTION_KEY, result[0]);
176         int answer = Integer.parseInt(result[1]) - 1;
177         map.put(ANSWER_KEY, new Integer JavaDoc(answer));
178         int length = result.length - 2;
179         String JavaDoc[] choices = new String JavaDoc[length];
180         System.arraycopy(result, 2, choices, 0, length);
181         map.put(CHOICES_KEY, choices);
182         return map;
183     }
184
185     /**
186      * "getNumberOfQuestions" method returns the number of questions (Strings) in the questions table that is used.
187      *
188      * @param questionSet an <code>int</code> value
189      * @return an <code>int</code> value
190      */

191     public int getNumberOfQuestions(int questionSet)
192     {
193         switch (questionSet)
194         {
195             case EASY_QUESTIONS :
196                 return easyQuestions.size();
197             case MEDIUM_QUESTIONS :
198                 return mediumQuestions.size();
199             default :
200                 return hardQuestions.size();
201         }
202     }
203
204     public void initialize(IRequestCycle cycle)
205     {
206         if (!init)
207         {
208             RequestContext requestContext = cycle.getRequestContext();
209             ApplicationServlet servlet = requestContext.getServlet();
210             ServletContext JavaDoc context = servlet.getServletContext();
211             IPropertySource propertySource = cycle.getEngine().getPropertySource();
212
213             Resource webInfLocation = new ContextResource(context, "/WEB-INF/");
214
215             Resource webInfAppLocation =
216                 webInfLocation.getRelativeResource(servlet.getServletName() + "/");
217
218             readQuestions(
219                 easyQuestions,
220                 webInfAppLocation,
221                 propertySource.getPropertyValue("easyquestionsfile"));
222             readQuestions(
223                 mediumQuestions,
224                 webInfAppLocation,
225                 propertySource.getPropertyValue("mediumquestiosfile"));
226             readQuestions(
227                 hardQuestions,
228                 webInfAppLocation,
229                 propertySource.getPropertyValue("hardquestionsfile"));
230             points = new int[10];
231             names = new String JavaDoc[10];
232             for (int i = 0; i <= 9; i++)
233             {
234                 points[i] = 0;
235                 names[i] = "nobody";
236             }
237             init = true;
238         }
239     }
240
241     /**
242      * This method reads the questions from the appropriate file to the appropriate Vector.
243      *
244      * @param questions a <code>Vector</code> value
245      * @param questionsFile a <code>String</code> value
246      * @exception java.io.IOException if an error occurs
247      */

248     private void readQuestions(List JavaDoc questions, String JavaDoc questionsFile) throws IOException JavaDoc
249     {
250         BufferedReader JavaDoc reader = null;
251         String JavaDoc line;
252         reader = new BufferedReader JavaDoc(new FileReader JavaDoc(questionsFile));
253         while ((line = reader.readLine()) != null)
254         {
255             if (line.trim().equals(""))
256                 break;
257             questions.add(line);
258         }
259     }
260
261     private void readQuestions(List JavaDoc questions, Resource folder, String JavaDoc filename)
262     {
263         Resource resource = folder.getRelativeResource(filename);
264
265         URL JavaDoc url = resource.getResourceURL();
266
267         try
268         {
269             readQuestions(questions, url.getFile());
270         }
271         catch (IOException JavaDoc e)
272         {
273             e.printStackTrace(System.err);
274         }
275     }
276
277 }
278
Popular Tags