01: /*
02: * Created on May 26, 2004
03: */
04: package org.openedit.store.shipping;
05:
06: import java.util.ArrayList;
07: import java.util.Iterator;
08: import java.util.List;
09:
10: import org.openedit.money.Money;
11: import org.openedit.store.Cart;
12: import org.openedit.store.CartItem;
13:
14: /**
15: * @author cburkey
16: *
17: */
18: public class WeightBasedShippingMethod extends BaseShippingMethod {
19: public Money getCost(Cart inCart) {
20: Money totalPrice = getHandlingCharge(inCart);
21: totalPrice = totalPrice.add(getCost());
22:
23: if (getPercentageCost() != 0.0) {
24: double weight = getWeight(inCart.getItems());
25: if (weight != 0.0) {
26: double shippingAsPercent = weight * getPercentageCost();
27: totalPrice = totalPrice
28: .add(new Money(shippingAsPercent));
29: }
30: }
31: totalPrice = totalPrice.round();
32: return totalPrice;
33: }
34:
35: public boolean applies(Cart inCart) {
36: List remaining = new ArrayList(inCart.getItems());
37:
38: //If any products have a fixed shipping method
39: //Then we must include that in the list
40: //and remove it from the pricing totals remaining
41: for (Iterator iter = inCart.getItemIterator(); iter.hasNext();) {
42: CartItem cartI = (CartItem) iter.next();
43: String method = cartI.getProduct().getShippingMethodId();
44: if (method != null) {
45: //this has its own method and should be removed from the list
46: remaining.remove(cartI);
47: }
48: }
49: //check the weight
50: double weight = getWeight(remaining);
51: if (getLowerThreshold() == null
52: || Money.ZERO.equals(getLowerThreshold())
53: || getLowerThreshold().doubleValue() <= weight) {
54: if (getUpperThreshold() == null
55: || getUpperThreshold().doubleValue() >= weight) {
56:
57: return true;
58: }
59: }
60: return false;
61: }
62:
63: protected double getWeight(List inRemaining) {
64: double total = 0.0;
65: for (Iterator iter = inRemaining.iterator(); iter.hasNext();) {
66: CartItem item = (CartItem) iter.next();
67: if (item.getProduct().getShippingMethodId() == null) {
68: double w = item.getWeight() * item.getQuantity();
69: total = total + w;
70: }
71: }
72: return total;
73: }
74: }
|