Example #1
0
  /**
   * Get the measure according to the appropriate measure base. If the measure configured in store
   * is LB and it needs KG then the appropriate calculation is done
   *
   * @param weight
   * @param store
   * @param base
   * @return
   */
  public static double getWeight(double weight, MerchantStore store, String base) {

    double weightConstant = 2.2;
    if (base.equals(MeasureUnit.LB.name())) {
      if (store.getWeightunitcode().equals(MeasureUnit.LB.name())) {
        return new BigDecimal(String.valueOf(weight))
            .setScale(2, BigDecimal.ROUND_HALF_UP)
            .doubleValue();
      } else { // pound = kilogram
        double answer = weight * weightConstant;
        BigDecimal w = new BigDecimal(answer);
        return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      }
    } else { // need KG
      if (store.getWeightunitcode().equals(MeasureUnit.KG.name())) {
        return new BigDecimal(String.valueOf(weight))
            .setScale(2, BigDecimal.ROUND_HALF_UP)
            .doubleValue();
      } else {

        double answer = weight / weightConstant;
        BigDecimal w = new BigDecimal(answer);
        return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      }
    }
  }