KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > common > CarConfigurator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * @author Thomas Spiegl (latest modification by $Author: matzew $)
33  * @version $Revision: 1.1 $ $Date: 2005/03/24 16:47:11 $
34  */

35 public class CarConfigurator
36         implements Serializable JavaDoc
37 {
38     private static List JavaDoc _cars;
39     private static List JavaDoc _colors;
40     private static List JavaDoc _extrasList;
41     private static HashMap JavaDoc _priceList = new HashMap JavaDoc();
42     private static HashMap JavaDoc _priceFactorColors = new HashMap JavaDoc();
43     private static HashMap JavaDoc _priceListExtras = new HashMap JavaDoc();
44     static
45     {
46         _cars = new ArrayList JavaDoc();
47         _colors = new ArrayList JavaDoc();
48         _extrasList = new ArrayList JavaDoc();
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 JavaDoc(30000));
69         _priceList.put("c2", new BigDecimal JavaDoc(32000));
70         _priceList.put("c3", new BigDecimal JavaDoc(20000));
71         _priceList.put("c4", new BigDecimal JavaDoc(25000));
72         _priceList.put("c5", new BigDecimal JavaDoc(10000));
73         _priceList.put("c6", new BigDecimal JavaDoc(100000000));
74         _priceList.put("c7", new BigDecimal JavaDoc(1000000000));
75
76         _priceFactorColors.put("color_black", new BigDecimal JavaDoc(1.15));
77         _priceFactorColors.put("color_blue", new BigDecimal JavaDoc(1.10));
78         _priceFactorColors.put("color_marine", new BigDecimal JavaDoc(1.05));
79         _priceFactorColors.put("color_red", new BigDecimal JavaDoc(1.0));
80
81         _priceListExtras.put("extra_aircond", new BigDecimal JavaDoc(510));
82         _priceListExtras.put("extra_sideab", new BigDecimal JavaDoc(1220));
83         _priceListExtras.put("extra_mirrowheat", new BigDecimal JavaDoc(1230));
84         _priceListExtras.put("extra_leaderseat", new BigDecimal JavaDoc(840));
85     }
86
87
88     private BigDecimal JavaDoc _price = new BigDecimal JavaDoc(0);
89     private String JavaDoc[] _extras;
90     private String JavaDoc _discount = "0";
91     private String JavaDoc _discount2 = "0";
92     private String JavaDoc _bandName;
93     private String JavaDoc _car;
94     private String JavaDoc _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 JavaDoc getCars()
116     {
117         return _cars;
118     }
119
120     public List JavaDoc getColors()
121     {
122         return _colors;
123     }
124
125     public List JavaDoc getExtrasList()
126     {
127         return _extrasList;
128     }
129
130     public String JavaDoc getCar()
131     {
132         return _car;
133     }
134
135     public void setCar(String JavaDoc car)
136     {
137         _car = car;
138     }
139
140     public String JavaDoc getColor()
141     {
142         return _color;
143     }
144
145     public void setColor(String JavaDoc color)
146     {
147         _color = color;
148     }
149
150     public BigDecimal JavaDoc getPrice()
151     {
152         return _price;
153     }
154
155     public void setPrice(BigDecimal JavaDoc price)
156     {
157         _price = price;
158     }
159
160     public String JavaDoc[] getExtras()
161     {
162         return _extras;
163     }
164
165     public void setExtras(String JavaDoc[] extras)
166     {
167         _extras = extras;
168     }
169
170     public String JavaDoc getDiscount()
171     {
172         return _discount;
173     }
174
175     public void setDiscount(String JavaDoc discount)
176     {
177         _discount = discount;
178     }
179
180     public String JavaDoc getDiscount2()
181     {
182         return _discount2;
183     }
184
185     public void setDiscount2(String JavaDoc discount2)
186     {
187         _discount2 = discount2;
188     }
189
190     public String JavaDoc getBandName()
191     {
192         return _bandName;
193     }
194
195     public void setBandName(String JavaDoc 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 JavaDoc calcPrice()
211     {
212         String JavaDoc car = getCar();
213         String JavaDoc color = getColor();
214         if (car == null ||
215             color == null)
216         {
217             _price = new BigDecimal JavaDoc(0);
218             return "ok";
219         }
220
221         BigDecimal JavaDoc carprice = (BigDecimal JavaDoc)_priceList.get(car);
222         BigDecimal JavaDoc colorfactor = (BigDecimal JavaDoc)_priceFactorColors.get(color);
223         if (carprice == null ||
224             colorfactor == null)
225         {
226             _price = new BigDecimal JavaDoc(0);
227             return "ok";
228         }
229         _price = carprice.multiply(colorfactor);
230
231         String JavaDoc[] extras = getExtras();
232         if (extras != null)
233         {
234             for (int i = 0; i < extras.length; i++)
235             {
236                 String JavaDoc extra = extras[i];
237                 _price = _price.add((BigDecimal JavaDoc)_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 JavaDoc(0.95)); break;
250                     case 2: _price = _price.multiply(new BigDecimal JavaDoc(0.91)); break;
251                 }
252             }
253             catch (NumberFormatException JavaDoc 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 JavaDoc(0.85)); break;
266                     case 2: _price = _price.multiply(new BigDecimal JavaDoc(0.80)); break;
267                     case 3: _price = _price.multiply(new BigDecimal JavaDoc(0.6)); break;
268                 }
269             }
270             catch (NumberFormatException JavaDoc e)
271             {
272             }
273         }
274
275         if (_salesTax)
276         {
277             _price = _price.multiply(new BigDecimal JavaDoc(1.2));
278         }
279
280         return "ok";
281     }
282
283
284     public void validateCar(FacesContext context, UIComponent component, Object JavaDoc value) throws ValidatorException
285     {
286         if (value instanceof String JavaDoc && 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