Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
771 blopes 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package num;
18
 
19
import java.io.Serializable;
20
import java.util.Random;
21
 
22
public class NumberGuessBean implements Serializable {
23
 
24
    private static final long serialVersionUID = 1L;
25
 
26
    private int answer;
27
    private String hint;
28
    private int numGuesses;
29
    private boolean success;
30
    private final Random random = new Random();
31
 
32
    public NumberGuessBean() {
33
        reset();
34
    }
35
 
36
    public int getAnswer() {
37
        return answer;
38
    }
39
 
40
    public void setAnswer(int answer) {
41
        this.answer = answer;
42
    }
43
 
44
    public String getHint() {
45
        return "" + hint;
46
    }
47
 
48
    public void setHint(String hint) {
49
        this.hint = hint;
50
    }
51
 
52
    public void setNumGuesses(int numGuesses) {
53
        this.numGuesses = numGuesses;
54
    }
55
 
56
    public int getNumGuesses() {
57
        return numGuesses;
58
    }
59
 
60
    public boolean getSuccess() {
61
        return success;
62
    }
63
 
64
    public void setSuccess(boolean success) {
65
        this.success = success;
66
    }
67
 
68
    public void setGuess(String guess) {
69
        numGuesses++;
70
 
71
        int g;
72
        try {
73
            g = Integer.parseInt(guess);
74
        } catch (NumberFormatException e) {
75
            g = -1;
76
        }
77
 
78
        if (g == answer) {
79
            success = true;
80
        } else if (g == -1) {
81
            hint = "a number next time";
82
        } else if (g < answer) {
83
            hint = "higher";
84
        } else if (g > answer) {
85
            hint = "lower";
86
        }
87
    }
88
 
89
    public void reset() {
90
        answer = Math.abs(random.nextInt() % 100) + 1;
91
        success = false;
92
        numGuesses = 0;
93
    }
94
}