### 案例一
- 整个购物车存放的商品信息 需要计算的属性需要重写get方法,保证每次获取属性都会进行计算
private BigDecimal totalPrice;
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;
}
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;
}
public BigDecimal getPayPrice() {
return getTotal();
}
}