1 package org.apache.tapestry.pets.domain.model.pojo; 2 3 import java.io.Serializable ; 4 import java.math.BigDecimal ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 9 import johnmammen.betterpetshop.service.PetshopManager; 10 import johnmammen.betterpetshop.service.spring.PetshopManagerImpl; 11 12 import org.apache.tapestry.pets.domain.model.IBasketItem; 13 import org.apache.tapestry.pets.domain.model.IInventory; 14 import org.apache.tapestry.pets.domain.model.IShoppingCart; 15 16 21 public class ShoppingCart implements IShoppingCart, Serializable { 22 23 private ArrayList alShoppingCart = new ArrayList (); 24 25 28 public int getCount() { 29 return alShoppingCart.size(); 30 } 31 32 35 public double getTotal() { 36 if (alShoppingCart.size() == 0) { return 0.0; } 37 38 double total = 0.0; 39 40 for (Iterator it = alShoppingCart.iterator(); it.hasNext();) { 41 BasketItem bitem = (BasketItem) it.next(); 42 total += (bitem.getPrice().multiply(BigDecimal.valueOf(bitem 43 .getQty()))).doubleValue(); 44 } 45 46 return total; 47 } 48 49 54 public Collection getItems() { 55 return alShoppingCart; 56 } 57 58 61 public IBasketItem getBasketItem(int index) { 62 return (IBasketItem) alShoppingCart.get(index); 63 } 64 65 68 public void clearCart() { 69 alShoppingCart.clear(); 70 } 71 72 78 public void add(IBasketItem value) { 79 for (Iterator it = alShoppingCart.iterator(); it.hasNext();) { 81 IBasketItem bitem = (IBasketItem) it.next(); 82 83 if (bitem.getItemID().equals(value.getItemID())) { 84 bitem.setQty(bitem.getQty() + 1); 87 88 return; 89 } 90 } 91 92 alShoppingCart.add(value); 94 } 95 96 102 public void removeAt(int index) { 103 alShoppingCart.remove(index); 104 } 105 106 112 public void remove(String itemID) { 113 for (Iterator it = alShoppingCart.iterator(); it.hasNext();) { 115 IBasketItem bitem = (IBasketItem) it.next(); 116 117 if (bitem.getItemID().equals(itemID)) { 118 alShoppingCart.remove(bitem); 120 121 return; 122 } 123 } 124 } 125 126 130 public void updateInStock() { 131 IBasketItem item; 133 IInventory inventory; 134 135 PetshopManager petManager = new PetshopManagerImpl(); 136 137 IInventory[] arrInventory = petManager.getInventory("csv"); 138 for (int i = 0; i < arrInventory.length; i++) { 139 inventory = (IInventory) arrInventory[i]; 140 141 for (Iterator it = alShoppingCart.iterator(); it.hasNext();) { 142 item = (IBasketItem) it.next(); 143 144 if (item.getItemID().equals(inventory.getItemID())) { 145 item 146 .setInStock(((inventory.getQty() - item.getQty()) >= 0) ? true 147 : false); 148 } 149 } 150 } 151 } 152 153 private String getBasketItemsCSVList() { 154 int k = 0; 155 StringBuffer csv = new StringBuffer (); 156 157 for (Iterator it = alShoppingCart.iterator(); it.hasNext();) { 158 IBasketItem bitem = (IBasketItem) it.next(); 159 160 if (k == 0) { 161 csv.append("\'" + bitem.getItemID().trim() + "\'"); 162 } else { 163 csv.append(", \'" + bitem.getItemID().trim() + "\'"); 164 } 165 166 k++; 167 } 168 169 return csv.toString(); 170 } 171 } | Popular Tags |