1 package net.trajano.twiff.sample.web;
2
3 import java.io.Serializable;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import net.trajano.twiff.sample.component.NumberToGuess;
7
8 /***
9 * Page bean that handles the guess application.
10 *
11 * @author Archimedes Trajano
12 */
13 public class Guess implements Serializable {
14 /***
15 * Hint text.
16 */
17 private static String GUESS_NEW_HINT = "Guess a number between 1 and 20";
18
19 /***
20 * Current guess.
21 */
22 private int guess;
23
24 /***
25 * The hint.
26 */
27 private String hint = GUESS_NEW_HINT;
28
29 /***
30 * The number to guess.
31 */
32 private final NumberToGuess numberToGuess;
33
34 /***
35 * Constructs the page bean.
36 *
37 * @param numberToGuess
38 * the number to guess stored in the session.
39 */
40 public Guess(final NumberToGuess numberToGuess) {
41 this.numberToGuess = numberToGuess;
42 }
43
44 /***
45 * @return Returns the guess.
46 */
47 public final int getGuess() {
48 return guess;
49 }
50
51 /***
52 * @return Returns the hint.
53 */
54 public final String getHint() {
55 return hint;
56 }
57
58 /***
59 * @return Returns the numberToGuess.
60 */
61 public final NumberToGuess getNumberToGuess() {
62 return numberToGuess;
63 }
64
65 /***
66 * Performs the action logic.
67 *
68 * @return action result.
69 * @throws MalformedURLException
70 */
71 public final Object play() throws MalformedURLException {
72
73
74
75
76 if (getGuess() == 99) {
77 return new URL("http://www.slashdot.org/");
78 }
79 if (getGuess() == Integer.MIN_VALUE || getGuess() == numberToGuess.getNumber()) {
80 setHint(GUESS_NEW_HINT);
81 numberToGuess.newRandom();
82 } else {
83 if (getGuess() > numberToGuess.getNumber()) {
84 setHint("Too high");
85 } else {
86 setHint("Too low");
87 }
88 }
89 return "guess";
90 }
91
92 /***
93 * Sets the guess from the POST parameters.
94 *
95 * @param guess
96 * The guess to set.
97 */
98 public final void setGuess(int guess) {
99 this.guess = guess;
100 }
101
102 /***
103 * @param hint
104 * The hint to set.
105 */
106 public final void setHint(String hint) {
107 this.hint = hint;
108 }
109 }