Advertisement
Guest User

Untitled

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