1 16 package org.apache.myfaces.examples.common; 17 18 import org.apache.myfaces.examples.util.LocalizedSelectItem; 19 20 import javax.faces.application.FacesMessage; 21 import javax.faces.component.UIComponent; 22 import javax.faces.context.FacesContext; 23 import javax.faces.model.SelectItem; 24 import javax.faces.validator.ValidatorException; 25 import java.io.Serializable ; 26 import java.math.BigDecimal ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 31 35 public class CarConfigurator 36 implements Serializable 37 { 38 private static List _cars; 39 private static List _colors; 40 private static List _extrasList; 41 private static HashMap _priceList = new HashMap (); 42 private static HashMap _priceFactorColors = new HashMap (); 43 private static HashMap _priceListExtras = new HashMap (); 44 static 45 { 46 _cars = new ArrayList (); 47 _colors = new ArrayList (); 48 _extrasList = new ArrayList (); 49 50 _cars.add(new SelectItem("c1", "Audee X6", null)); 51 _cars.add(new SelectItem("c2", "PMW 321u", null)); 52 _cars.add(new SelectItem("c3", "Masta ZX7", null)); 53 _cars.add(new SelectItem("c4", "Renolt ESP", null)); 54 _cars.add(new SelectItem("c5", "WV Lumpo", null)); 55 _cars.add(new SelectItem("c6", "James Blond Car", null)); 56 _cars.add(new SelectItem("c7", "Neko Bus", null)); 57 58 _colors.add(new LocalizedSelectItem("color_black")); 59 _colors.add(new LocalizedSelectItem("color_blue")); 60 _colors.add(new LocalizedSelectItem("color_marine")); 61 _colors.add(new LocalizedSelectItem("color_red")); 62 63 _extrasList.add(new LocalizedSelectItem("extra_aircond")); 64 _extrasList.add(new LocalizedSelectItem("extra_sideab")); 65 _extrasList.add(new LocalizedSelectItem("extra_mirrowheat")); 66 _extrasList.add(new LocalizedSelectItem("extra_leaderseat")); 67 68 _priceList.put("c1", new BigDecimal (30000)); 69 _priceList.put("c2", new BigDecimal (32000)); 70 _priceList.put("c3", new BigDecimal (20000)); 71 _priceList.put("c4", new BigDecimal (25000)); 72 _priceList.put("c5", new BigDecimal (10000)); 73 _priceList.put("c6", new BigDecimal (100000000)); 74 _priceList.put("c7", new BigDecimal (1000000000)); 75 76 _priceFactorColors.put("color_black", new BigDecimal (1.15)); 77 _priceFactorColors.put("color_blue", new BigDecimal (1.10)); 78 _priceFactorColors.put("color_marine", new BigDecimal (1.05)); 79 _priceFactorColors.put("color_red", new BigDecimal (1.0)); 80 81 _priceListExtras.put("extra_aircond", new BigDecimal (510)); 82 _priceListExtras.put("extra_sideab", new BigDecimal (1220)); 83 _priceListExtras.put("extra_mirrowheat", new BigDecimal (1230)); 84 _priceListExtras.put("extra_leaderseat", new BigDecimal (840)); 85 } 86 87 88 private BigDecimal _price = new BigDecimal (0); 89 private String [] _extras; 90 private String _discount = "0"; 91 private String _discount2 = "0"; 92 private String _bandName; 93 private String _car; 94 private String _color = "color_blue"; 95 private boolean _salesTax = false; 96 private int _doors = 4; 97 98 public CarConfigurator() 99 { 100 } 101 102 103 public int getDoors() 104 { 105 return _doors; 106 } 107 108 109 public void setDoors(int doors) 110 { 111 this._doors = doors; 112 } 113 114 115 public List getCars() 116 { 117 return _cars; 118 } 119 120 public List getColors() 121 { 122 return _colors; 123 } 124 125 public List getExtrasList() 126 { 127 return _extrasList; 128 } 129 130 public String getCar() 131 { 132 return _car; 133 } 134 135 public void setCar(String car) 136 { 137 _car = car; 138 } 139 140 public String getColor() 141 { 142 return _color; 143 } 144 145 public void setColor(String color) 146 { 147 _color = color; 148 } 149 150 public BigDecimal getPrice() 151 { 152 return _price; 153 } 154 155 public void setPrice(BigDecimal price) 156 { 157 _price = price; 158 } 159 160 public String [] getExtras() 161 { 162 return _extras; 163 } 164 165 public void setExtras(String [] extras) 166 { 167 _extras = extras; 168 } 169 170 public String getDiscount() 171 { 172 return _discount; 173 } 174 175 public void setDiscount(String discount) 176 { 177 _discount = discount; 178 } 179 180 public String getDiscount2() 181 { 182 return _discount2; 183 } 184 185 public void setDiscount2(String discount2) 186 { 187 _discount2 = discount2; 188 } 189 190 public String getBandName() 191 { 192 return _bandName; 193 } 194 195 public void setBandName(String bandName) 196 { 197 _bandName = bandName; 198 } 199 200 public boolean isSalesTax() 201 { 202 return _salesTax; 203 } 204 205 public void setSalesTax(boolean salesTax) 206 { 207 _salesTax = salesTax; 208 } 209 210 public String calcPrice() 211 { 212 String car = getCar(); 213 String color = getColor(); 214 if (car == null || 215 color == null) 216 { 217 _price = new BigDecimal (0); 218 return "ok"; 219 } 220 221 BigDecimal carprice = (BigDecimal )_priceList.get(car); 222 BigDecimal colorfactor = (BigDecimal )_priceFactorColors.get(color); 223 if (carprice == null || 224 colorfactor == null) 225 { 226 _price = new BigDecimal (0); 227 return "ok"; 228 } 229 _price = carprice.multiply(colorfactor); 230 231 String [] extras = getExtras(); 232 if (extras != null) 233 { 234 for (int i = 0; i < extras.length; i++) 235 { 236 String extra = extras[i]; 237 _price = _price.add((BigDecimal )_priceListExtras.get(extra)); 238 } 239 } 240 241 if (_discount != null) 242 { 243 try 244 { 245 int i = Integer.parseInt(_discount); 246 switch (i) 247 { 248 case 0: break; 249 case 1: _price = _price.multiply(new BigDecimal (0.95)); break; 250 case 2: _price = _price.multiply(new BigDecimal (0.91)); break; 251 } 252 } 253 catch (NumberFormatException e) 254 { 255 } 256 } 257 258 if (_discount2 != null) 259 { 260 try 261 { 262 int i = Integer.parseInt(_discount2); 263 switch (i) 264 { 265 case 1: _price = _price.multiply(new BigDecimal (0.85)); break; 266 case 2: _price = _price.multiply(new BigDecimal (0.80)); break; 267 case 3: _price = _price.multiply(new BigDecimal (0.6)); break; 268 } 269 } 270 catch (NumberFormatException e) 271 { 272 } 273 } 274 275 if (_salesTax) 276 { 277 _price = _price.multiply(new BigDecimal (1.2)); 278 } 279 280 return "ok"; 281 } 282 283 284 public void validateCar(FacesContext context, UIComponent component, Object value) throws ValidatorException 285 { 286 if (value instanceof String && value.equals("c6")) 287 { 288 throw new ValidatorException(new FacesMessage("Are you kidding?", "You cannot buy a James Blond car!")); 289 } 290 } 291 292 293 } 294 | Popular Tags |