流程图
封装一个购物车对象
1、Cart:购物车对象
CartItem的集合(选择Map集合,移除购物车比较方便)
total:总计
功能
将购物项添加到购物车
从购物车中移除购物项
清空购物车
2、CartItem:购物项对象
Product:商品信息
count:购买某种商品数量
subtotal:购买某种商品的小计
实体类
1、购物项类
package cn.itcast.shop.cart.vo; import cn.itcast.shop.product.vo.Product; /** * 购物项对象 * @author 郭贤达 * */ public class CartItem { private Product product; // 购物项中商品信息 private int count; // 购买某种商品数量 private double subtotal; // 购买某种商品小计 public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } // 小计自动计算的. public double getSubtotal() { return count * product.getShop_price(); } /*public void setSubtotal(double subtotal) { this.subtotal = subtotal; } */ }
2、购物车类
package cn.itcast.shop.cart.vo; import java.io.Serializable; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; /* * 购物车对象 */ public class Cart implements Serializable { //购物车属性 //购物箱集合:Map的key就是商品pid,value:购物项 private Map<Integer,CartItem> map = new LinkedHashMap<Integer,CartItem>(); //Cart对象中有一个叫CartItem属性 public Collection<CartItem> getCartItems(){ return map.values(); } //购物总计 private double total; public double getTotal() { return total; } //购物车的功能 //1.将购物项添加到购物车 public void addCart(CartItem cartItem){ //判断购物车中是否已经存在该购物项 /* * 如果存在,数量增加 * 总计=总计+购物项小计 * 如果不存在 * 向map中添加购物项 * 总计=总计+购物项小计 */ //获得商品id Integer pid=cartItem.getProduct().getPid(); //判断购物车中是否已经存在该购物项 if(map.containsKey(pid)){ //存在 CartItem _cartItem=map.get(pid);//获得购物车中原来的购物项 _cartItem.setCount(cartItem.getCount()+cartItem.getCount()); }else{ //不存在 map.put(pid, cartItem); } //设置总计的值 total += cartItem.getSubtotal(); } //2.从购物车移除购物项 public void removeCart(Integer pid){ //将购物项移除购物车 CartItem cartItem=map.remove(pid); //总计=总计-移除的购物项小计 total -= cartItem.getSubtotal(); } //3.清空购物车 public void clearCart(){ //将所有购物项清空 map.clear(); //将总设计设置为0 total=0; } }
jsp中提交表单
<script> function saveCart(){ document.getElementById("cartForm").submit(); } </script> <form id="cartForm" action="${pageContext.request.contextPath }/cart_addCart.action" method="post"> <input type="hidden" name="pid" value="<s:property value="model.pid"/>"/> <div class="action"> <dl class="quantity"> <dt>购买数量:</dt> <dd> <input id="count" name="count" value="1" maxlength="4" onpaste="return false;" type="text"/> <div> <span id="increase" class="increase"> </span> <span id="decrease" class="decrease"> </span> </div> </dd> <dd> 件 </dd> </dl> <div class="buy"> <input id="addCart" class="addCart" value="加入购物车" type="button" onclick="saveCart()"/> </div> </div> </form>
action
package cn.itcast.shop.cart.action; import org.apache.struts2.ServletActionContext; import cn.itcast.shop.cart.vo.Cart; import cn.itcast.shop.cart.vo.CartItem; import cn.itcast.shop.product.service.ProductService; import cn.itcast.shop.product.vo.Product; import com.opensymphony.xwork2.ActionSupport; /** * 购物车Action * * @author 传智.郭嘉 * */ public class CartAction extends ActionSupport { // 接收pid private Integer pid; // 接收数量count private Integer count; // 注入商品的Service private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } public void setPid(Integer pid) { this.pid = pid; } public void setCount(Integer count) { this.count = count; } // 将购物项添加到购物车:执行的方法 public String addCart() { // 封装一个CartItem对象. CartItem cartItem = new CartItem(); // 设置数量: cartItem.setCount(count); // 根据pid进行查询商品: Product product = productService.findByPid(pid); // 设置商品: cartItem.setProduct(product); // 将购物项添加到购物车. // 购物车应该存在session中. Cart cart = getCart(); cart.addCart(cartItem); return "addCart"; } // 清空购物车的执行的方法: public String clearCart(){ // 获得购物车对象. Cart cart = getCart(); // 调用购物车中清空方法. cart.clearCart(); return "clearCart"; } // 从购物车中移除购物项的方法: public String removeCart(){ // 获得购物车对象 Cart cart = getCart(); // 调用购物车中移除的方法: cart.removeCart(pid); // 返回页面: return "removeCart"; } // 我的购物车:执行的方法 public String myCart(){ return "myCart"; } /** * 获得购物车的方法:从session中获得购物车. * @return */ private Cart getCart() { Cart cart = (Cart) ServletActionContext.getRequest().getSession() .getAttribute("cart"); if (cart == null) { cart = new Cart(); ServletActionContext.getRequest().getSession() .setAttribute("cart", cart); } return cart; } }