首先分析数据库
那些数据需要传 ,那些不需要
-
订单表
桌子(全局sesion拿) ,日期(new()) ,总价(订单有保存在sesion中或者自己算) ,状态默认为0 -
订单详情
订单id (后生成的),菜名 ,数量 [菜不止一个 需要循环]
// 将最新下的订单的id值给取出来
orderId = ordersService.getMaxId();
想好我们的数据都可以拿到,从哪拿 就可以说是
读书破万卷,下笔如有神
代码编写
按钮:[下单] 开始
<input type="button" value="下单" class="btn_next" onclick="genernateOrder()" />
// 下单
function genernateOrder() {
window.location.href = "${pageContext.request.contextPath }/order?method=takeOrder";
}
好这里总价我们自己算
涉及map的遍历
Map<Food,Integer> m =(Map<Food,Integer>) session.getAttribute("foods");
Set<Entry<Food, Integer>> entrySet =m.entrySet();
变成Set结构
for(Map.Entry<Food, Integer> entry :entrySet){
Food food = entry.getKey();
Integer count = entry.getValue();
sum+=food.getPrice()*count;
//自己算
}
//订单详情
for (Map.Entry<Food, Integer> entry : entrySet) {
OrderDetail orderdetail = new OrderDetail();
Food food = entry.getKey();
Integer count = entry.getValue();
orderdetail.setFood_id(food.getId());
orderdetail.setFoodCount(count);
orderdetail.setOrderId(orderId);
orderDetailService.add(orderdetail);
}
上完整代码:
package com.xjo.servlet;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xjo.entity.Food;
import com.xjo.entity.OrderDetail;
import com.xjo.entity.Orders;
import com.xjo.utils.PageBean;
@WebServlet("/order")
public class OrderServlet extends BaseServlet {
@Override
public void init() throws ServletException {
List<Orders> orders = ordersService.query();
List<OrderDetail> orderDetail = orderDetailService.query();
// servletContext
this.getServletContext().setAttribute("orders", orders);
this.getServletContext().setAttribute("orderDetail", orderDetail);
}
public Object list(HttpServletRequest request, HttpServletResponse response){
Object uri=null;
//1.判断当前页,如果没有默认第1页
String currPage=request.getParameter("currentPage");
if(currPage==null||"".equals(currPage.trim())){
currPage="1";
}
int currentPage=Integer.parseInt(currPage);
//2.创建PageBean
PageBean<Orders> pageBean=new PageBean<Orders>();
pageBean.setCurrentPage(currentPage);
pageBean.setPageCount(2);
//3.调用service
ordersService.getAll(pageBean);
//4.保存pagebean到requester
request.setAttribute("pageBean", pageBean);
//5.将方法调用返回值baseServlet
uri=request.getRequestDispatcher("sys/order/orderList.jsp");
System.out.println(uri);
return uri;
}
//放入购物车 点击放入餐车 跳到购物车 接收到参数food_id
public Object putInCar(HttpServletRequest request, HttpServletResponse response){
Object uri=null;
HttpSession session = request.getSession();
String id = request.getParameter("food_id");
Food food = new Food();
//添加到购物车的菜
food= foodService.findById(Integer.parseInt(id));
//取购物车 遍历对比原来是否有存过 决定是加1 还是 另外生成
Map<Food,Integer> m = (Map)session.getAttribute("foods");
if(m==null){
Map<Food,Integer> map = new LinkedHashMap<Food,Integer>();//底层是链表实现的可以保证怎么存就怎么取
map.put(food, 1);
session.setAttribute("foods", map);
}
if(m!=null){
if(m.containsKey(food)){ //方法用来判断Map集合对象中是否包含指定的键名
//有
Integer count = m.get(food);//该方法返回指定键所映射的值。如果此映射不包含该键的映射关系,则返回null。
count++;
//返回页面
m.put(food, count);
} else{
//没有
m.put(food, 1);
}
}
//存回页面
if(m!=null){
session.setAttribute("foods", m);
}
uri= request.getRequestDispatcher("app/detail/clientCart.jsp");
return uri;
}
//删除购物车内的菜
public Object removeOrder(HttpServletRequest request, HttpServletResponse response) {
Object uri=null;
String id=request.getParameter("gid");
Food food=foodService.findById(Integer.parseInt(id));
HttpSession session=request.getSession();
Map<Food, Integer> m = (Map<Food, Integer>) session.getAttribute("foods");
m.remove(food);
session.setAttribute("foods", m);
uri="/app/detail/clientCart.jsp";
return uri;
}
//修改购物车内的菜的数量
public Object alterSorder(HttpServletRequest request, HttpServletResponse response) {
Object uri=null;
String id=request.getParameter("gid");
String num=request.getParameter("snumber");
System.out.println(id);
System.out.println(num);
Food food=foodService.findById(Integer.parseInt(id));
HttpSession session=request.getSession();
Map<Food, Integer> m = (Map<Food, Integer>) session.getAttribute("foods");
m.put(food, Integer.parseInt(num));
session.setAttribute("foods", m);
uri="/app/detail/clientCart.jsp";
return uri;
}
//订单生成
public Object takeOrder(HttpServletRequest request, HttpServletResponse response){
Object uri=null;
HttpSession session = request.getSession();
Map<Food,Integer> m =(Map<Food,Integer>) session.getAttribute("foods");
Set<Map.Entry<Food, Integer>> entrySet =m.entrySet();
// String table_id =session.getAttribute("table_id"); 前面没有完成 这里先写死
int table_id =1;
int orderId = 0;
//新建订单对象
Orders order = new Orders();
double sum=0;//总价格 我们自己算吧map的食物一个一个拿出来计算
for(Map.Entry<Food, Integer> entry :entrySet){
Food food = entry.getKey();
Integer count = entry.getValue();
sum+=food.getPrice()*count;
}
order.setTable_id(table_id);
order.setTotalPrice(sum);
order.setOrderDate(new Date());
ordersService.add(order);
// 将最新下的订单的id值给取出来
orderId = ordersService.getMaxId();
// int a=1/0;
// 添加订单详情
for (Map.Entry<Food, Integer> entry : entrySet) {
OrderDetail orderdetail = new OrderDetail();
Food food = entry.getKey();
Integer count = entry.getValue();
orderdetail.setFood_id(food.getId());
orderdetail.setFoodCount(count);
orderdetail.setOrderId(orderId);
orderDetailService.add(orderdetail);
}
uri = "/app/detail/clientOrderList.jsp";
return uri;
}
//在后台查看订单的详细情况
public Object getOrderDetail(HttpServletRequest request, HttpServletResponse response) {
Object uri = null;
String id=request.getParameter("orderId");
List<OrderDetail> list=null;
if(id!=null&&!id.isEmpty()){
list=orderDetailService.findByOrderId(Integer.parseInt(id));
}
request.setAttribute("orderDetail", list);
uri=request.getRequestDispatcher("/sys/order/orderDetail.jsp");
return uri;
}
//后台结账(1.账单orderStatus由0改成1 2.改桌子的状态为0)
public Object pay(HttpServletRequest request, HttpServletResponse response) {
Object uri = null;
//修改订单
String oid=request.getParameter("orderId");
Orders order=new Orders();
order.setId(Integer.parseInt(oid));
order.setOrderStatus(1);
ordersService.update(order);
//修改桌子
String tid=request.getParameter("tableId");
if(tid!=null){
tableService.quitTable(Integer.parseInt(tid));
}
uri=request.getRequestDispatcher("sys/order/orderList.jsp");
return null;
}
}