1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package guessNumber;
19
20 import java.util.Random;
21
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.Validator;
25 import javax.faces.validator.ValidatorException;
26 import javax.faces.validator.LongRangeValidator;
27
28
29 public class UserNumberBean {
30
31 Integer userNumber = null;
32 Integer randomInt = null;
33 String response = null;
34
35
36 public UserNumberBean () {
37 Random randomGR = new Random();
38 randomInt = new Integer(randomGR.nextInt(10));
39 System.out.println("Duke's number: "+randomInt);
40 }
41
42 public void setUserNumber(Integer user_number) {
43 userNumber = user_number;
44 System.out.println("Set userNumber " + userNumber);
45 }
46
47 public Integer getUserNumber() {
48 System.out.println("get userNumber " + userNumber);
49 return userNumber;
50 }
51
52 public String getResponse() {
53 if(userNumber != null && userNumber.compareTo(randomInt) == 0) {
54 return "Yay! You got it!";
55 }
56 else {
57 return "Sorry, "+userNumber+" is incorrect.";
58 }
59 }
60
61 protected String [] status = null;
62 public String [] getStatus() {
63 return status;
64 }
65
66 public void setStatus(String [] newStatus) {
67 status = newStatus;
68 }
69
70 private int maximum = 0;
71 private boolean maximumSet = false;
72
73 public int getMaximum() {
74 return (this.maximum);
75 }
76
77 public void setMaximum(int maximum) {
78 this.maximum = maximum;
79 this.maximumSet = true;
80 }
81
82 private int minimum = 0;
83 private boolean minimumSet = false;
84
85 public int getMinimum() {
86 return (this.minimum);
87 }
88
89
90 public void setMinimum(int minimum) {
91 this.minimum = minimum;
92 this.minimumSet = true;
93 }
94
95 public void validate(FacesContext context,
96 UIComponent component,
97 Object value) throws ValidatorException {
98
99 if ((context == null) || (component == null)) {
100 throw new NullPointerException();
101 }
102 if (value != null) {
103 try {
104 int converted = intValue(value);
105 if (maximumSet &&
106 (converted > maximum)) {
107 if (minimumSet) {
108 throw new ValidatorException(MessageFactory.getMessage
109 (context,
110 Validator.NOT_IN_RANGE_MESSAGE_ID,
111 new Object[] {
112 new Integer(minimum),
113 new Integer(maximum) }));
114
115 }
116 else {
117 throw new ValidatorException(MessageFactory.getMessage
118 (context,
119 LongRangeValidator.MAXIMUM_MESSAGE_ID,
120 new Object[] {
121 new Integer(maximum) }));
122 }
123 }
124 if (minimumSet &&
125 (converted < minimum)) {
126 if (maximumSet) {
127 throw new ValidatorException(MessageFactory.getMessage
128 (context,
129 Validator.NOT_IN_RANGE_MESSAGE_ID,
130 new Object[] {
131 new Double(minimum),
132 new Double(maximum) }));
133
134 }
135 else {
136 throw new ValidatorException(MessageFactory.getMessage
137 (context,
138 LongRangeValidator.MINIMUM_MESSAGE_ID,
139 new Object[] {
140 new Integer(minimum) }));
141 }
142 }
143 } catch (NumberFormatException e) {
144 throw new ValidatorException(MessageFactory.getMessage
145 (context, LongRangeValidator.TYPE_MESSAGE_ID));
146 }
147 }
148
149 }
150
151 private int intValue(Object attributeValue)
152 throws NumberFormatException {
153
154 if (attributeValue instanceof Number) {
155 return ( ((Number) attributeValue).intValue() );
156 } else {
157 return (Integer.parseInt(attributeValue.toString()));
158 }
159
160 }
161
162
163
164
165 }