View Javadoc

1   package org.paneris.jammyjoes.shopping;
2   
3   import org.melati.Melati;
4   import org.paneris.jammyjoes.model.JammyjoesDatabase;
5   import org.paneris.jammyjoes.model.Product;
6   import org.paneris.jammyjoes.model.ShopOrder;
7   import org.paneris.jammyjoes.model.ShopOrderItem;
8   
9   public class JammyJoesShoppingTrolleyItem extends ShoppingTrolleyItem {
10  
11    private Product product;
12    private JammyJoesShoppingTrolley jammyjoesShoppingTrolley;
13  
14    public void initialise(
15      ShoppingTrolley trolley,
16      Melati melati,
17      Integer id,
18      String description,
19      Double price) {
20      super.initialise(trolley, melati, id, description, price);
21      jammyjoesShoppingTrolley = (JammyJoesShoppingTrolley) trolley;
22    }
23  
24    /* load in information about this product given an id.  
25       perhaps this id represents a poem troid?
26    */
27    protected void load(Integer id) {
28      JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase();
29      product = db.getProductTable().getProductObject(id);
30      description = product.getName();
31      price = product.getRetailpriceincvat().doubleValue();
32    }
33  
34    protected void save(JammyjoesDatabase db, ShopOrder order) {
35      ShopOrderItem item = (ShopOrderItem) db.getShopOrderItemTable().newPersistent();
36      item.setUser(order.getUser());
37      item.setOrder(order);
38      item.setProduct(product);
39      item.setQuantity(getQuantity());
40      item.setAmount(jammyjoesShoppingTrolley.convertFromUK(price * quantity.intValue()));
41      item.setAmountUK(price * quantity.intValue());
42      db.getShopOrderItemTable().create(item);
43    }
44  
45    /* work out the cost of delivery
46    */
47    public double getDeliveryValue() {
48      return 0;
49    }
50  
51    public String getWeightDisplay() {
52      double weight = getMass();
53      if (weight < 1000)
54        return weight + "g";
55      return weight / 1000 + "Kg";
56    }
57  
58    public double getWeight() {
59      return getMass() * getQuantity().intValue();
60    }
61  
62    public double getMass() {
63      double weight = 0;
64      if (product.getWeight() != null)
65        weight = product.getWeight().doubleValue();
66      return weight;
67    }
68  
69    public Product getProduct() {
70      return product;
71    }
72  
73    public Integer getStocklevel() {
74      Integer level = getProduct().getStocklevel();
75      if (level == null) {
76        return new Integer(0);
77      } else {
78        return getProduct().getStocklevel();
79      }
80    }
81  
82    public boolean enoughStock() {
83      if (quantity.intValue() > getStocklevel().intValue())
84        return false;
85      return true;
86    }
87  
88  }