View Javadoc

    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
    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 }