0
点赞
收藏
分享

微信扫一扫

JAVA实体类-自定义Getter Setter

### 案例一

  • 整个购物车存放的商品信息 需要计算的属性需要重写get方法,保证每次获取属性都会进行计算
private BigDecimal totalPrice;

/**
 * 计算当前购物项总价
 * @return
 */
public BigDecimal getTotalPrice() {
    // 等于单价 *数量
    return this.price.multiply(new BigDecimal("" + this.count));
}

public void setTotalPrice(BigDecimal totalPrice) {
    this.totalPrice = totalPrice;
}

///购物车实体类
/**
 * 购物车子项信息
 */
List<CartItem> items;

/**
 * 商品数量
 */
private Integer countNum;

/**
 * 商品类型数量
 */
private Integer countType;

/**
 * 商品总价
 */
private BigDecimal totalAmount;

/**
 * 减免价格
 */
private BigDecimal reduce = new BigDecimal("0.00"); //默认没有满减

public List<CartItem> getItems() {
    return items;
}

public void setItems(List<CartItem> items) {
    this.items = items;
}

public Integer getCountNum() {
    int count = 0;
    if (items != null && items.size() > 0) {
        for (CartItem item : items) {
            count += item.getCount();
        }
    }
    return count;
}

public Integer getCountType() {
    int count = 0;
    if (items != null && items.size() > 0) {
        for (CartItem item : items) {
            count += 1;
        }
    }
    return count;
}


public BigDecimal getTotalAmount() {
    BigDecimal amount = new BigDecimal("0");
    // 计算购物项总价
    if (!CollectionUtils.isEmpty(items)) {
        for (CartItemVo cartItem : items) {
            if (cartItem.getCheck()) {
                amount = amount.add(cartItem.getTotalPrice());
            }
        }
    }
    // 计算优惠后的价格
    return amount.subtract(getReduce());
}

public BigDecimal getReduce() {
    return reduce;
}

public void setReduce(BigDecimal reduce) {
    this.reduce = reduce;
}

### 案例二

public class OrderConfirmVo {
    @Getter
    @Setter
    /** 会员收获地址列表 **/
            List<MemberAddressVo> address;

    @Getter
    @Setter
    /** 所有选中的购物项 **/
            List<OrderItemVo> items;

    /** 发票记录 **/
    @Getter
    @Setter
    /** 优惠券(会员积分) **/
    private Integer integration;

    /** 防止重复提交的令牌 **/
    @Getter
    @Setter
    private String orderToken;

    @Getter
    @Setter
    Map<Long, Boolean> stocks;

    public Integer getCount() {
        Integer count = 0;
        if (items != null && items.size() > 0) {
            for (OrderItemVo item : items) {
                count += item.getCount();
            }
        }
        return count;
    }


    /** 订单总额 **/
    //BigDecimal total;
    //计算订单总额
    public BigDecimal getTotal() {
        BigDecimal totalNum = BigDecimal.ZERO;
        if (items != null && items.size() > 0) {
            for (OrderItemVo item : items) {
                //计算当前商品的总价格
                BigDecimal itemPrice = item.getPrice().multiply(new BigDecimal(item.getCount().toString()));
                //再计算全部商品的总价格
                totalNum = totalNum.add(itemPrice);
            }
        }
        return totalNum;
    }


    /** 应付价格 **/
    //BigDecimal payPrice;
    public BigDecimal getPayPrice() {
        return getTotal();
    }
}

举报

相关推荐

0 条评论