前言因为每个人的方法不一样,没有固定写法,比如说在真正的项目时,积分肯定有它自己的规则
也要单独的抽取出来,甚至规则里面的东西还要抽取,分力度的
因本人技术较菜,下面只是一个最简单的购物车案例
1.抽取模型
首先要做的购物车呢长这样- -
首先每一项是单独的一个购物项
购物项中有价格 数量 和加起来的价格
购物车里又有购物项
这样就有了两个模型
1.购物车 2.购物项
购物项的抽取
`private int count;
private Product product;
private double price;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public double getPrice() {
return product.getShop_price()*count;//这样写前端用el可以直接获取,因为${gwx.price}就是在执行getprice方法
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Gwx [count=" + count + ", product=" + product + ", price=" + getPrice() + "]";
}
`
购物车的抽取
private Map<Integer,Gwx>map=new HashMap<>();//也可以用list集合,无非是遍历的时候判断传入的购物项里面的pid是不是一样
private double allprice;
public Map<Integer, Gwx> getMap() {
return map;
}
public void setMap(Map<Integer, Gwx> map) {
this.map = map;
}
public double getAllprice() {//遍历获取价格
double a=0;
Collection<Gwx> values = map.values();
for(Gwx g : values) {
a+=g.getPrice();
}
return a;
}
public void setAllprice(double allprice) {
this.allprice = allprice;
}
public void addGwx(Gwx gwx) {//如果里面已经有了这样的商品那么就不再添加购物项了而是改一下数量
if(map.containsKey(gwx.getProduct().getPid())) {//如果有
Gwx x = map.get(gwx.getProduct().getPid());//获取出来
x.setCount(x.getCount()+gwx.getCount());//设置数量加上去
}else {
map.put(gwx.getProduct().getPid(), gwx);
}
}
public void clear() {
map.clear();
}
public Collection getValues() {
return map.values();
}
@Override
public String toString() {
return "Gwc [map=" + map + ", allprice=" + getAllprice() + "]";
}
public void delByPid(int pid) {
map.remove(pid);
}
因为我菜所以,那个啥,积分我就直接在前端写${gwc.price*0.1了}哈哈哈哈哈哈哈
视图层如下:
<c:if test="${not empty gwc }">
<c:forEach items="${gwc.values}" var="x">
<tr class="active">
<td width="60" width="40%">
<input type="hidden" name="id" value="22">
<img src="${x.product.pimage}" width="70" height="60">
</td>
<td width="30%">
<a target="_blank"> ${x.product.pname}</a>
</td>
<td width="20%">
${x.product.shop_price}
</td>
<td width="10%">
<input type="text" name="quantity" value="${x.count}" maxlength="4" size="10">
</td>
<td width="15%">
<span class="subtotal">${x.price}</span>
</td>
<td>
<a href="javascript:void(0);" class="delete" id="${x.product.pid}">删除</a>
</td>
</tr>
</c:forEach>
<div style="margin-right:130px;">
<div style="text-align:right;">
<em style="color:#ff6600;">
登录后确认是否享有优惠
</em> 赠送积分: <em style="color:#ff6600;">${gwc.allprice*0.1}</em> 商品金额: <strong style="color:#ff6600;">${gwc.allprice}元</strong>
</div>
<div style="text-align:right;margin-top:10px;margin-bottom:10px;">
<a href="order_info.htm" id="clear" class="clear">清空购物车</a>
<a href="javascript:void(0)">
<input type="submit" width="100" value="提交订单" name="button" border="0" style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
height:35px;width:100px;color:white;">
</a>
</div>
</div>
</c:if>