KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > bean > HigherLowerGame


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

16 package com.tctest.spring.bean;
17
18 //import java.util.Calendar;
19
import java.io.Serializable JavaDoc;
20 import java.util.Random JavaDoc;
21
22 /**
23  * Adapded from SWF numberguess example (made 1.4 compliant and non Serializable).
24  *
25  * Action that encapsulates logic for the number guess sample flow. Note that
26  * this is a stateful action: it holds modifiable state in instance members!
27  *
28  * @author Erwin Vervaet
29  * @author Keith Donald
30  */

31 public class HigherLowerGame implements Serializable JavaDoc { // TODO required by SerializedFlowExecutionContinuation
32

33     public static final Random JavaDoc random = new Random JavaDoc();
34
35   public static final String JavaDoc INVALID = "invalid";
36
37   public static final String JavaDoc TOO_HIGH = "toohigh";
38
39   public static final String JavaDoc TOO_LOW = "toolow";
40
41   public static final String JavaDoc CORRECT = "corrent";
42
43 // private Calendar start = Calendar.getInstance();
44
private long start = System.currentTimeMillis();
45
46     private int answer = random.nextInt(101);
47
48     private int guesses = 0;
49
50     private String JavaDoc result;
51   
52
53     public int getAnswer() {
54         return answer;
55     }
56
57     public int getGuesses() {
58         return guesses;
59     }
60
61     public String JavaDoc getResult() {
62         return result;
63     }
64
65     public void setResult(String JavaDoc result) {
66         this.result = result;
67     }
68
69     public long getDuration() {
70 // Calendar now = Calendar.getInstance();
71
// long durationMilliseconds = now.getTime().getTime() - start.getTime().getTime();
72
// return durationMilliseconds / 1000;
73
long now = System.currentTimeMillis();
74     return now - start;
75     }
76
77     public String JavaDoc makeGuess(int guess) {
78         if (guess < 0 || guess > 100) {
79             setResult(INVALID);
80         }
81         else {
82             guesses++;
83             if (answer < guess) {
84                 setResult(TOO_HIGH);
85             }
86             else if (answer > guess) {
87                 setResult(TOO_LOW);
88             }
89             else {
90                 setResult(CORRECT);
91             }
92         }
93         return getResult();
94     }
95
96 }
97
98
Popular Tags