01: /*
02: * Copyright 2002-2004 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.util.Random;
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 { // TODO required by SerializedFlowExecutionContinuation
32:
33: public static final Random random = new Random();
34:
35: public static final String INVALID = "invalid";
36:
37: public static final String TOO_HIGH = "toohigh";
38:
39: public static final String TOO_LOW = "toolow";
40:
41: public static final String 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 result;
51:
52: public int getAnswer() {
53: return answer;
54: }
55:
56: public int getGuesses() {
57: return guesses;
58: }
59:
60: public String getResult() {
61: return result;
62: }
63:
64: public void setResult(String result) {
65: this .result = result;
66: }
67:
68: public long getDuration() {
69: // Calendar now = Calendar.getInstance();
70: // long durationMilliseconds = now.getTime().getTime() - start.getTime().getTime();
71: // return durationMilliseconds / 1000;
72: long now = System.currentTimeMillis();
73: return now - start;
74: }
75:
76: public String makeGuess(int guess) {
77: if (guess < 0 || guess > 100) {
78: setResult(INVALID);
79: } else {
80: guesses++;
81: if (answer < guess) {
82: setResult(TOO_HIGH);
83: } else if (answer > guess) {
84: setResult(TOO_LOW);
85: } else {
86: setResult(CORRECT);
87: }
88: }
89: return getResult();
90: }
91:
92: }
|