Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2.  
  3. String strAction = request.getParameter("action");
  4.  
  5. if(strAction!=null && !strAction.equals("")) {
  6.  
  7. if(strAction.equals("add")) {
  8.  
  9. addToCart(request);
  10.  
  11.  
  12.  
  13.  
  14. } else if (strAction.equals("Update")) {
  15.  
  16. updateCart(request);
  17.  
  18.  
  19. } else if (strAction.equals("Delete")) {
  20.  
  21. deleteCart(request);
  22.  
  23.  
  24. }
  25.  
  26.  
  27. }
  28.  
  29. HttpSession session = request.getSession();
  30.  
  31. Cart cart =(Cart) session.getAttribute("cart");
  32.  
  33.  
  34. int cartSize = cart.getCartItems().size();
  35. response.getWriter().print(cartSize);
  36. }
  37.  
  38. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  39. doGet(request, response);
  40. }
  41.  
  42. protected void deleteCart(HttpServletRequest request) {
  43.  
  44. HttpSession session = request.getSession();
  45.  
  46. String strId = request.getParameter("id");
  47.  
  48. Cart cartBean = null;
  49.  
  50.  
  51. Object objCartBean = session.getAttribute("cart");
  52.  
  53. if(objCartBean!=null) {
  54.  
  55. cartBean = (Cart) objCartBean ;
  56.  
  57. } else {
  58.  
  59. cartBean = new Cart();
  60.  
  61. }
  62.  
  63. cartBean.deleteCartItem(strId);
  64.  
  65. }
  66.  
  67.  
  68. protected void updateCart(HttpServletRequest request) {
  69.  
  70. HttpSession session = request.getSession();
  71. String strQuantity = request.getParameter("quantity");
  72. String strId = request.getParameter("id");
  73. String size = request.getParameter("size");
  74. Cart cartBean = null;
  75. Object objCartBean = session.getAttribute("cart");
  76.  
  77. if(objCartBean!=null) {
  78.  
  79. cartBean = (Cart) objCartBean ;
  80.  
  81. } else {
  82.  
  83. cartBean = new Cart();
  84.  
  85. }
  86.  
  87. cartBean.updateCartItem(strId, strQuantity,size);
  88.  
  89. }
  90.  
  91.  
  92. protected void addToCart(HttpServletRequest request) {
  93.  
  94. HttpSession session = request.getSession();
  95.  
  96. String strId = request.getParameter("id");
  97. String strQuantity = request.getParameter("quantity");
  98. String size = request.getParameter("size");
  99. Cart cartBean = null;
  100.  
  101. Object objCartBean = session.getAttribute("cart");
  102.  
  103. if(objCartBean!=null) {
  104.  
  105. cartBean = (Cart) objCartBean ;
  106.  
  107. } else {
  108.  
  109. cartBean = new Cart();
  110.  
  111. session.setAttribute("cart", cartBean);
  112. }
  113.  
  114. ArrayList<Product> alCartItems = cartBean.getCartItems();
  115.  
  116. int flag = 0;
  117.  
  118. for(int i = 0 ; i < alCartItems.size() ; i++ ){
  119. if(alCartItems.get(i).getId() == Integer.parseInt(strId) && alCartItems.get(i).getSize().equalsIgnoreCase(size)){
  120. flag = 1;
  121.  
  122. int qtyNew = alCartItems.get(i).getQuantity() + Integer.parseInt(strQuantity);
  123.  
  124. cartBean.updateCartItem(String.valueOf(strId), String.valueOf(qtyNew),size);
  125. }
  126. }
  127.  
  128. if(flag == 0){
  129. try{
  130. Product cartItem = new Product();
  131. int idProduct = Integer.parseInt(strId);
  132. cartItem = model.doRetrieveByKey(idProduct);
  133.  
  134. cartBean.addCartItem(cartItem, strQuantity,size);
  135.  
  136. } catch (SQLException e) {
  137. System.out.println("Error:" + e.getMessage());
  138. }
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement