/Users/lyon/j4p/src/net/server/num/NumberGuessBean.java

1    // Number Guess Game 
2    // Written by Jason Hunter <jasonh@kasoftware.com>, CTO, K&A Software 
3    // Copyright 1999, K&A Software, distributed by Sun with permission 
4     
5    package net.server.num; 
6     
7    import java.util.Random; 
8     
9    public class NumberGuessBean { 
10    
11       int answer; 
12       boolean success; 
13       String hint; 
14       int numGuesses; 
15    
16       public NumberGuessBean() { 
17           reset(); 
18       } 
19    
20       public void setGuess(String guess) { 
21           numGuesses++; 
22    
23           int g; 
24           try { 
25               g = Integer.parseInt(guess); 
26           } catch (NumberFormatException e) { 
27               g = -1; 
28           } 
29    
30           if (g == answer) { 
31               success = true; 
32           } else if (g == -1) { 
33               hint = "a number next time"; 
34           } else if (g < answer) { 
35               hint = "higher"; 
36           } else if (g > answer) { 
37               hint = "lower"; 
38           } 
39       } 
40    
41       public boolean getSuccess() { 
42           return success; 
43       } 
44    
45       public String getHint() { 
46           return "" + hint; 
47       } 
48    
49       public int getNumGuesses() { 
50           return numGuesses; 
51       } 
52    
53       public void reset() { 
54           answer = Math.abs(new Random().nextInt() % 100) + 1; 
55           success = false; 
56           numGuesses = 0; 
57       } 
58   } 
59