1 32 33 package com.knowgate.hipergate; 34 35 import java.util.ArrayList ; 36 import java.util.Date ; 37 import java.util.Map ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.math.BigDecimal ; 41 42 import com.knowgate.misc.Gadgets; 43 51 public class ShoppingBasket { 52 53 private String sGuCustomer; 54 private ArrayList oLines; 55 private HashMap oProps; 56 private HashMap oLastLine; 57 private int iLastLine; 58 59 61 64 public ShoppingBasket() { 65 sGuCustomer = null; 66 oLines = new ArrayList (); 67 oProps = new HashMap (); 68 oLastLine = null; 69 iLastLine = -1; 70 } 71 72 74 78 public String getCustomer() { 79 return sGuCustomer; 80 } 81 82 84 89 public void setCustomer(String sIdCustomer) { 90 sGuCustomer = sIdCustomer; 91 } 92 93 95 100 public Object getProperty(String sKey) { 101 return oProps.get(sKey); 102 } 103 104 106 111 public boolean containsProperty(String sKey) { 112 return oProps.containsKey(sKey); 113 } 114 115 117 122 public void setProperty(String sKey, Object oProperty) { 123 if (oProps.containsKey(sKey)) oProps.remove(sKey); 124 oProps.put(sKey, oProperty); 125 } 126 127 129 133 public void setProperties (Map oPropertiesMap) { 134 Iterator oKeys = oPropertiesMap.keySet().iterator(); 135 String sKey; 136 while (oKeys.hasNext()) { 137 sKey = (String ) oKeys.next(); 138 setProperty(sKey, oPropertiesMap.get(sKey)); 139 } } 142 144 147 public void clearProperties () { 148 oProps.clear(); 149 } 150 151 153 157 public void removeProperty(String sKey) { 158 oProps.remove(sKey); 159 } 160 161 163 166 public void addLine() { 167 oLines.add(new HashMap ()); 168 } 169 170 172 177 public void addLine(Map oLine) { 178 oLines.add(oLine); 179 } 180 181 183 189 public void addLine(String sInputStr, String sDelimiter) { 190 oLines.add(new HashMap ()); 191 setLineAttributes(oLines.size()-1, sInputStr, sDelimiter); 192 } 193 194 196 200 public int getLineCount() { 201 return oLines.size(); 202 } 203 204 206 210 public void removeLine(int nLine) { 211 oLines.remove(nLine); 212 oLastLine=null; 213 iLastLine=-1; 214 } 215 216 218 223 public void clearLine(int nLine) throws ArrayIndexOutOfBoundsException { 224 ((HashMap ) oLines.get(nLine)).clear(); 225 } 226 227 229 235 public HashMap getLine(int nLine) throws ArrayIndexOutOfBoundsException { 236 return (HashMap ) oLines.get(nLine); 237 } 238 239 241 248 public Object getLineAttribute(int nLine, String sKey) 249 throws ArrayIndexOutOfBoundsException { 250 if (nLine!=iLastLine) { 251 iLastLine = nLine; 252 oLastLine = (HashMap ) oLines.get(nLine); 253 } 254 return oLastLine.get(sKey); 255 } 257 259 267 public String getLineString(int nLine, String sKey) 268 throws ArrayIndexOutOfBoundsException , ClassCastException { 269 if (nLine!=iLastLine) { 270 iLastLine = nLine; 271 oLastLine = (HashMap ) oLines.get(nLine); 272 } 273 return (String ) oLastLine.get(sKey); 274 } 276 278 286 public BigDecimal getLineBigDecimal(int nLine, String sKey) 287 throws ArrayIndexOutOfBoundsException , ClassCastException { 288 if (nLine!=iLastLine) { 289 iLastLine = nLine; 290 oLastLine = (HashMap ) oLines.get(nLine); 291 } 292 return (BigDecimal ) oLastLine.get(sKey); 293 } 295 297 305 public Date getLineDate(int nLine, String sKey) 306 throws ArrayIndexOutOfBoundsException , ClassCastException { 307 if (nLine!=iLastLine) { 308 iLastLine = nLine; 309 oLastLine = (HashMap ) oLines.get(nLine); 310 } 311 return (Date ) oLastLine.get(sKey); 312 } 314 316 324 public Integer getLineInteger(int nLine, String sKey) 325 throws ArrayIndexOutOfBoundsException , ClassCastException { 326 if (nLine!=iLastLine) { 327 iLastLine = nLine; 328 oLastLine = (HashMap ) oLines.get(nLine); 329 } 330 return (Integer ) oLastLine.get(sKey); 331 } 333 335 342 public void setLineAttribute(int nLine, String sKey, Object oAttr) 343 throws ArrayIndexOutOfBoundsException { 344 if (nLine!=iLastLine) { 345 iLastLine = nLine; 346 oLastLine = (HashMap ) oLines.get(nLine); 347 } 348 if (oLastLine.containsKey(sKey)) oLastLine.remove(sKey); 349 oLastLine.put(sKey, oAttr); 350 } 352 354 public void setLineAttributes(int nLine, String sInputStr, String sDelimiter) 355 throws ArrayIndexOutOfBoundsException { 356 String [] aAttrs; 357 String [] aAtrVl; 358 String sKey; 359 String sVal; 360 if (nLine!=iLastLine) { 361 iLastLine = nLine; 362 oLastLine = (HashMap ) 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 } } } } } 380 382 390 public int findLine (String sAttrKey, Object oAttrValue) { 391 final int nLines = oLines.size(); 392 HashMap oCurrentLine; 393 int l; 394 int iRetVal = -1; 395 if (null==oAttrValue) { 396 for (l=0; l<nLines && -1==iRetVal; l++) { 397 oCurrentLine = (HashMap ) oLines.get(l); 398 if (oCurrentLine.containsKey(sAttrKey)) 399 if (oCurrentLine.get(sAttrKey)==null) 400 iRetVal = l; 401 } } else { 403 for (l=0; l<nLines && -1==iRetVal; l++) { 404 oCurrentLine = (HashMap ) oLines.get(l); 405 if (oAttrValue.equals(oCurrentLine.get(sAttrKey))) 406 iRetVal = l; 407 } } return iRetVal; 410 } 412 414 421 public BigDecimal sum(String sAttrKey) 422 throws ClassCastException { 423 final int nLines = oLines.size(); 424 BigDecimal oSum = new BigDecimal (0d); 425 HashMap oCurrentLine; 426 Object oVal; 427 for (int l=0; l<nLines; l++) { 428 oCurrentLine = (HashMap ) oLines.get(l); 429 oVal = oCurrentLine.get(sAttrKey); 430 if (null!=oVal) 431 oSum = oSum.add((BigDecimal ) oVal); 432 } return oSum; 434 } 435 436 438 } 439 | Popular Tags |