View Javadoc

1   /* Copyright 2004 Apache Software Foundation
2   *
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15   
16  package guessNumber;
17  
18  import java.util.Random;
19  
20  import javax.faces.component.UIComponent;
21  import javax.faces.context.FacesContext;
22  import javax.faces.validator.Validator;
23  import javax.faces.validator.ValidatorException;
24  import javax.faces.validator.LongRangeValidator;
25  
26  
27  public class UserNumberBean {
28  
29      Integer userNumber = null;
30      Integer randomInt = null;
31      String response = null;
32      
33  
34      public UserNumberBean () {
35  	Random randomGR = new Random();
36  	randomInt = new Integer(randomGR.nextInt(10));
37          System.out.println("Duke's number: "+randomInt);
38      }
39    
40      public void setUserNumber(Integer user_number) {
41          userNumber = user_number;
42          System.out.println("Set userNumber " + userNumber);
43      }
44  
45      public Integer getUserNumber() {
46          System.out.println("get userNumber " + userNumber);
47          return userNumber;
48      }
49  
50      public String getResponse() {
51      	if(userNumber != null && userNumber.compareTo(randomInt) == 0) {
52              return "Yay! You got it!"; 
53          }
54  	else {
55              return "Sorry, "+userNumber+" is incorrect.";
56          }
57      }
58  
59      protected String [] status = null;
60      public String [] getStatus() {
61      	return status;
62      }
63  
64      public void setStatus(String [] newStatus) {
65  	status = newStatus;
66      }
67  
68      private int maximum = 0;
69      private boolean maximumSet = false;
70  
71      public int getMaximum() {
72          return (this.maximum);
73      }
74  
75      public void setMaximum(int maximum) {
76          this.maximum = maximum;
77          this.maximumSet = true;
78      }
79  
80      private int minimum = 0;
81      private boolean minimumSet = false;
82  
83      public int getMinimum() {
84          return (this.minimum);
85      }
86  
87  
88      public void setMinimum(int minimum) {
89          this.minimum = minimum;
90          this.minimumSet = true;
91      }
92  
93      public void validate(FacesContext context,
94                           UIComponent  component,
95                           Object       value) throws ValidatorException {
96  
97          if ((context == null) || (component == null)) {
98              throw new NullPointerException();
99          }
100         if (value != null) {
101             try {
102                 int converted = intValue(value);
103                 if (maximumSet &&
104                     (converted > maximum)) {
105 		    if (minimumSet) {
106                         throw new ValidatorException(MessageFactory.getMessage
107 					   (context,
108 					    Validator.NOT_IN_RANGE_MESSAGE_ID,
109 					    new Object[] {
110 						new Integer(minimum),
111 						new Integer(maximum) }));
112 			
113 		    }
114 		    else {
115                         throw new ValidatorException(MessageFactory.getMessage
116 					   (context,
117 					    LongRangeValidator.MAXIMUM_MESSAGE_ID,
118 					    new Object[] {
119 						new Integer(maximum) }));
120 		    }
121                 }
122                 if (minimumSet &&
123                     (converted < minimum)) {
124 		    if (maximumSet) {
125                         throw new ValidatorException(MessageFactory.getMessage
126 					   (context,
127 					    Validator.NOT_IN_RANGE_MESSAGE_ID,
128 					    new Object[] {
129 						new Double(minimum),
130 						new Double(maximum) }));
131 			
132 		    }
133 		    else {
134                         throw new ValidatorException(MessageFactory.getMessage
135 					   (context,
136 					    LongRangeValidator.MINIMUM_MESSAGE_ID,
137 					    new Object[] {
138 						new Integer(minimum) }));
139 		    }
140                 }
141             } catch (NumberFormatException e) {
142                 throw new ValidatorException(MessageFactory.getMessage
143                                      (context, LongRangeValidator.TYPE_MESSAGE_ID));
144             }
145         }
146 
147     }
148 
149     private int intValue(Object attributeValue)
150         throws NumberFormatException {
151 
152         if (attributeValue instanceof Number) {
153             return ( ((Number) attributeValue).intValue() );
154         } else {
155             return (Integer.parseInt(attributeValue.toString()));
156         }
157 
158     }
159 
160 
161 
162 
163 }