1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%-- 3 Created by IntelliJ IDEA. 4 User: tony 5 Date: 2019/8/29 6 Time: 16:53 7 To change this template use File | Settings | File Templates. 8 --%> 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 10 <html> 11 <head> 12 <title>我的购物车</title> 13 <%--导入bootstrap框架--%> 14 <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"> 15 <script src="../bootstrap/js/jquery.min.js"></script> 16 <script src="../bootstrap/js/bootstrap.min.js"></script> 17 </head> 18 <body> 19 20 <div class="container"> 21 22 <div class="panel panel-primary"> 23 <div class="panel-heading"> 24 <h1>我的购物车</h1> 25 </div> 26 <div class="panel-body"> 27 28 <table class="table table-striped table-bordered table-hover text-center"> 29 30 <tr> 31 <td>编号</td> 32 <td>图片</td> 33 <td>菜名</td> 34 <td>风味</td> 35 <td>餐馆</td> 36 <td>价格</td> 37 <td>数量</td> 38 <td>小计</td> 39 <td>操作</td> 40 </tr> 41 42 <c:forEach items="${car.map}" var="m"> 43 <tr> 44 <td>${m.value.fid}</td> 45 <td><img width="35px" src="../${m.value.img}"/></td> 46 <td>${m.value.fname}</td> 47 <td>${m.value.ftype}</td> 48 <td>${m.value.fshop}</td> 49 <td>${m.value.fprice}.00元</td> 50 <td> 51 <a href="/food?param=change&cha=jian&fid=${m.value.fid}" style="text-decoration: none;" class="btn btn-primary glyphicon glyphicon-minus"></a> 52 ${m.value.itemNum} 53 <a href="/food?param=change&cha=jia&fid=${m.value.fid}" style="text-decoration: none;" class="btn btn-primary glyphicon glyphicon-plus"></a> 54 </td> 55 <td>${m.value.itemPrice}.00元</td> 56 <td> 57 <a class="btn btn-danger" href="/food?param=removeall&fid=${m.value.fid}">移除全部</a> 58 </td> 59 </tr> 60 </c:forEach> 61 62 <tr> 63 <td colspan="9"> 64 数量共:${car.sumnum}件, 总金额:${car.sumprice}.00元, 65 </td> 66 </tr> 67 68 </table> 69 <a class="btn btn-success btn-lg btn-block" href="/food?param=list">继续点餐</a> 70 </div> 71 72 <div class="panel-footer text-right"> 73 海底捞列表页 74 </div> 75 </div> 76 77 </div> 78 79 </body> 80 </html>
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%-- 3 Created by IntelliJ IDEA. 4 User: Administrator 5 Date: 2019/8/29 6 Time: 19:57 7 To change this template use File | Settings | File Templates. 8 --%> 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 10 <html> 11 <head> 12 <title>海底捞列表页</title> 13 <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"> 14 <script src="../bootstrap/js/jquery.min.js"></script> 15 <script src="../bootstrap/js/bootstrap.min.js"></script> 16 </head> 17 <body> 18 <div class="container"> 19 <div class="panel panel-primary"> 20 <div class="panel-heading"> 21 <h1>海底捞列表页</h1> 22 </div> 23 <div class="panel-body"> 24 <div class="form-group"> 25 <div class="row"> 26 <c:forEach items="${flist}" var="f"> 27 <div class="col-md-3"> 28 <div class="thumbnail"> 29 <img src="../${f.img}"> 30 <div class="caption"> 31 <h5>${f.fname}(${f.fshop}:${f.ftype})</h5> 32 <p>¥:${f.fprice}元</p> 33 <p> 34 <a href="/food?param=add&foodid=${f.fid}" class="btn btn-primary" role="button">开始点餐</a> 35 <a href="#" class="btn btn-warning" role="button">查看评论</a> 36 </p> 37 </div> 38 </div> 39 </div> 40 </c:forEach> 41 </div> 42 </div> 43 <a href="/food?param=view" class="btn btn-primary col-md-3 btn-lg" style="float: right;">查看购物车</a> 44 </div> 45 <div class="panel-footer text-right"> 46 海底捞列为您服务 47 </div> 48 </div> 49 </div> 50 </body> 51 </html>
1 package com.food.servlet; 2 3 import com.food.dao.FoodDao; 4 import com.food.entity.Car; 5 import com.food.entity.Car2; 6 import com.food.entity.CarItem; 7 import com.food.entity.Food; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import javax.servlet.http.HttpSession; 15 import java.io.IOException; 16 import java.math.BigDecimal; 17 import java.util.HashMap; 18 import java.util.Map; 19 20 @WebServlet("/food") 21 public class FoodServlet extends HttpServlet { 22 //需要一个dao对象 23 private FoodDao foodDao = new FoodDao(); 24 25 /** 26 * dopost,根据参数名判断进入哪一个servlet 27 * @param request 28 * @param response 29 * @throws ServletException 30 * @throws IOException 31 */ 32 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 doGet(request,response); 34 } 35 36 /** 37 * doget 38 * @param request 39 * @param response 40 * @throws ServletException 41 * @throws IOException 42 */ 43 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 44 String param = request.getParameter("param");//param=list 45 if (!(param==null || "".equals(param))){ 46 if ("list".equals(param)){展示所有的菜品信息 47 foodlist(request,response); 48 } else if ("add".equals(param)) {//点餐 49 foodadd(request,response); 50 } else if ("view".equals(param)) {//查看购物车 51 foodview(request,response); 52 } else if ("removeall".equals(param)){ 53 foodremoveall(request,response); 54 } else if("change".equals(param)){ 55 foodchange(request,response); 56 }else{ 57 throw new RuntimeException("参数错误。。。。"); 58 } 59 } 60 61 } 62 63 /** 64 * 购物车中加-减按钮servlet 65 * @param request 66 * @param response 67 * @throws IOException 68 */ 69 private void foodchange(HttpServletRequest request, HttpServletResponse response) throws IOException { 70 HttpSession session = request.getSession(); 71 Car2 car = (Car2) session.getAttribute("car"); 72 Map<Integer, CarItem> map = car.getMap(); 73 String cha = request.getParameter("cha"); 74 String fid = request.getParameter("fid"); 75 CarItem carItem = map.get(Integer.parseInt(fid)); 76 if ("jian".equals(cha)){ 77 if (carItem.getItemNum()-1<1){ 78 carItem.setItemNum(0); 79 }else{ 80 carItem.setItemNum(carItem.getItemNum()-1); 81 } 82 }else if ("jia".equals(cha)){ 83 carItem.setItemNum(carItem.getItemNum()+1); 84 } 85 map.put(Integer.parseInt(fid),carItem); 86 car.setMap(map); 87 session.setAttribute("car",car); 88 response.sendRedirect("/html/foodcar.jsp"); 89 } 90 91 /** 92 * 购物车中移除按钮servlet 93 * @param request 94 * @param response 95 * @throws IOException 96 */ 97 private void foodremoveall(HttpServletRequest request, HttpServletResponse response) throws IOException { 98 String fid = request.getParameter("fid"); 99 HttpSession session = request.getSession(); 100 Car2 car = (Car2)session.getAttribute("car"); 101 Map<Integer, CarItem> map = car.getMap(); 102 map.remove(Integer.parseInt(fid)); 103 car.setMap(map); 104 session.setAttribute("car",car); 105 response.sendRedirect("/html/foodcar.jsp"); 106 } 107 108 /** 109 * 菜品展示servlet 110 * @param request 111 * @param response 112 * @throws IOException 113 */ 114 private void foodview(HttpServletRequest request, HttpServletResponse response) throws IOException { 115 response.sendRedirect("/html/foodcar.jsp"); 116 } 117 118 /** 119 * 菜品添加servlet 120 * @param request 121 * @param response 122 * @throws ServletException 123 * @throws IOException 124 */ 125 private void foodadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 126 //获取菜品id 127 String foodid = request.getParameter("foodid"); 128 //找到菜品 129 Food food = foodDao.findFoodById(Integer.parseInt(foodid)); 130 //构造一个session 131 HttpSession session = request.getSession(); 132 //从session中拿购物车(car)对象 133 Object obj = session.getAttribute("car"); 134 //声明一个map对象 135 Map<Integer,CarItem> map = null; 136 //构造一个空的购物车 137 Car2 car = new Car2(); 138 if (obj == null){//第一次添加到购物车 139 //构造一个map对象 140 map = new HashMap<>(); 141 //构造子项 142 CarItem i = new CarItem(); 143 i.setImg(food.getImg()); 144 i.setFname(food.getFname()); 145 i.setFshop(food.getFshop()); 146 i.setFtype(food.getFtype()); 147 i.setFid(food.getFid()); 148 i.setFprice(food.getFprice()); 149 //小计金额与小计数量 150 i.setItemNum(1); 151 i.setItemPrice(food.getFprice()*1); 152 //添加到集合中 153 map.put(food.getFid(),i); 154 }else {//购物车中有小项 155 //判断添加的菜品是否有重复的,怎么判断? 156 map = ((Car2)obj).getMap();//转换为购物车对象,从购物车中拿到map对象 157 boolean b = map.keySet().contains(food.getFid());//根据菜品的id判断 158 if (b){//重复的菜品,数量加一,小计已经在CarItem类中计算了 159 CarItem carItem = map.get(food.getFid()); 160 carItem.setItemNum(carItem.getItemNum()+1); 161 }else{//不是重复的菜品 162 //构造子项 163 CarItem i = new CarItem(); 164 i.setImg(food.getImg()); 165 i.setFname(food.getFname()); 166 i.setFshop(food.getFshop()); 167 i.setFtype(food.getFtype()); 168 i.setFid(food.getFid()); 169 i.setFprice(food.getFprice()); 170 //小计金额与小计数量 171 i.setItemNum(1); 172 i.setItemPrice(food.getFprice()*1); 173 //添加到集合中 174 map.put(food.getFid(),i); 175 } 176 } 177 System.out.println("总金额"+car.getSumnum()+car.getSumprice()); 178 car.setMap(map); 179 session.setAttribute("car",car); 180 response.sendRedirect("/html/foodcar.jsp"); 181 } 182 183 /** 184 * 菜品列表servlet 185 * @param request 186 * @param response 187 * @throws ServletException 188 * @throws IOException 189 */ 190 private void foodlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 191 request.setAttribute("flist",foodDao.getFoodList()); 192 request.getRequestDispatcher("/html/foodlist.jsp").forward(request,response); 193 } 194 }
1 package com.food.entity; 2 3 import java.math.BigDecimal; 4 import java.util.Iterator; 5 import java.util.Map; 6 7 /** 8 * 购物车 9 * 包括每一种菜品小项 总计每一种菜品数量(不包括相同的菜品)、总计金额 10 */ 11 public class Car2 { 12 private int sumnum;//总数量数量 13 private int sumprice;//总金额 14 private Map<Integer,CarItem> map;//购物车中的每一项(每一个菜品信息) 15 16 public Car2() { 17 } 18 19 @Override 20 public String toString() { 21 return "Car2{" + 22 "sumnum=" + sumnum + 23 ", sumprice=" + sumprice + 24 ", map=" + map + 25 '}'; 26 } 27 28 public int getSumnum() { 29 return sumnum; 30 } 31 32 public void setSumnum(int sumnum) { 33 this.sumnum = sumnum; 34 } 35 36 public int getSumprice() { 37 return sumprice; 38 } 39 40 public void setSumprice(int sumprice) { 41 this.sumprice = sumprice; 42 } 43 44 public Map<Integer, CarItem> getMap() { 45 return map; 46 } 47 48 /** 49 * 计算总数量与计算总价格 50 * @param map 51 */ 52 public void setMap(Map<Integer, CarItem> map) { 53 int a = 0; 54 int b = 0; 55 Iterator<CarItem> i = map.values().iterator(); 56 while (i.hasNext()){ 57 CarItem c = i.next(); 58 a+=c.getItemNum(); 59 b+=c.getItemPrice(); 60 } 61 this.sumnum=a; 62 this.sumprice=b; 63 this.map = map; 64 } 65 }
1 package com.food.entity; 2 3 import java.io.Serializable; 4 import java.math.BigDecimal; 5 6 /** 7 * 购物车中每一个小项 8 * 小项中包括food类中的 信息 与小计数量、小计金额 9 */ 10 public class CarItem extends Food implements Serializable { 11 private int itemNum;//购物车中小项的菜品数量 12 private int itemPrice;//购物车中小项的菜品价格小计 13 14 public CarItem() { 15 } 16 17 @Override 18 public String toString() { 19 return "CarItem{" + 20 "itemNum=" + itemNum + 21 ", itemPrice=" + itemPrice + 22 '}'; 23 } 24 25 public int getItemNum() { 26 return itemNum; 27 } 28 29 public void setItemNum(int itemNum) { 30 this.itemNum = itemNum; 31 } 32 33 public int getItemPrice() { 34 return this.getItemNum()*this.getFprice(); 35 } 36 37 public void setItemPrice(int itemPrice) { 38 this.itemPrice = itemPrice; 39 } 40 }
1 package com.food.entity; 2 3 import java.io.Serializable; 4 import java.math.BigDecimal; 5 6 /** 7 * 菜品类 8 */ 9 public class Food implements Serializable { 10 private int fid;//int(11) NOT NULL购物车id 11 private String fname;//varchar(20) NULL购物车商品名称 12 private String ftype;//varchar(20) NULL购物车中商品类型 13 private String fshop;//varchar(100) NULL购物车中的商品 14 private int fprice;//decimal(9,2) NULL价格 15 private String img;//varchar(30) NULL图片 16 17 @Override 18 public String toString() { 19 return "Food{" + 20 "fid=" + fid + 21 ", fname='" + fname + '\'' + 22 ", ftype='" + ftype + '\'' + 23 ", fshop='" + fshop + '\'' + 24 ", fprice=" + fprice + 25 ", img='" + img + '\'' + 26 '}'; 27 } 28 29 public Food() { 30 } 31 32 public int getFid() { 33 return fid; 34 } 35 36 public void setFid(int fid) { 37 this.fid = fid; 38 } 39 40 public String getFname() { 41 return fname; 42 } 43 44 public void setFname(String fname) { 45 this.fname = fname; 46 } 47 48 public String getFtype() { 49 return ftype; 50 } 51 52 public void setFtype(String ftype) { 53 this.ftype = ftype; 54 } 55 56 public String getFshop() { 57 return fshop; 58 } 59 60 public void setFshop(String fshop) { 61 this.fshop = fshop; 62 } 63 64 public int getFprice() { 65 return fprice; 66 } 67 68 public void setFprice(int fprice) { 69 this.fprice = fprice; 70 } 71 72 public String getImg() { 73 return img; 74 } 75 76 public void setImg(String img) { 77 this.img = img; 78 } 79 }
页面效果