01: /*
02: * Copyright 2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of 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,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: /*
18: * Originally written by Jason Hunter, http://www.servlets.com.
19: */
20:
21: package num;
22:
23: import java.util.*;
24:
25: public class NumberGuessBean {
26:
27: int answer;
28: boolean success;
29: String hint;
30: int numGuesses;
31:
32: public NumberGuessBean() {
33: reset();
34: }
35:
36: public void setGuess(String guess) {
37: numGuesses++;
38:
39: int g;
40: try {
41: g = Integer.parseInt(guess);
42: } catch (NumberFormatException e) {
43: g = -1;
44: }
45:
46: if (g == answer) {
47: success = true;
48: } else if (g == -1) {
49: hint = "a number next time";
50: } else if (g < answer) {
51: hint = "higher";
52: } else if (g > answer) {
53: hint = "lower";
54: }
55: }
56:
57: public boolean getSuccess() {
58: return success;
59: }
60:
61: public String getHint() {
62: return "" + hint;
63: }
64:
65: public int getNumGuesses() {
66: return numGuesses;
67: }
68:
69: public void reset() {
70: answer = Math.abs(new Random().nextInt() % 100) + 1;
71: success = false;
72: numGuesses = 0;
73: }
74: }
|