1 4 package com.openedit.store.shipping; 5 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.openedit.money.Money; 11 12 import com.openedit.store.Cart; 13 import com.openedit.store.CartItem; 14 15 16 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 remaining = new ArrayList ( inCart.getItems() ); 49 50 for (Iterator iter = inCart.getItemIterator(); iter.hasNext();) 54 { 55 CartItem cartI = (CartItem) iter.next(); 56 String method = cartI.getProduct().getShippingMethodId(); 57 if ( method != null ) 58 { 59 remaining.remove(cartI); 61 } 62 } 63 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 inRemaining) 78 { 79 double total = 0.0; 80 for (Iterator 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 |