KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > ShoppingBasket


1 /*
2   Copyright (C) 2003-2005 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.math.BigDecimal JavaDoc;
41
42 import com.knowgate.misc.Gadgets;
43 /**
44  * <p>Shopping basket</p>
45  * This class is specially designed for usage as a shopping basket for a website<br>
46  * Shopping baskets have 3 main elements:<br>
47  * <ul><li>Customer identification</li><li>Global properties</li><li>Lines</li></ul>
48  * @author Sergio Montoro Ten
49  * @version 3.0
50  */

51 public class ShoppingBasket {
52
53   private String JavaDoc sGuCustomer;
54   private ArrayList JavaDoc oLines;
55   private HashMap JavaDoc oProps;
56   private HashMap JavaDoc oLastLine;
57   private int iLastLine;
58
59   // ---------------------------------------------------------------------------
60

61   /**
62    * Default constructor
63    */

64   public ShoppingBasket() {
65     sGuCustomer = null;
66     oLines = new ArrayList JavaDoc();
67     oProps = new HashMap JavaDoc();
68     oLastLine = null;
69     iLastLine = -1;
70   }
71
72   // ---------------------------------------------------------------------------
73

74   /**
75    * Get customer identification
76    * @return String uniquely identifying the customer owner of this shopping basket
77    */

78   public String JavaDoc getCustomer() {
79     return sGuCustomer;
80   }
81
82   // ---------------------------------------------------------------------------
83

84   /**
85    * <p>Set customer identification</p>
86    * Customer Id. can be any arbitrary string that uniquely identifies current customer
87    * @param sIdCustomer String uniquely identifying the customer owner of this shopping basket
88    */

89   public void setCustomer(String JavaDoc sIdCustomer) {
90     sGuCustomer = sIdCustomer;
91   }
92
93   // ---------------------------------------------------------------------------
94

95   /**
96    * Get a global property of this basket
97    * @param sKey String property name
98    * @return Object or <b>null</b> if there is no property with such name
99    */

100   public Object JavaDoc getProperty(String JavaDoc sKey) {
101     return oProps.get(sKey);
102   }
103
104   // ---------------------------------------------------------------------------
105

106   /**
107    * Whether or not this shopping basket contains a given global property
108    * @param sKey String property name
109    * @return boolean
110    */

111   public boolean containsProperty(String JavaDoc sKey) {
112     return oProps.containsKey(sKey);
113   }
114
115   // ---------------------------------------------------------------------------
116

117   /**
118    * Set global property for this basket
119    * @param sKey String property name
120    * @param oProperty Object
121    */

122   public void setProperty(String JavaDoc sKey, Object JavaDoc oProperty) {
123     if (oProps.containsKey(sKey)) oProps.remove(sKey);
124     oProps.put(sKey, oProperty);
125   }
126
127   // ---------------------------------------------------------------------------
128

129   /**
130    * Set global properties for this basket
131    * @param oPropertiesMap Map containing property names as map keys and property values as map values
132    */

133   public void setProperties (Map JavaDoc oPropertiesMap) {
134     Iterator JavaDoc oKeys = oPropertiesMap.keySet().iterator();
135     String JavaDoc sKey;
136     while (oKeys.hasNext()) {
137       sKey = (String JavaDoc) oKeys.next();
138       setProperty(sKey, oPropertiesMap.get(sKey));
139     } // wend
140
} // setProperties
141

142   // ---------------------------------------------------------------------------
143

144   /**
145    * Clear all global properties
146    */

147   public void clearProperties () {
148     oProps.clear();
149   }
150
151   // ---------------------------------------------------------------------------
152

153   /**
154    * Remove a single global property
155    * @param sKey String property name
156    */

157   public void removeProperty(String JavaDoc sKey) {
158     oProps.remove(sKey);
159   }
160
161   // ---------------------------------------------------------------------------
162

163   /**
164    * Add empty order line to this basket
165    */

166   public void addLine() {
167     oLines.add(new HashMap JavaDoc());
168   }
169
170   // ---------------------------------------------------------------------------
171

172   /**
173    * <p>Add order line to this basket</p>
174    * Each line has an arbitrary number of named attributes given in the Map passed as parameter
175    * @param oLine Map with line attributes
176    */

177   public void addLine(Map JavaDoc oLine) {
178     oLines.add(oLine);
179   }
180
181   // ---------------------------------------------------------------------------
182

183   /**
184    * <p>Add order line to this basket</p>
185    * This method adds a line from a string of the form: "attribute1=value1,attribute2=value2,attribute3=value3"
186    * @param sInputStr String
187    * @param sDelimiter String Delimiter to be used between attribute, in the examle above, a comma
188    */

189   public void addLine(String JavaDoc sInputStr, String JavaDoc sDelimiter) {
190     oLines.add(new HashMap JavaDoc());
191     setLineAttributes(oLines.size()-1, sInputStr, sDelimiter);
192   }
193
194   // ---------------------------------------------------------------------------
195

196   /**
197    * Count of lines on this basket
198    * @return int
199    */

200   public int getLineCount() {
201     return oLines.size();
202   }
203
204   // ---------------------------------------------------------------------------
205

206   /**
207    * Remove order line from this basket
208    * @param nLine int Line number [0..getLineCount()-1)]
209    */

210   public void removeLine(int nLine) {
211     oLines.remove(nLine);
212     oLastLine=null;
213     iLastLine=-1;
214   }
215
216   // ---------------------------------------------------------------------------
217

218   /**
219    * Clear all attributes for a line
220    * @param nLine int Line number [0..getLineCount()-1)]
221    * @throws ArrayIndexOutOfBoundsException
222    */

223   public void clearLine(int nLine) throws ArrayIndexOutOfBoundsException JavaDoc {
224     ((HashMap JavaDoc) oLines.get(nLine)).clear();
225   }
226
227   // ---------------------------------------------------------------------------
228

229   /**
230    * Get order line
231    * @param nLine int Line number [0..getLineCount()-1)]
232    * @return HashMap Line attributes
233    * @throws ArrayIndexOutOfBoundsException
234    */

235   public HashMap JavaDoc getLine(int nLine) throws ArrayIndexOutOfBoundsException JavaDoc {
236     return (HashMap JavaDoc) oLines.get(nLine);
237   }
238
239   // ---------------------------------------------------------------------------
240

241   /**
242    * Get attribute of a given line
243    * @param nLine int Line number [0..getLineCount()-1)]
244    * @param sKey String attribute name
245    * @return Object
246    * @throws ArrayIndexOutOfBoundsException
247    */

248   public Object JavaDoc getLineAttribute(int nLine, String JavaDoc sKey)
249     throws ArrayIndexOutOfBoundsException JavaDoc {
250     if (nLine!=iLastLine) {
251       iLastLine = nLine;
252       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
253     }
254     return oLastLine.get(sKey);
255   } // getLineAttribute
256

257   // ---------------------------------------------------------------------------
258

259   /**
260    * Get attribute of a given line casted to String
261    * @param nLine int Line number [0..getLineCount()-1)]
262    * @param sKey String attribute name
263    * @return String
264    * @throws ArrayIndexOutOfBoundsException
265    * @throws ClassCastException
266    */

267   public String JavaDoc getLineString(int nLine, String JavaDoc sKey)
268     throws ArrayIndexOutOfBoundsException JavaDoc, ClassCastException JavaDoc {
269     if (nLine!=iLastLine) {
270       iLastLine = nLine;
271       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
272     }
273     return (String JavaDoc) oLastLine.get(sKey);
274   } // getLineString
275

276   // ---------------------------------------------------------------------------
277

278   /**
279    * Get attribute of a given line casted to java.math.BigDecimal
280    * @param nLine int Line number [0..getLineCount()-1)]
281    * @param sKey String attribute name
282    * @return BigDecimal
283    * @throws ArrayIndexOutOfBoundsException
284    * @throws ClassCastException
285    */

286   public BigDecimal JavaDoc getLineBigDecimal(int nLine, String JavaDoc sKey)
287     throws ArrayIndexOutOfBoundsException JavaDoc, ClassCastException JavaDoc {
288     if (nLine!=iLastLine) {
289       iLastLine = nLine;
290       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
291     }
292     return (BigDecimal JavaDoc) oLastLine.get(sKey);
293   } // getLineBigDecimal
294

295   // ---------------------------------------------------------------------------
296

297   /**
298    * Get attribute of a given line casted to java.util.Date
299    * @param nLine int Line number [0..getLineCount()-1)]
300    * @param sKey String attribute name
301    * @return Date
302    * @throws ArrayIndexOutOfBoundsException
303    * @throws ClassCastException
304    */

305   public Date JavaDoc getLineDate(int nLine, String JavaDoc sKey)
306     throws ArrayIndexOutOfBoundsException JavaDoc, ClassCastException JavaDoc {
307     if (nLine!=iLastLine) {
308       iLastLine = nLine;
309       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
310     }
311     return (Date JavaDoc) oLastLine.get(sKey);
312   } // getLineDate
313

314   // ---------------------------------------------------------------------------
315

316   /**
317    * Get attribute of a given line casted to java.lang.Integer
318    * @param nLine int Line number [0..getLineCount()-1)]
319    * @param sKey String attribute name
320    * @return Date
321    * @throws ArrayIndexOutOfBoundsException
322    * @throws ClassCastException
323    */

324   public Integer JavaDoc getLineInteger(int nLine, String JavaDoc sKey)
325     throws ArrayIndexOutOfBoundsException JavaDoc, ClassCastException JavaDoc {
326     if (nLine!=iLastLine) {
327       iLastLine = nLine;
328       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
329     }
330     return (Integer JavaDoc) oLastLine.get(sKey);
331   } // getLineInteger
332

333   // ---------------------------------------------------------------------------
334

335   /**
336    * Set attribute for a given line
337    * @param nLine int Line number [0..getLineCount()-1)]
338    * @param sKey String attribute name
339    * @param oAttr Object attribute value
340    * @throws ArrayIndexOutOfBoundsException
341    */

342   public void setLineAttribute(int nLine, String JavaDoc sKey, Object JavaDoc oAttr)
343     throws ArrayIndexOutOfBoundsException JavaDoc {
344     if (nLine!=iLastLine) {
345       iLastLine = nLine;
346       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
347     }
348     if (oLastLine.containsKey(sKey)) oLastLine.remove(sKey);
349     oLastLine.put(sKey, oAttr);
350   } // setLineAttribute
351

352   // ---------------------------------------------------------------------------
353

354   public void setLineAttributes(int nLine, String JavaDoc sInputStr, String JavaDoc sDelimiter)
355     throws ArrayIndexOutOfBoundsException JavaDoc {
356     String JavaDoc[] aAttrs;
357     String JavaDoc[] aAtrVl;
358     String JavaDoc sKey;
359     String JavaDoc sVal;
360     if (nLine!=iLastLine) {
361       iLastLine = nLine;
362       oLastLine = (HashMap JavaDoc) oLines.get(nLine);
363     }
364     if (sInputStr!=null) {
365       if (sInputStr.length()>0) {
366         aAttrs = Gadgets.split(sInputStr, sDelimiter);
367         for (int a=aAttrs.length-1; a>=0; a--) {
368           if (aAttrs[a].length()>0) {
369             aAtrVl = Gadgets.split(aAttrs[a], '=');
370             sKey = aAtrVl[0].trim();
371             sVal = aAtrVl[1].trim();
372             if (oLastLine.containsKey(sKey)) oLastLine.remove(sKey);
373             oLastLine.put(sKey, sVal);
374           } // fi (aAttrs[a].length()>0)
375
} // next
376
} // fi
377
} // fi
378
} // setLineAttributes
379

380   // ---------------------------------------------------------------------------
381

382   /**
383    * Get the first line that contains an attribute with a given value
384    * @param sKey String Attribute key
385    * @param oAttr Object Attribute value
386    * @return int Number [0..getLineCount()-1)] of the first line that contains
387    * an attribute with the given name and value or -1 if no line attribute matches
388    * the given one.
389    */

390   public int findLine (String JavaDoc sAttrKey, Object JavaDoc oAttrValue) {
391     final int nLines = oLines.size();
392     HashMap JavaDoc oCurrentLine;
393     int l;
394     int iRetVal = -1;
395     if (null==oAttrValue) {
396       for (l=0; l<nLines && -1==iRetVal; l++) {
397         oCurrentLine = (HashMap JavaDoc) oLines.get(l);
398         if (oCurrentLine.containsKey(sAttrKey))
399           if (oCurrentLine.get(sAttrKey)==null)
400             iRetVal = l;
401       } // next
402
} else {
403       for (l=0; l<nLines && -1==iRetVal; l++) {
404         oCurrentLine = (HashMap JavaDoc) oLines.get(l);
405         if (oAttrValue.equals(oCurrentLine.get(sAttrKey)))
406           iRetVal = l;
407       } // next
408
} // fi (oAttr==null)
409
return iRetVal;
410   } // findLine
411

412   // ---------------------------------------------------------------------------
413

414   /**
415    * <p>Get sum of all line attributes of a given name</p>
416    * @param sAttrKey String Atrributes common name
417    * @return BigDecimal Sum of values or zero if no attributes where found
418    * @throws ClassCastException Attribute values must be of type BigDecimal
419    * or else a ClassCastException is raised
420    */

421   public BigDecimal JavaDoc sum(String JavaDoc sAttrKey)
422     throws ClassCastException JavaDoc {
423     final int nLines = oLines.size();
424     BigDecimal JavaDoc oSum = new BigDecimal JavaDoc(0d);
425     HashMap JavaDoc oCurrentLine;
426     Object JavaDoc oVal;
427     for (int l=0; l<nLines; l++) {
428       oCurrentLine = (HashMap JavaDoc) oLines.get(l);
429       oVal = oCurrentLine.get(sAttrKey);
430       if (null!=oVal)
431         oSum = oSum.add((BigDecimal JavaDoc) oVal);
432     } // next
433
return oSum;
434   }
435
436   // ---------------------------------------------------------------------------
437

438 }
439
Popular Tags