面向对象中的session版的购物车
生活随笔
收集整理的這篇文章主要介紹了
面向对象中的session版的购物车
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先設置一個product類,用來存儲和獲取數據,使用LIst集合存儲所有商品即(ProductDao類),在ShowProductServlet類查看商品信息,然后將數據提交到AddCarServlet類來處理數據,最后在ShowCarServlet類查看購物車并顯示數量和價格。
程序運行如下圖所示:
ShowProductServlet類顯示效果
ShowCarServlet類查看購物車
源代碼如下:
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; //默認值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 {//存儲所有的商品的集合public static List<Product> data = new ArrayList<Product>();static{data.add(new Product(1,"三星",4500,"Android機皇"));data.add(new Product(2,"蘋果",5800,"ISO系統,流暢爽快"));data.add(new Product(3,"小米",1999,"為發燒而生"));data.add(new Product(4,"華為",2888,"強大的研發實力"));data.add(new Product(5,"一加",1999,"超高的性價比"));}//返回所有的商品數據public List<Product> findAll(){return data;}//根據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對象 //顯示中文response.setContentType("text/html;charset=utf-8");//顯示商品列表String html ="";html += "<html><body>";html +="<table border='1' width = 500px>";html += "<tr><th>編號</th><th>品牌</th><th>價格</th><th>描述</th><th>購買</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,用以添加商品數量"<td><a href='"+request.getContextPath()+"/AddCarServlet?id="+p.getId()+"'>購買</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();//設計一個集合用以存儲購物車中的所有商品數據//使用session.getAttribute()是為了判斷第一個商品Map<Integer,Product> car = (Map<Integer, Product>) session.getAttribute("car");//如果是第一個商品,就進行初始化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)對應的值p.setNum(p.getNum()+1); //商品數量+1}else{//沒有買過,需要重新加入購物車car.put(product.getId(), product);}//將car的值賦值給屬性car,后者給前者session.setAttribute("car", car);//回顯response.setContentType("text/html;charset=utf-8");response.getWriter().write(product.getName()+"已成功加入購物車! <a href = '"+request.getContextPath()+"/ShowCarServlet'>查看購物車</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中取出數據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>編號</th><th>品牌</th><th>價格</th><th>數量</th><th>描述</th><th>小計</th>";double totalProduct = 0; //商品的總價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'>合計:"+totalProduct+"元</td></tr>";html += "</table>";html += "<p><a href='"+request.getContextPath()+"/ShowProductServlet'>繼續購物</a>";html += "</body></html>";response.getWriter().write(html);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
總結
以上是生活随笔為你收集整理的面向对象中的session版的购物车的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用cookie显示上次浏览的时间
- 下一篇: 利用MySQL创建一个简单的employ