📄 modifybilldishdo.java
字号:
package controller.bill;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.DealDish;
import beans.Bill;
import beans.Dish;
import beans.DishNumber;
import beans.Order;
import beans.User;
public class ModifyBillDishDo extends HttpServlet {
/**
* Constructor of the object.
*/
public ModifyBillDishDo() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("GBK");
HttpSession mySession = request.getSession();
String action = request.getParameter("action");
int type = 0;
if(mySession.getAttribute("type")!= null){
type = Integer.parseInt(mySession.getAttribute("type").toString());
}
if(type != 2){
//跳转至主页面
mySession.setAttribute("errorMsg", "只能服务员能够操作订单");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/mian.jsp");
rd.forward(request, response);
return;
}
if(action != null){
if(action.equals("addDishes")){
doAddDishes(request,response);
return;
}
if(action.equals("modifyDishes")){
doModifyDishes(request,response);
return;
}
if(action.equals("modifyDishesOK")){
doModifyDishesOK(request,response);
return;
}
}
mySession.setAttribute("errorMsg", "错误操作");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/mian.jsp");
rd.forward(request, response);
return;
}
private void doModifyDishesOK(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(billInfor ==null){
billInfor = new Hashtable();
}
Bill billItem = (Bill)billInfor.get("bill");
if(billItem == null){
billItem = new Bill();
}
Enumeration aa = request.getParameterNames();
//获取菜的份数
while(aa.hasMoreElements()){
String temp = aa.nextElement().toString();
if(temp.equals("action")){
continue;
}
Dish add = new Dish();
add.getMainInfo(temp);
int dishNumber = 0;
dishNumber = Integer.parseInt(request.getParameter(temp).toString());
if(dishNumber != 0){
billItem.addDishes(add, dishNumber);
continue;
}
billItem.deleteDishes(add);
}
billInfor.put("action", "modify");
billInfor.put("bill", billItem);
mySession.setAttribute("billInfor", billInfor);
mySession.setAttribute("errorMsg", "");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBill.jsp");
rd.forward(request, response);
return;
}
private void doModifyDishes(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession mySession = request.getSession();
DealDish temp = new DealDish();
String errorMsg = temp.getDishes();
Vector dishes = temp.getVector();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(billInfor == null){
billInfor = new Hashtable();
}
Bill tempBill = (Bill)billInfor.get("bill");
Vector addDishesNumber = new Vector();
if(tempBill == null){
tempBill = new Bill();
billInfor.put("bill", tempBill);
billInfor.put("action", "modify");
mySession.setAttribute("billInfor", billInfor);
}
for(int i = 0 ; i <dishes.size();i++){
Dish changeDish = (Dish)dishes.get(i);
DishNumber addDish = new DishNumber();
addDish.setDish(changeDish);
addDish.setNumber(tempBill.queryDish(changeDish.getDishID()));
addDishesNumber.add(addDish);
}
//转发请求
if(errorMsg == null){
request.setAttribute("allDish", addDishesNumber);
mySession.setAttribute("errorMsg", errorMsg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/showDishes.jsp");
rd.forward(request, response);
return;
}else{
mySession.setAttribute("errorMsg", errorMsg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
rd.forward(request, response);
return;
}
}
private void doAddDishes(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
DealDish temp = new DealDish();
String errorMsg = temp.getDishes();
Vector dishes = temp.getVector();
Vector addDishes = new Vector();
for(int i = 0 ;i<dishes.size();i++){
Dish getDish = (Dish)dishes.get(i);
DishNumber add = new DishNumber();
add.setDish(getDish);
add.setNumber(0);
addDishes.add(add);
}
request.setAttribute("allDish", addDishes);
Hashtable billInfor = new Hashtable();
Bill billItem = new Bill();
billItem.setBeginTime(new Date());
User waiter = new User();
waiter.getWaiterData((String)mySession.getAttribute("username"));
billItem.setWaiter(waiter);
billInfor.put("bill", billItem);
billInfor.put("action", "modify");
if(errorMsg == null){
mySession.setAttribute("billInfor", billInfor);
mySession.setAttribute("errorMsg", errorMsg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/showDishes.jsp");
rd.forward(request, response);
return;
}else{
mySession.setAttribute("errorMsg", errorMsg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
rd.forward(request, response);
return;
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -