📄 modifybilldo.java
字号:
package controller.bill;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
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.DealOrder;
import beans.Bill;
import beans.Order;
import beans.User;
public class ModifyBillDo extends HttpServlet {
/**
* Constructor of the object.
*/
public ModifyBillDo() {
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();
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("/main.jsp");
rd.forward(request, response);
return;
}
String action = request.getParameter("action");
if(action == null){
//没有给出主要信息,跳转至主页
mySession.setAttribute("errorMsg", "错误操作");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
rd.forward(request, response);
return;
}else{
if(action.equals("modifyOneBill")){
doModifyOneBill(request,response);
return;
}
if(action.equals("billOK")){
doBillOk(request,response);
return;
}
if(action.equals("checkout")){
doCheckout(request,response);
return;
}
if(action.equals("cancelBill")){
doCancelBill(request,response);
return;
}
if(action.equals("importOrder")){
doImportOrder(request,response);
return;
}
if(action.equals("importOrderOK")){
doImportOrderOK(request,response);
return;
}
}
mySession.setAttribute("errorMsg", "错误操作");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
rd.forward(request, response);
}
private void doImportOrderOK(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
String orderID = (String)request.getParameter("orderID");
Bill add = new Bill();
String errorMsg = add.importOrder(orderID);
if(errorMsg == null){
Hashtable billInfor = new Hashtable();
billInfor.put("action", "modify");
billInfor.put("bill", add);
billInfor.put("orderID", orderID);
mySession.setAttribute("billInfor", billInfor);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBill.jsp");
rd.forward(request, response);
return;
}else{
mySession.setAttribute("errorMsg", errorMsg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/addBill.jsp");
rd.forward(request, response);
return;
}
}
private void doImportOrder(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
if(validateOperation("不能执行新的而订单修改",request,response)){
DealOrder totalOrders = new DealOrder();
String errorMsg = totalOrders.generateOrders();
//设置request属性
mySession.setAttribute("errorMsg", errorMsg);
Vector temp = totalOrders.getOrders();
request.setAttribute("allOrdersInfor", temp);
//跳转至显示所有的order的页面
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/showOrders.jsp");
rd.forward(request, response);
return;
}
}
private void doCancelBill(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(billInfor != null){
billInfor.put("action", "ok");
}
mySession.setAttribute("billInfor", billInfor);
mySession.setAttribute("errorMsg", "操作成功");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
rd.forward(request, response);
}
private void doCheckout(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(billInfor != null){
Bill bill = (Bill)billInfor.get("bill");
if(bill != null){
int tableNumber = 0,peopleNumber = 0;
if(request.getParameter("tableNumber")!=null){
tableNumber = Integer.parseInt(request.getParameter("tableNumber").toString());
}
if(request.getParameter("peopleNumber")!=null){
peopleNumber = Integer.parseInt(request.getParameter("peopleNumber").toString());
}
bill.setNumberOfMan(peopleNumber);
bill.setTableNum(tableNumber);
bill.caculatePrice();
bill.setEndTime(new Date());
User waiter = new User();
String username = (String)mySession.getAttribute("username");
waiter.getWaiterData(username);
bill.setWaiter(waiter);
String billID = bill.getBillID();
//执行添加操作
String errorMsg = null;
if(billID == null||billID.equals("")){
errorMsg = bill.persist();
errorMsg = bill.checkOut();
}else{
errorMsg = bill.checkOut();
}
if(errorMsg == null || errorMsg.equals("")){
mySession.setAttribute("errorMsg", "成功修改");
}else{
//设置session
mySession.setAttribute("errorMsg", errorMsg);
}
billInfor.put("action", "checked");
mySession.setAttribute("billInfor", billInfor);
//删除订单
String orderID = (String)billInfor.get("orderID");
if(orderID != null){
Order delete = new Order();
delete.setOrderID(orderID);
delete.deleteOrder();
}
//执行完成开始跳转
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/billResult.jsp");
rd.forward(request, response);
return;
}
}
//有错误跳转
mySession.setAttribute("errorMsg", "有错误信息产生");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBill.jsp");
rd.forward(request, response);
}
private void doBillOk(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(billInfor != null){
Bill bill = (Bill)billInfor.get("bill");
if(bill != null){
int tableNumber = 0,peopleNumber = 0;
if(request.getParameter("tableNumber")!=null){
tableNumber = Integer.parseInt(request.getParameter("tableNumber").toString());
}
if(request.getParameter("peopleNumber")!=null){
peopleNumber = Integer.parseInt(request.getParameter("peopleNumber").toString());
}
bill.setNumberOfMan(peopleNumber);
bill.setTableNum(tableNumber);
bill.caculatePrice();
String billID = bill.getBillID();
String username = (String)mySession.getAttribute("username");
User waiter = new User();
waiter.getWaiterData(username);
bill.setWaiter(waiter);
//执行添加操作
String errorMsg = null;
if(billID == null||billID.equals("")){
errorMsg = bill.persist();
}else{
errorMsg = bill.update();
}
if(errorMsg == null || errorMsg.equals("")){
mySession.setAttribute("errorMsg", "成功修改");
}else{
//设置session
mySession.setAttribute("errorMsg", errorMsg);
}
billInfor.put("action", "ok");
mySession.setAttribute("billInfor", billInfor);
//执行完成开始跳转
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/billResult.jsp");
rd.forward(request, response);
return;
}
}
//有错误跳转
mySession.setAttribute("errorMsg", "有错误信息产生");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBill.jsp");
rd.forward(request, response);
return;
}
private void doModifyOneBill(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(validateOperation("不能执行新的而订单修改",request,response)){
String billID = request.getParameter("billID");
if(billID == null){
mySession.setAttribute("errorMsg", "请输入正确的ID名");
//跳转至主页面
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBillShow.jsp");
rd.forward(request, response);
return;
}
Bill temp = new Bill();
String errorMsg = temp.getData(billID);
mySession.setAttribute("errorMsg", errorMsg);
if(errorMsg == null){
int type = temp.getType();
if(type == 2){
mySession.setAttribute("errorMsg", "已经是账单,不能修改");
billInfor = new Hashtable();
billInfor.put("action", "checked");
billInfor.put("bill", temp);
mySession.setAttribute("billInfor", billInfor);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/billResult.jsp");
rd.forward(request, response);
return;
}
}
//设置billInfor
billInfor = new Hashtable();
billInfor.put("action", "modify");
billInfor.put("bill", temp);
mySession.setAttribute("billInfor", billInfor);
//跳转至修改订单信息页面
if(errorMsg == null){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBill.jsp");
rd.forward(request, response);
return;
}else{
RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
rd.forward(request, response);
return;
}
}
}
//判断能不能执行操作
private boolean validateOperation(String show,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
HttpSession mySession = request.getSession();
Hashtable billInfor = (Hashtable)mySession.getAttribute("billInfor");
if(billInfor != null){
String action = (String)billInfor.get("action");
if(action!=null){
if(action.equals("modify")){
mySession.setAttribute("errorMsg",show+", 请完成这个订单的填写");
//跳转至修改订单的信息一栏
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/modifyBill.jsp");
rd.forward(request, response);
return false;
}
if(action.equals("delete")){
mySession.setAttribute("errorMsg", show+",请完成删除这个订单");
//跳转至删除订单的信息一栏
RequestDispatcher rd = getServletContext().getRequestDispatcher("/waiter/bill/deleteBill.jsp");
rd.forward(request, response);
return false;
}
}
}
return true;
}
/**
* 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 + -