List of usage examples for com.liferay.portal.kernel.util MathUtil isOdd
public static boolean isOdd(int x)
From source file:com.liferay.portlet.shopping.util.ShoppingUtil.java
License:Open Source License
public static double calculateInsurance(Map<ShoppingCartItem, Integer> items) throws PortalException, SystemException { double insurance = 0.0; double subtotal = 0.0; ShoppingPreferences preferences = null; Iterator<Map.Entry<ShoppingCartItem, Integer>> itr = items.entrySet().iterator(); while (itr.hasNext()) { Map.Entry<ShoppingCartItem, Integer> entry = itr.next(); ShoppingCartItem cartItem = entry.getKey(); Integer count = entry.getValue(); ShoppingItem item = cartItem.getItem(); if (preferences == null) { ShoppingCategory category = item.getCategory(); preferences = ShoppingPreferences.getInstance(category.getCompanyId(), category.getGroupId()); }/*w w w . j a v a 2 s . co m*/ ShoppingItemPrice itemPrice = _getItemPrice(item, count.intValue()); subtotal += calculateActualPrice(itemPrice) * count.intValue(); } if ((preferences != null) && (subtotal > 0)) { double insuranceRate = 0.0; double[] range = ShoppingPreferences.INSURANCE_RANGE; for (int i = 0; i < range.length - 1; i++) { if (subtotal > range[i] && subtotal <= range[i + 1]) { int rangeId = i / 2; if (MathUtil.isOdd(i)) { rangeId = (i + 1) / 2; } insuranceRate = GetterUtil.getDouble(preferences.getInsurance()[rangeId]); } } String formula = preferences.getInsuranceFormula(); if (formula.equals("flat")) { insurance += insuranceRate; } else if (formula.equals("percentage")) { insurance += subtotal * insuranceRate; } } return insurance; }
From source file:com.liferay.portlet.shopping.util.ShoppingUtil.java
License:Open Source License
public static double calculateShipping(Map<ShoppingCartItem, Integer> items) throws PortalException, SystemException { double shipping = 0.0; double subtotal = 0.0; ShoppingPreferences preferences = null; Iterator<Map.Entry<ShoppingCartItem, Integer>> itr = items.entrySet().iterator(); while (itr.hasNext()) { Map.Entry<ShoppingCartItem, Integer> entry = itr.next(); ShoppingCartItem cartItem = entry.getKey(); Integer count = entry.getValue(); ShoppingItem item = cartItem.getItem(); if (preferences == null) { ShoppingCategory category = item.getCategory(); preferences = ShoppingPreferences.getInstance(category.getCompanyId(), category.getGroupId()); }// w w w.j av a 2 s . c o m if (item.isRequiresShipping()) { ShoppingItemPrice itemPrice = _getItemPrice(item, count.intValue()); if (itemPrice.isUseShippingFormula()) { subtotal += calculateActualPrice(itemPrice) * count.intValue(); } else { shipping += itemPrice.getShipping() * count.intValue(); } } } if ((preferences != null) && (subtotal > 0)) { double shippingRate = 0.0; double[] range = ShoppingPreferences.SHIPPING_RANGE; for (int i = 0; i < range.length - 1; i++) { if (subtotal > range[i] && subtotal <= range[i + 1]) { int rangeId = i / 2; if (MathUtil.isOdd(i)) { rangeId = (i + 1) / 2; } shippingRate = GetterUtil.getDouble(preferences.getShipping()[rangeId]); } } String formula = preferences.getShippingFormula(); if (formula.equals("flat")) { shipping += shippingRate; } else if (formula.equals("percentage")) { shipping += subtotal * shippingRate; } } return shipping; }