Cart 類 //實體類
javascript
1 package com.shore.entity; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /** 7 * @author DSHORE/2019-5-16 8 * 9 */ 10 public class Cart { 11 //Map集合中,可能有不少本書 12 private Map<String, CartItem> items = new HashMap<String, CartItem>();//key:購物項對應的書的id value:購物項 13 private int num;//購物車中,書籍的數量 14 private float price;//總價,付款時的金額 15 16 public Map<String, CartItem> getItems() { 17 return items; 18 } 19 20 public int getNum() { 21 num = 0; 22 for(Map.Entry<String, CartItem> me:items.entrySet()){ 23 num += me.getValue().getNum(); 24 } 25 return num; 26 } 27 /*public void setNum(int num) {//不能設置書籍的數量 28 this.num = num; 29 }*/ 30 public float getPrice() { 31 price = 0; 32 for(Map.Entry<String, CartItem> me:items.entrySet()){ 33 price += me.getValue().getPrice(); 34 } 35 return price; 36 } 37 /*public void setPrice(float price) {//不能設置價格 38 this.price = price; 39 }*/ 40 41 //把商品加入購物車 42 public void addCart(Book book) {//同一本書,可購買了多本 43 if(items.containsKey(book.getId())){ 44 //有這本書的id(當同一本書,我購買兩本或更多本時,保持其餘字段的屬性不變,只增長數量) 45 CartItem item = items.get(book.getId()); 46 item.setNum(item.getNum() + 1);//其餘不改變,只增長數量 47 }else{ 48 //沒有這本書的id 49 CartItem item = new CartItem(); 50 item.setBook(book); 51 item.setNum(1); 52 items.put(book.getId(), item); 53 } 54 } 55 56 //刪除購物車中的商品(同一種商品,有多個時,則是減小該商品的數量) 57 public void deleteCartByBook(String bookId) { 58 CartItem item = items.get(bookId); 59 if (item.getNum() > 1) {//購物車中,同一商品,購買了多件,則每刪除一次,該商品的數量就減一次(價格自動減小) 60 item.setNum(item.getNum() - 1);//其餘不改變,只減小數量 61 }else { 62 items.remove(bookId); 63 } 64 } 65 }
CartItem 類 //購物項類html
1 package com.shore.entity; 2 3 /** 4 * @author DSHORE/2019-5-16 5 * 6 */ 7 public class CartItem {//購物項 8 private Book book; 9 private int num; 10 private float price; 11 12 public Book getBook() { 13 return book; 14 } 15 public void setBook(Book book) { 16 this.book = book; 17 } 18 public int getNum() { 19 return num; 20 } 21 public void setNum(int num) { 22 this.num = num; 23 } 24 public float getPrice() { 25 return num*book.getPrice();//計算出同一本書的總價(或同一類書) 26 } 27 /*public void setPrice(float price) {//不能設置價格 28 this.price = price; 29 }*/ 30 }
ClientServlet 類前端
1 package com.shore.web.controller.client; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 import com.shore.entity.Book; 13 import com.shore.entity.Cart; 14 import com.shore.entity.CartItem; 15 import com.shore.service.BookService; 16 import com.shore.service.impl.BookServiceImpl; 17 18 /** 19 * @author DSHORE/2019-5-12 20 * 21 */ 22 public class ClientServlet extends HttpServlet { 23 private static final long serialVersionUID = 1L;//序列化 24 BookService bService = new BookServiceImpl(); 25 26 public void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 String operation = request.getParameter("operation"); 29 if ("addCartByBook".equals(operation)){ 30 addCartByBook(request,response); 31 }else if("deleteCartByBook".equals(operation)){ 32 deleteCartByBook(request,response); 33 } 34 } 35 36 //刪除購物車中的商品(同一種商品,有多個時,則是減小該商品的數量) 37 private void deleteCartByBook(HttpServletRequest request, 38 HttpServletResponse response) throws ServletException, IOException { 39 //獲取數的id 40 String bookId = request.getParameter("bookId"); 41 //從HttpSession中取出購物車 42 HttpSession session = request.getSession(); 43 Cart cart = (Cart)session.getAttribute("cart"); 44 cart.deleteCartByBook(bookId); //直接調用 實體類Cart中的deleteCartByBook()方法 45 /*//提示刪除商品成功 46 request.setAttribute("message","<script type='text/javascript'>alert('刪除商品成功')</script>");*/ 47 request.getRequestDispatcher("/client/showCart.jsp").forward(request, response); 48 } 49 50 //購買書籍(把要購買的商品加入購物車) 51 private void addCartByBook(HttpServletRequest request, 52 HttpServletResponse response) throws ServletException, IOException { 53 //獲取數的id 54 String bookId = request.getParameter("bookId"); 55 //獲取要購買的書 56 Book book = bService.findBookById(bookId); 57 //從HttpSession中取出購物車 58 HttpSession session = request.getSession(); 59 Cart cart = (Cart)session.getAttribute("cart"); 60 //沒有:建立購物車,並放入到HttpSession中(購物車的car) 61 if(cart == null){ 62 cart = new Cart(); 63 session.setAttribute("cart",cart); 64 } 65 //有:把書放到購物車中 66 cart.addCart(book);//直接調用 實體類Cart中的addCart()方法 67 //提示加入購物車成功 68 request.setAttribute("message","<script type='text/javascript'>alert('加入購物車成功,請前去付款!')</script>"); 69 request.getRequestDispatcher("/index.jsp").forward(request, response); 70 } 71 72 public void doPost(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException, IOException { 74 doGet(request, response); 75 } 76 }
showCart.jsp 購物車的頁面(簡單測試,頁面隨意搞搞的,還看得過去吧)java
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@include file="/client/header.jsp"%> 3 <style> 4 table tr th{ border:1px solid #C1C1C1; font-size: 16px;} 5 table,table tr td { border:1px solid #C1C1C1; } 6 table { width: 71%; min-height: 25px; line-height: 25px; border-collapse: collapse; padding:2px; margin:auto;text-align: center;} 7 </style> 8 9 <h2 style="text-align: center;font-size: 20px">您購買的商品以下</h2> 10 <c:if test="${empty sessionScope.cart.items}"> 11 <!-- 若是購物車爲空(沒商品),則顯示這張圖片,提示「購物車空空如也」 --> 12 <img height="432" width="720" src="${pageContext.request.contextPath}/images/0.jpg"/> 13 </c:if> 14 <c:if test="${!empty sessionScope.cart.items}"> 15 <table> 16 <tr> 17 <th>書名</th> 18 <th>做者</th> 19 <th>單價</th> 20 <th>數量</th> 21 <th>小計</th> 22 <th>操做</th> 23 </tr> 24 <c:forEach items="${sessionScope.cart.items}" var="me"> 25 <tr> 26 <td>${me.value.book.name}</td> 27 <td>${me.value.book.author}</td> 28 <td>${me.value.book.price}</td> 29 <td>${me.value.num}</td> 30 <td>${me.value.price}</td> 31 <td> 32 <a href="${pageContext.request.contextPath}/ClientServlet?operation=deleteCartByBook&bookId=${me.value.book.id}">刪除</a> 33 </td> 34 </tr> 35 </c:forEach> 36 <tr> 37 <td colspan="6" align="right"> 38 總數量:${sessionScope.cart.num} 39 應付款總金額:${sessionScope.cart.price} 40 <a href="${pageContext.request.contextPath}/ClientServlet?operation=genOrders" style="text-decoration: none;font-weight: bold;">付款購買</a> 41 42 </td> 43 </tr> 44 </table> 45 </c:if> 46 ${message}
效果圖:web
原創做者:DSHOREsession 做者主頁:http://www.cnblogs.com/dshore123/jsp 原文出自:http://www.javashuo.com/article/p-mrfxngfn-a.htmlpost 歡迎轉載,轉載務必說明出處。(若是本文對您有幫助,能夠點擊一下右下角的 推薦,或評論,謝謝!)測試 |