Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. public class allItemsServlet extends HttpServlet {
  2.  
  3.     @EJB
  4.     ItemsBeanLocal itembean;
  5.    
  6.     @Override
  7.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  8.             throws ServletException, IOException {
  9.            
  10.             List<Items> items = itembean.findAll();
  11.            
  12.             PrintWriter out = response.getWriter();
  13.             out.println("<html>");
  14.             out.println("<head>");
  15.             out.println("<title>Shopping Cart</title>");
  16.             out.println("</head>");
  17.             out.println("<body>");
  18.             out.println("<form action=allItemsServlet method=GET>");
  19.             out.println("<table>");
  20.             out.println("<tr><td>Search By:</td><td><input type=text name=searchValue></td><td><input type=submit value=Search /></tr>");
  21.             out.println("</table>");
  22.             out.println("<form action=allItemsServlet method=POST>");            
  23.             out.println("<input type=hidden name=action value=add>");
  24.             out.println("<table>");
  25.             out.println("<tr><td><b><u>Item</u></b></td><td><b><u>Quantity</u></b></td></tr>");
  26.             int count = 0;
  27.             String search = request.getParameter("searchValue");
  28.            
  29.             for (int i = 0; i < items.size(); i++)
  30.             {
  31.                 if(search == null)
  32.                 {
  33.                     if (i>=0)
  34.                         out.println("</tr><tr>");
  35.            
  36.                         out.println("<td>" + items.get(i).getCardname() + ":</td><td><input type=text name=item" + (i+1) + " value=0 size=3 />/" + items.get(i).getCardquantity() + "</td>");
  37.                         count++;
  38.                 }
  39.                
  40.                 else
  41.                 {
  42.                     if(items.get(i).getCardname().contains(search))
  43.                     {
  44.                         if (i>=0)
  45.                         out.println("</tr><tr>");
  46.                        
  47.                         out.println("<td>" + items.get(i).getCardname() + ":</td><td><input type=text name=item" + (i+1) + " value=0 size=3 />/" + items.get(i).getCardquantity() + "</td>");
  48.                         count++;
  49.                     }                    
  50.                 }
  51.             }
  52.            
  53.             out.println("<tr><td colspan=2><input type=submit></td></tr>");
  54.             out.println("<input type=hidden name=count value=" + count + " />");
  55.             out.println("</table></form>");
  56.             out.println("</body>");
  57.             out.println("</html>");
  58.          
  59.     }
  60.  
  61.     @Override
  62.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  63.             throws ServletException, IOException {
  64.        
  65.         HttpSession session = request.getSession();
  66.         cartBeanLocal shoppingCart = (cartBeanLocal) session.getAttribute("cart");
  67.         if (shoppingCart == null) {
  68.  
  69.             try
  70.             {
  71.                 System.out.println("request new bean\n");
  72.                 Context c = new InitialContext();
  73.                 shoppingCart = (cartBeanLocal) c.lookup("java:global/12150142_v2/12150142_v2-ejb/cartBean!appBeans.cartBeanLocal");
  74.                 session.setAttribute("cart", shoppingCart);
  75.             }
  76.            
  77.             catch (NamingException ne)
  78.             {
  79.                 throw new ServletException(ne);
  80.             }
  81.         }
  82.  
  83.         PrintWriter out = response.getWriter();
  84.         out.println("<html>");
  85.         out.println("<head>");
  86.         out.println("<title>Shopping Cart</title>");
  87.         out.println("</head>");
  88.         out.println("<body>");
  89.  
  90.         String action = request.getParameter("action");
  91.         if (action != null && action.equals("add"))
  92.         {
  93.             int count = 10; //Integer.parseInt(request.getParameter("count"));
  94.             List<Items> list = new ArrayList<Items>();
  95.             List<Integer> values = new ArrayList<Integer>();
  96.            
  97.             int i = 1;
  98.             while(i <= count)
  99.             {
  100.                 list.add(itembean.find(i));
  101.                 values.add(Integer.parseInt(request.getParameter("item" + i)));
  102.                 i++;
  103.             }
  104.            
  105.             for(int j = 0; j < list.size(); j++)
  106.                 shoppingCart.addItem(list.get(j).getCardname(), values.get(j));
  107.  
  108.             out.println(shoppingCart.getItemList());
  109.             out.println("<h2><a href=allItemsServlet>Add more Items</a></h2>");
  110.             out.println("<form action=ShoppingCartServlet method=POST>");
  111.             out.println("<input type=hidden name=action value=cancel>");
  112.             out.println("<input type=submit value=Cancel>");
  113.             out.println("</form><br>");
  114.             out.println("<form action=allItemsServlet method=POST>");
  115.             out.println("<input type=hidden name=action value=checkout>");
  116.             out.println("<input type=submit value=Checkout>");
  117.             out.println("</form>");
  118.  
  119.         }
  120.        
  121.         else if (action != null && action.equals("cancel"))
  122.         {
  123.             out.println("<h2>Order cancelled</h2>");
  124.             shoppingCart.cancel();
  125.             session.removeAttribute("cart");
  126.         }
  127.        
  128.         else if (action != null && action.equals("checkout"))
  129.         {
  130.             out.println("<h2>You checked out the following:</h2>");
  131.             HashMap<String, Integer>  items = shoppingCart.getItems();
  132.             List<Items> list = new ArrayList<Items>();
  133.             Set<String> keys = items.keySet();
  134.             Iterator<String> it = keys.iterator();
  135.             String k;
  136.             while (it.hasNext())
  137.             {
  138.                 k = it.next();
  139.                 if(items.get(k) > 0)
  140.                 {
  141.                     Items item = itembean.getItemByName(k);
  142.                     itembean.editAmount(item.getCardid(), items.get(k));
  143.                 }  
  144.             }
  145.  
  146.             out.println(shoppingCart.checkout());
  147.             session.removeAttribute("cart");
  148.         }
  149.        
  150.         out.println("<h2><a href=index.jsp>Back to start</a></h2>");
  151.         out.println("</body>");
  152.         out.println("</html>");
  153.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement