KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > shipping > WeightBasedShippingMethod


1 /*
2  * Created on May 26, 2004
3  */

4 package com.openedit.store.shipping;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.openedit.money.Money;
11
12 import com.openedit.store.Cart;
13 import com.openedit.store.CartItem;
14
15
16 /**
17  * @author cburkey
18  *
19  */

20 public class WeightBasedShippingMethod extends BaseShippingMethod
21 {
22     public Money getCost(Cart inCart)
23     {
24         Money totalPrice = getHandlingCharge(inCart);
25             
26         if ( getPercentageCost() != 0.0 )
27         {
28             double weight = getWeight(inCart.getItems());
29             if ( weight != 0.0)
30             {
31                 double shippingAsPercent = weight * getPercentageCost();
32                 totalPrice.add(new Money(shippingAsPercent));
33             }
34             else
35             {
36                 totalPrice.add(getCost());
37             }
38             totalPrice = totalPrice.round();
39         }
40         else
41         {
42             totalPrice = totalPrice.add(getCost());
43         }
44         return totalPrice;
45     }
46     public boolean applies(Cart inCart)
47     {
48         List JavaDoc remaining = new ArrayList JavaDoc( inCart.getItems() );
49         
50         //If any products have a fixed shipping method
51
//Then we must include that in the list
52
//and remove it from the pricing totals remaining
53
for (Iterator JavaDoc iter = inCart.getItemIterator(); iter.hasNext();)
54         {
55             CartItem cartI = (CartItem) iter.next();
56             String JavaDoc method = cartI.getProduct().getShippingMethodId();
57             if ( method != null )
58             {
59                 //this has its own method and should be removed from the list
60
remaining.remove(cartI);
61             }
62         }
63         //check the weight
64
double weight = getWeight(remaining);
65         if ( getLowerThreshold() == null || Money.ZERO.equals(getLowerThreshold()) ||
66             getLowerThreshold().doubleValue() <= weight)
67         {
68             if (getUpperThreshold() == null ||
69                 getUpperThreshold().doubleValue() >= weight)
70             {
71
72                 return true;
73             }
74         }
75         return false;
76     }
77     protected double getWeight(List JavaDoc inRemaining)
78     {
79         double total = 0.0;
80         for (Iterator JavaDoc iter = inRemaining.iterator(); iter.hasNext();)
81         {
82             CartItem item = (CartItem) iter.next();
83             if( item.getProduct().getShippingMethodId() == null)
84             {
85                 double w = item.getWeight() * item.getQuantity();
86                 total = total + w;
87             }
88         }
89         return total;
90     }
91 }
92
Popular Tags