面向对象中的session版的购物车
生活随笔
收集整理的這篇文章主要介紹了
面向对象中的session版的购物车
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先設(shè)置一個(gè)product類,用來(lái)存儲(chǔ)和獲取數(shù)據(jù),使用LIst集合存儲(chǔ)所有商品即(ProductDao類),在ShowProductServlet類查看商品信息,然后將數(shù)據(jù)提交到AddCarServlet類來(lái)處理數(shù)據(jù),最后在ShowCarServlet類查看購(gòu)物車并顯示數(shù)量和價(jià)格。
程序運(yùn)行如下圖所示:
ShowProductServlet類顯示效果
ShowCarServlet類查看購(gòu)物車
源代碼如下:
Product
package star.july.buycar;public class Product {private int id;private String name;private int price;public void setPrice(int price) {this.price = price;}private String descr;private int num =1; //默認(rèn)值public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescr() {return descr;}public void setDescr(String descr) {this.descr = descr;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public int getPrice(){return price;}public Product(int id, String name, int price, String descr) {super();this.id = id;this.name = name;this.price = price;this.descr = descr;}public Product() {super();}}
ProductDao:
package star.july.buycar;import java.util.ArrayList; import java.util.List;//商品dao類 public class ProductDao {//存儲(chǔ)所有的商品的集合public static List<Product> data = new ArrayList<Product>();static{data.add(new Product(1,"三星",4500,"Android機(jī)皇"));data.add(new Product(2,"蘋果",5800,"ISO系統(tǒng),流暢爽快"));data.add(new Product(3,"小米",1999,"為發(fā)燒而生"));data.add(new Product(4,"華為",2888,"強(qiáng)大的研發(fā)實(shí)力"));data.add(new Product(5,"一加",1999,"超高的性價(jià)比"));}//返回所有的商品數(shù)據(jù)public List<Product> findAll(){return data;}//根據(jù)id查找商品public Product findById(int id){for(Product p:data){if(p.getId() == id){return p;}}return null;} }
ShowProductServlet:
package star.july.buycar;import java.io.IOException; import java.io.PrintWriter; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class ShowProductServlet extends HttpServlet {ProductDao dao = new ProductDao();public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {List<Product> product = dao.findAll(); //返回Product對(duì)象 //顯示中文response.setContentType("text/html;charset=utf-8");//顯示商品列表String html ="";html += "<html><body>";html +="<table border='1' width = 500px>";html += "<tr><th>編號(hào)</th><th>品牌</th><th>價(jià)格</th><th>描述</th><th>購(gòu)買</th>";for(Product p: product){html += "<tr><td>"+p.getId()+"</td><td>"+p.getName()+"</td>" +"<td>"+p.getPrice()+"</td><td>"+p.getDescr()+"</td>" +//getContextPath:獲取父目錄。 在超鏈接中傳遞商品的id,用以添加商品數(shù)量"<td><a href='"+request.getContextPath()+"/AddCarServlet?id="+p.getId()+"'>購(gòu)買</a></td>"; } html += "</table>";html += "</body></html>";response.getWriter().write(html);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}
AddCarServlet:
package star.july.buycar;import java.io.IOException; import java.util.HashMap; import java.util.Map;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;public class AddCarServlet extends HttpServlet {ProductDao dao = new ProductDao();public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//獲得IDString id = request.getParameter("id");//查找商品Product product = dao.findById(Integer.parseInt(id)); //獲取sessionHttpSession session = request.getSession();//設(shè)計(jì)一個(gè)集合用以存儲(chǔ)購(gòu)物車中的所有商品數(shù)據(jù)//使用session.getAttribute()是為了判斷第一個(gè)商品Map<Integer,Product> car = (Map<Integer, Product>) session.getAttribute("car");//如果是第一個(gè)商品,就進(jìn)行初始化if(car == null ){car = new HashMap<Integer,Product>();}if(car != null && car.containsKey(Integer.parseInt(id))){ //判斷是否是集合里的key的值 Product p = car.get(Integer.parseInt(id)); //返回key(id)對(duì)應(yīng)的值p.setNum(p.getNum()+1); //商品數(shù)量+1}else{//沒(méi)有買過(guò),需要重新加入購(gòu)物車car.put(product.getId(), product);}//將car的值賦值給屬性car,后者給前者session.setAttribute("car", car);//回顯response.setContentType("text/html;charset=utf-8");response.getWriter().write(product.getName()+"已成功加入購(gòu)物車! <a href = '"+request.getContextPath()+"/ShowCarServlet'>查看購(gòu)物車</a>");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
ShowCarServlet:
package star.july.buycar;import java.io.IOException; import java.util.Map; import java.util.Map.Entry;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;public class ShowCarServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//從HttpSession中取出數(shù)據(jù)HttpSession session = request.getSession();Map<Integer,Product> car =(Map<Integer,Product>)session.getAttribute("car");//顯示中文response.setContentType("text/html;charset=utf-8");String html ="";html += "<html><body>";html +="<table border='1' width = 500px>";html += "<tr><th>編號(hào)</th><th>品牌</th><th>價(jià)格</th><th>數(shù)量</th><th>描述</th><th>小計(jì)</th>";double totalProduct = 0; //商品的總價(jià)for(Entry<Integer,Product> entry : car.entrySet()){Product p = entry.getValue(); html += "<tr><td>"+p.getId()+"</td><td>"+p.getName()+"</td>" +"<td>"+p.getPrice()+"元</td><td>"+p.getNum()+"</td><td>"+p.getDescr()+"" +"</td><td>"+p.getPrice()*p.getNum()+"元</td>";totalProduct += p.getPrice()*p.getNum();} html += "<tr><td colspan ='6' align='right'>合計(jì):"+totalProduct+"元</td></tr>";html += "</table>";html += "<p><a href='"+request.getContextPath()+"/ShowProductServlet'>繼續(xù)購(gòu)物</a>";html += "</body></html>";response.getWriter().write(html);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
總結(jié)
以上是生活随笔為你收集整理的面向对象中的session版的购物车的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 利用cookie显示上次浏览的时间
- 下一篇: 利用MySQL创建一个简单的employ