Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package assignment;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. import javax.servlet.ServletConfig;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.ServletRequest;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import javax.servlet.http.HttpSession;
  21. import javax.websocket.Session;
  22.  
  23. import assignment.ItemModel;;
  24.  
  25. @WebServlet("/ShoppingCart")
  26. public class ShoppingCart extends HttpServlet {
  27. private static final long serialVersionUID = 1L;
  28.  
  29. public ShoppingCart() {
  30. super();
  31. }
  32. int numberOfItems = 0;
  33. //int totalPrice = 0;
  34.  
  35. public void init(ServletConfig config) throws ServletException {
  36. super.init(config);
  37. try {
  38. Class.forName("com.mysql.jdbc.Driver");
  39. } catch (ClassNotFoundException e) {
  40. throw new ServletException(e);
  41. }
  42. }
  43. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  44.  
  45. List<ItemModel> cartItems = new ArrayList<ItemModel>();
  46.  
  47. cartItems = (List<ItemModel>)request.getSession().getAttribute("cartItems");
  48. for (ItemModel cart : cartItems) {
  49. System.out.println("cart quantity: "+cart.getQuantity());
  50. }
  51.  
  52. request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  53. }
  54.  
  55. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  56. List<ItemModel> cartItems;
  57.  
  58. Double price = Double.parseDouble(request.getParameter("price"));
  59. int quantity = Integer.valueOf(request.getParameter("quantity"));
  60. System.out.println(quantity);
  61. System.out.println(price);
  62. Integer id = Integer.valueOf(request.getParameter("id"));
  63. String name = request.getParameter("name");
  64. String details="";
  65.  
  66. Double eachTotal= 0.0;
  67. eachTotal = quantity*price;
  68. ItemModel cart = new ItemModel(id, name, price, quantity, details);
  69. cart.setId(id);
  70. cart.setName(name);
  71. cart.setPrice(price);
  72. cart.setQuantity(quantity);
  73. cart.setDetails(details);
  74. cart.setEachTotal(eachTotal);
  75.  
  76. Connection c = null;
  77. try
  78. {
  79. String url = "jdbc:mysql://localhost/roughdb";
  80. String username = "root";
  81. String password = "mypass";
  82. /* String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  83. String username = "cs3220stu38";
  84. String password = "w5woPb7Z"; */
  85.  
  86. //List<ItemModel> items = new ArrayList<ItemModel>();
  87. c = DriverManager.getConnection( url, username, password );
  88.  
  89. //String sql = "insert into shopping_cart (name, quantity, price, each_total) values (?,?,?,?) where id = ?";
  90. String sql = "INSERT INTO shopping_cart(id, name, quantity, price, each_total) VALUES(?, ?, ?, ?, ?)";
  91.  
  92. PreparedStatement pstmt = c.prepareStatement(sql);
  93.  
  94.  
  95. //totalPrice += cart.getQuantity()*cart.getPrice();
  96. //totalPrice += cart.getEachTotal();
  97.  
  98. pstmt.setInt(1, id);
  99. pstmt.setString(2, name);
  100. pstmt.setInt(3, quantity);
  101. pstmt.setDouble(4, price);
  102. pstmt.setDouble(5, quantity*price);
  103. pstmt.executeUpdate();
  104. }
  105. catch( SQLException e )
  106. {
  107. throw new ServletException( e );
  108. }
  109. finally
  110. {
  111. try
  112. {
  113. if( c != null ) c.close();
  114. }
  115. catch( SQLException e )
  116. {
  117. throw new ServletException( e );
  118. }
  119. }
  120.  
  121. cartItems = (List<ItemModel>)request.getSession().getAttribute("cartItems");
  122.  
  123. if(cartItems == null){
  124. cartItems = new ArrayList<ItemModel>();
  125. cartItems.add(cart);
  126. request.getSession().setAttribute("cartItems", cartItems);
  127. }else{
  128. cartItems.add(cart);
  129. request.getSession().setAttribute("cartItems", cartItems);
  130. }
  131. numberOfItems = cartItems.size();
  132. request.getSession().setAttribute("quantity", quantity);
  133. request.getSession().setAttribute("id", id);
  134. request.getSession().setAttribute("numberOfItems", numberOfItems);
  135. request.getSession().setAttribute("eachTotal", eachTotal);
  136. int totalPrice=0;
  137. for (ItemModel itemModel : cartItems) {
  138. totalPrice += itemModel.getQuantity()*itemModel.getPrice();
  139. }
  140.  
  141. request.getSession().setAttribute("total", totalPrice);
  142. request.getSession().setAttribute("cartItems", cartItems);
  143. response.sendRedirect("ShoppingCart");
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement