⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dishcontroller.java

📁 简单的餐厅管理系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			}
		}
		
	}
	//当点击主页上的修改菜时触发这个动作
	//已经完成功能
	private void doModifyDish(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
		if(dishInfor == null){
			mySession.setAttribute("errorMsg","");
			response.sendRedirect("../admin/modifyDish.jsp");
			return;
		}else{
			String action = (String)dishInfor.get("action");
			if(action!=null){
				if(action.equals("modify")){
					mySession.setAttribute("errorMsg", "不能修改菜。请完成修改这个菜");
					response.sendRedirect("../admin/showDishInfor.jsp");
					return;
				}else{
					mySession.setAttribute("errorMsg","");
					response.sendRedirect("../admin/modifyDish.jsp");
					return;
				}
			}else{
				mySession.setAttribute("errorMsg","");
				response.sendRedirect("../admin/modifyDish.jsp");
				return;
			}
		}
	}

	
	//当点击删除菜的信息时执行这个方法
	//没有工作
	private void doDeleteDish(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
		if(dishInfor == null){
			mySession.setAttribute("errorMsg","");
			response.sendRedirect("../admin/deleteDish.jsp");
			return;
		}else{
			String action = (String)dishInfor.get("action");
			if(action!=null){
				if(action.equals("modify")){
					mySession.setAttribute("errorMsg", "不能删除菜。请完成修改这个菜");
					response.sendRedirect("../admin/showDishInfor.jsp");
					return;
				}else{
					mySession.setAttribute("errorMsg","");
					response.sendRedirect("../admin/deleteDish.jsp");
					return;
				}
			}else{
				mySession.setAttribute("errorMsg","");
				response.sendRedirect("../admin/deleteDish.jsp");
				return;
			}
		}
	}
	
	
	//当点击添加主料信息时触发这个动作
	//已经完成功能
	private void doAddMaterial(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable materialsInfor = new Hashtable();
		materialsInfor.put("action", "addMaterial");
		DealMaterial temp = new DealMaterial();
		String errorMsg = temp.getMaterial();
		
		if(errorMsg == null){
			Vector materials = new Vector();
			int i = 0;
			while(temp.hasMore(i)){
				Material addMaterial = temp.getMaterial(i);
				addMaterial.setWeight(0);
				materials.add(addMaterial);
				i++;
			}
			Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
			if(dishInfor == null){
				dishInfor = new Hashtable();
			}
			dishInfor.put("action", "modify");
			Dish add = new Dish();
			dishInfor.put("dish", add);
			mySession.setAttribute("dishInfor", dishInfor);
			materialsInfor.put("materials", materials);
			mySession.setAttribute("errorMsg", null);
			mySession.setAttribute("materialsInfor", materialsInfor);
			response.sendRedirect("../admin/showMaterials.jsp");
		}else{
			mySession.setAttribute("errorMsg", errorMsg);
			response.sendRedirect("../admin/showDishInfor.Jsp");
		}
		
	}
	
	//修改主料信息的
	//完成功能
	//测试通过
	private void doModifyMaterial(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
		if(dishInfor == null){
			dishInfor = new Hashtable();
		}
			Dish modifyDish = (Dish)dishInfor.get("dish");
			if(modifyDish == null){
				modifyDish = new Dish();
				mySession.setAttribute("errorMsg", "系统出现错误!请重新添加");
				dishInfor.put("action", "new");
				dishInfor.put("dish", modifyDish);
				response.sendRedirect("../admin/showDishInfor.jsp");
				return;
			}else{
				DealMaterial temp = new DealMaterial();
				String errorMsg = temp.getMaterial();
				
				if(errorMsg == null){
					Vector materials = new Vector();
					int i = 0;
					while(temp.hasMore(i)){
						Material addMaterial = temp.getMaterial(i);
						addMaterial.setWeight(modifyDish.queryMaterial(addMaterial.getMaterialID()));
						materials.add(addMaterial);
						i++;
					}
					Hashtable materialsInfor = new Hashtable();//(Hashtable)mySession.getAttribute("materialsInfor");
					materialsInfor.put("action", "modifyMaterial");
					materialsInfor.put("materials", materials);
					mySession.setAttribute("errorMsg", null);
					mySession.setAttribute("materialsInfor", materialsInfor);
					response.sendRedirect("../admin/showMaterials.jsp");
				}else{
					mySession.setAttribute("errorMsg", errorMsg);
					response.sendRedirect("../admin/showDishInfor.jsp");
				}
			}
			
			
		}
		
	

	
	//添加材料确定按钮后触发的动作
	//完成功能,
	//测试通过
	private void doMaterialOK(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable materialsInfor = (Hashtable)mySession.getAttribute("materialsInfor");
		if(materialsInfor == null){
			mySession.setAttribute("errorMsg", "错误");
			response.sendRedirect("../servlet/showDishInfor.jsp");
			return;
		}else{
			Vector materials = (Vector)materialsInfor.get("materials");
			if(materials == null){
				mySession.setAttribute("errorMsg", "错误");
				response.sendRedirect("../servlet/showDishInfor.jsp");
				return;
			}else{
				Hashtable dishIn = (Hashtable)mySession.getAttribute("dishInfor");
				Dish dish;
				
				//获取dish 若获取不到则初始化dish
				if(dishIn != null){
					 dish = (Dish)dishIn.get("dish");
					if(dish == null){
						dish = new Dish();
					}
				}else{
					 dish = new Dish();
				}
				
				
				//修改dish的信息用于下一次显示
				for(int i=0; i < materials.size();i++){
					Material material = (Material)materials.elementAt(i);
					float weight = Float.parseFloat(request.getParameter(material.getMaterialID()));
					Material add = new Material();
					add.setMaterialID(material.getMaterialID());
					if(weight != 0){
						add.setName(material.getName());
						add.setWeight(weight);
						dish.addMaterial(add);
					}else{
						dish.deleteMaterial(add);
					}
				}
				
				//设置dish
				dishIn.put("dish", dish);
				
				//设置session的值
				
				mySession.setAttribute("dishInfor", dishIn);
				
				//跳转页面
				response.sendRedirect("../admin/showDishInfor.jsp");
			}
		}
		
	}

	
	//当执行添加dish按钮时,将添加的dish保存到数据库获取信息,没有获取最后的东西呢
	//暂时功能不完善,需要判断是添加新菜还是更改菜的信息 
	private void doOkDish(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
		if(dishInfor == null){
			mySession.setAttribute("errorMsg", "出现错误,不能添加成功");
			response.sendRedirect("../admin/showDishInfor.jsp");
			return;
		}else{
			Dish savedDish= (Dish)dishInfor.get("dish");
			
			String errorMsg = null;
			if(savedDish == null){
						mySession.setAttribute("errorMsg", "出现错误,不能添加成功");
				response.sendRedirect("../admin/showDishInfor.jsp");
				return;
			}else{
				String dishName,dishSort,type,description;
				float price;
				
				
				dishName = request.getParameter("dishName");
				if(dishName == null){
					dishName = "";
				}
				dishSort = request.getParameter("dishSort");
				if(dishSort == null){
					dishSort = "";
				}
				type = request.getParameter("type");
				if(type == null){
					type = "";
				}
				description = (String)request.getParameter("description");
				if(description == null){
					description = "";
				}
				savedDish.setDishName(dishName);
				savedDish.setDishSort(dishSort);
				savedDish.setType(type);
				savedDish.setDescription(description);
				try{
					price = Float.parseFloat(request.getParameter("price").toString());
				}catch(Exception e){
					price = 0;
					errorMsg = "错误的价钱";
					dishInfor.put("dish", savedDish);
					mySession.setAttribute("dishInfor", dishInfor);
					mySession.setAttribute("errorMsg", errorMsg);
					response.sendRedirect("../admin/showDishInfor.jsp");
					return;
				}
				savedDish.setPrice(price);
				if(savedDish.getDishID().equals("")){
					errorMsg = savedDish.persist();
				}else{
					errorMsg = savedDish.updateDish();
				}
				dishInfor.put("action", "ok");

				dishInfor.put("dish", savedDish);
				if(errorMsg != null){
					mySession.setAttribute("dishInfor", dishInfor);
					mySession.setAttribute("errorMsg", errorMsg);
					response.sendRedirect("../admin/showDishInfor.jsp");
				}else{
					dishInfor.put("action", "ok");
					mySession.setAttribute("dishInfor", dishInfor);
					mySession.setAttribute("errorMsg", "添加成功");

					response.sendRedirect("../admin/showDishInfor.jsp");
				}
			}

		}	
	}
	
	
	private void doCancelModifyDish(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		HttpSession mySession = request.getSession();
		Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
		if(dishInfor !=null){
			dishInfor.put("action", "cancel");
			dishInfor.put("dish", new Dish());
		}
		mySession.setAttribute("dishInfor", dishInfor);
		mySession.setAttribute("errorMsg", "取消操作");
		response.sendRedirect("../admin/caipu.jsp");
	}
	
	/**
	 * 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 {

		response.setContentType("text/html");
		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 + -