购物车包括货物bean,业务逻辑处理bean(购物车bean),控制器servlet,显示页面mycart.jsp。
货物bean的属性对应了货物的数据库表的一些字段,同时加上了购买的数量。
package com.xie.shop.modal;
//货物bean
public class CartBean{
private String id;
private String goodName;
private float goodsPrice;
private int goodsNum;
private String buyNum;
public String getBuyNum() {
return buyNum;
}
public void setBuyNum(String buyNum) {
this.buyNum = buyNum;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public float getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(float goodsPrice) {
this.goodsPrice = goodsPrice;
}
public int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
}
业务逻辑处理bean(购物车bean):
主要的逻辑是:将货物编号和货物实体放入一个HashMap中,HashMap购物车bean的一个属性,同时提供了一个能返回HashMap中value所对应的货物bean
import java.sql.Connection;
import java.sql.PreparedStatement;
//购物车的业务逻辑处理
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class MyCartPro {
private Connection ct=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
Map<String , CartBean> cMap=new HashMap<String, CartBean>();
private float totalPrice=0;
public float getTotalPrice() {
return totalPrice;
}
public Map<String, CartBean> getcMap() {
return cMap;
}
/**
* 返回选中准备购买的货物
* @author Administrator:centre
* @param id:传入选中货物的编号
* @return cb:操作成功返回一个CartBean
*/
private CartBean getGood(String id){
CartBean cb=new CartBean();
try {
ct=new ConnGood().getConn();
ps=ct.prepareStatement("select goodsid, goodsname,goodsprice,goodsnum from goods where goodsid=?");
ps.setString(1, id);
rs=ps.executeQuery();
if(rs.next()) {
cb.setId(rs.getLong(1)+"");
cb.setGoodName(rs.getString(2));
cb.setGoodsPrice(rs.getFloat(3));
cb.setGoodsNum(rs.getInt(4));
cb.setBuyNum(1+"");
}
} catch (Exception e) {
System.out.println("取得CartBean失败。");
e.printStackTrace();
}finally{
closeRs();
}
return cb;
}
/**
* 将cb放入到一个map里
* @author Administrator:centre
* @param id:商品id
*/
public void addBook(String id){
if (!cMap.containsKey(id)) {
cMap.put(id, getGood(id));
}else {
cMap.get(id).setBuyNum((Integer.parseInt(cMap.get(id).getBuyNum())+1)+"");
}
}
/**
* 返回购物车容器
* @author Administrator:centre
* @return 返回购物车的容器
*/
public Map<String , CartBean> myCart(){
return getcMap();
}
/**
* 删除选定的商品
* @author Administrator:centre
* @param id:货物的id
* @return:删除成功,返回true,否则返回false
*/
public void deleteBook(String id){
cMap.remove(id);
}
/**
* 删除购物车的所有商品
* @author Administrator:centre
*/
public void deleteAllBooks(){
cMap.clear();
}
/**
* 修改所有商品购买数量
* @author Administrator:centre
* @param id:商品id
* @param num:商品数量
*/
public void updateNum(String[] id,String[] num){
for (int i = 0; i < id.length; i++) {
if (cMap.containsKey(id[i])) {
if (num[i].equals("0")||num[i].equals("")) {
cMap.remove(id[i]);
}else {
cMap.get(id[i]).setBuyNum(num[i]);
}
}
}
}
/**
* 返回所有购买的货物
* @author Administrator:centre
* @return:无返回值
*/
public List<CartBean> showMyCart(){
List<CartBean> al=new ArrayList<CartBean>();
Iterator<String> it=cMap.keySet().iterator();
totalPrice=0;
while (it.hasNext()) {
CartBean cb=cMap.get(it.next());
totalPrice+=Integer.parseInt(cb.getBuyNum())*cb.getGoodsPrice();
al.add(cb);
}
return al;
}
/**
* @author Administrator:centre
* 关闭相关的数据库资源
*/
private void closeRs(){
try {
if (rs!=null) {
rs.close();
rs=null;
}
if (ps!=null) {
ps.close();
ps=null;
}
if (ct!=null) {
ct.close();
ct=null;
}
} catch (Exception e) {
System.out.println("关闭资源失败。");
e.printStackTrace();
}
}
}
控制器servlet:
如何让每个IE进程都只有唯一的一个购物车呢?我用了session。
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xie.shop.modal.CartBean;
import com.xie.shop.modal.MyCartPro;
public class MyCartServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cartop=request.getParameter("cartop");
if (cartop.equals("add")) {
String id=request.getParameter("id");
String pageNow=request.getParameter("pageNow");
MyCartPro mcp=(MyCartPro)request.getSession().getAttribute("mycart");
if (mcp==null) {
//第一次购物,创建一个购物车,同时加入session
mcp=new MyCartPro();
request.getSession().setAttribute("mycart", mcp);
}
if (id!=null) {
mcp.addBook(id);
}
List<CartBean> al=mcp.showMyCart();
request.setAttribute("alist", al);
request.setAttribute("pageNow", pageNow);
request.getRequestDispatcher("jsps/mycart.jsp").forward(request, response);
}else if (cartop.equals("delete")) {
MyCartPro mcp=(MyCartPro)request.getSession().getAttribute("mycart");
String id=request.getParameter("id");
String pageNow=request.getParameter("pageNow");
mcp.deleteBook(id);
List<CartBean> al=mcp.showMyCart();
request.setAttribute("alist", al);
request.setAttribute("pageNow", pageNow);
request.getRequestDispatcher("jsps/mycart.jsp").forward(request, response);
}else if (cartop.equals("deleteall")) {
String pageNow=request.getParameter("pageNow");
MyCartPro mcp=(MyCartPro)request.getSession().getAttribute("mycart");
if (mcp==null) {
request.setAttribute("pageNow", pageNow);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else {
mcp.deleteAllBooks();
request.setAttribute("pageNow", pageNow);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}else if (cartop.equals("chagenum")) {
String pageNow=request.getParameter("pageNow");
String[] id=request.getParameterValues("goodId");
String[] newNum=request.getParameterValues("newNum");
/*for (int i = 0; i < id.length; i++) {
System.out.println("id=="+id[i]+" "+"num=="+newNum[i]);
}*/
MyCartPro mcp=(MyCartPro)request.getSession().getAttribute("mycart");
if (mcp==null) {
request.getRequestDispatcher("jsps/mycart.jsp").forward(request, response);
}else {
mcp.updateNum(id, newNum);
List<CartBean> al=mcp.showMyCart();
request.setAttribute("alist", al);
request.setAttribute("pageNow", pageNow);
request.getRequestDispatcher("jsps/mycart.jsp").forward(request, response);
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
显示页面mycart.jsp