SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <%@ page import="java.sql.*" %>
  2. <%@ page import="java.text.NumberFormat" %>
  3. <%@ page import="java.util.HashMap" %>
  4. <%@ page import="java.util.Iterator" %>
  5. <%@ page import="java.util.ArrayList" %>
  6. <%@ page import="java.util.Map" %>
  7. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF8"%>
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>Nick and Jiv's Grocery Order Processing</title>
  12.    <link href="css/bootstrap.min.css" rel="stylesheet">
  13.    <style type="text/css">
  14.         table, th, td{
  15.         border: 1px solid;
  16.         padding: 1%;
  17.         width: 400px;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.  
  23. <%
  24. // Get customer id
  25. String custId = request.getParameter("customerId");
  26. String password = request.getParameter("password");
  27. @SuppressWarnings({"unchecked"})
  28. HashMap<String, ArrayList<Object>> productList = (HashMap<String, ArrayList<Object>>) session.getAttribute("productList");
  29.  
  30. NumberFormat currFormat = NumberFormat.getCurrencyInstance();
  31.  
  32. // Make connection
  33.  
  34. String url = "jdbc:sqlserver://sql04.ok.ubc.ca:1433;DatabaseName=db_nmcgee;";
  35. String uid = "nmcgee";
  36. String pw = "75488726";
  37.  
  38. System.out.println("Connecting to database.");
  39.  
  40. try ( Connection con = DriverManager.getConnection(url, uid, pw); ) {
  41.     // Save order information to database
  42.     String SQL = "";
  43.     PreparedStatement pstmt;
  44.     ResultSet rst;
  45.     int testId = -1;
  46.    
  47.     // Determine if valid customer id was entered
  48.     // Determine if there are products in the shopping cart
  49.     // If either are not true, display an error message
  50.     try {
  51.         testId = Integer.parseInt(custId);
  52.        
  53.         SQL = "SELECT * "
  54.              +"FROM customer "
  55.              +"WHERE customerId = ? and password = ?";
  56.         pstmt = con.prepareStatement(SQL);
  57.         pstmt.setInt(1, testId);
  58.         pstmt.setString(2, password);
  59.         rst = pstmt.executeQuery();
  60.        
  61.         if(!rst.next()){
  62.             out.println("<tr><td><h1>Invalid customer id or password. Go back to the previous page and try again</h1><");
  63.             return;
  64.         }
  65.     }
  66.     catch (Exception e) {
  67.         out.println("<h1>Invalid customer id or password. Go back to the previous page and try again</h1>");
  68.         return;
  69.     }
  70.    
  71.    Iterator<Map.Entry<String, ArrayList<Object>>> iterator1 = productList.entrySet().iterator();
  72.    if (!iterator1.hasNext()) {
  73.         out.println("<h1>Empty Cart. Go back and add some items to your cart.</h1>");
  74.    }
  75.    
  76.     // Create new order summary containing customer id and date.
  77.     SQL = "INSERT INTO ordersummary (customerId, orderDate)" +
  78.           "VALUES (?, ?)";
  79.    pstmt = con.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
  80.    
  81.     pstmt.setInt(1, Integer.parseInt(custId));                                          // customerId
  82.     pstmt.setTimestamp(2, java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()));   // date time
  83.     pstmt.executeUpdate();
  84.    
  85.     // auto generate a unique orderId key
  86.    ResultSet keys = pstmt.getGeneratedKeys();
  87.    keys.next();
  88.    int orderId = keys.getInt(1);
  89.        
  90.     // Create new orderproduct
  91.    SQL = "INSERT INTO orderproduct VALUES(?, ?, ?, ?)";
  92.    pstmt = con.prepareStatement(SQL);
  93.    
  94.    // initialize values
  95.    double pr = 0;
  96.    int qty = 0;
  97.    String productId = "";
  98.    String productName = "";
  99.    double total = 0;
  100.    
  101.     out.println("<h1>Order Summary</h1>");
  102.     out.print("<th><table><tr><th>Product Id</th>");
  103.     out.println("<th>Product Name</th>");
  104.     out.println("<th>Quantity</th>");
  105.     out.println("<th>Price</th>");
  106.    out.println("<th>Subtotal</th></tr>");
  107.    
  108.    // Each entry in the HashMap is an ArrayList with item 0-id, 1-name, 2-quantity, 3-price
  109.    Iterator<Map.Entry<String, ArrayList<Object>>> iterator2 = productList.entrySet().iterator();
  110.    while (iterator2.hasNext())
  111.    {
  112.        Map.Entry<String, ArrayList<Object>> entry = iterator2.next();
  113.        ArrayList<Object> product = (ArrayList<Object>) entry.getValue();
  114.        
  115.        // grabs values from shopping cart
  116.        productId = (String) product.get(0);
  117.        productName = (String) product.get(1);
  118.        String price = (String) product.get(2);
  119.        pr = Double.parseDouble(price);
  120.        qty = ((Integer)product.get(3)).intValue();
  121.        
  122.        double subtotal = pr * qty;
  123.        total += subtotal;
  124.        
  125.        // Insert each item into OrderProduct table using OrderId from previous INSERT
  126.        pstmt.setInt(1, orderId);
  127.        pstmt.setInt(2, Integer.parseInt(productId));
  128.        pstmt.setInt(3, qty);
  129.        pstmt.setDouble(4, pr);
  130.        
  131.        pstmt.executeUpdate();
  132.        
  133.        // Output product
  134.         out.println("<tr><td>" + productId + "</td>"
  135.                    +"<td>" + productName + "</td>"
  136.                    +"<td>" + qty + "</td>"
  137.                    +"<td align=\"right\">" +  currFormat.format(pr) + "</td>"
  138.                    +"<td align=\"right\">" + currFormat.format(subtotal) + "</td></tr>");
  139.    }
  140.    
  141.    out.println("<tr><td colspan=\"4\" align=\"right\"><b>Order Total</b></td>"
  142.             +"<td align=\"right\">"+currFormat.format(total)+"</td></tr>");
  143.     out.println("</table>");
  144.    
  145.    out.println("<h1>Order Complete. Will be shipped soon...");
  146.    out.println("<h1>Your order reference number is: " + orderId + "</h1>");
  147.    
  148.     // get name of customer for printing
  149.     SQL = "SELECT customerId, firstName, lastName " + "FROM customer "
  150.                + "WHERE customerId LIKE ?";
  151.     pstmt = con.prepareStatement(SQL);
  152.     pstmt.setString(1, "%" + custId + "%");
  153.     rst = pstmt.executeQuery();
  154.    
  155.     rst.next();
  156.    out.println("<tr><td><a href = order.jsp?customerId=" + rst.getInt(1) + "></a>" +
  157.                 "<h1>Shipping to customer: " + rst.getInt(1) + " Name: " + rst.getString(2) + " " + rst.getString(3) +
  158.                 "</h1><h2><a href = index.jsp>Return to shopping</a></h2></td></tr>");
  159.    
  160.    System.out.println(total);
  161.    
  162.    SQL = "UPDATE ordersummary "
  163.          +"SET totalAmount = ? "
  164.         +"WHERE orderId = ?";
  165.    pstmt = con.prepareStatement(SQL);
  166.     pstmt.setDouble(1, total);
  167.     pstmt.setInt(2, orderId);
  168.     pstmt.executeUpdate();
  169.    
  170.     con.close();
  171.    
  172.     // Clear cart if order placed successfully
  173.     Iterator<Map.Entry<String, ArrayList<Object>>> iterator3 = productList.entrySet().iterator();
  174.    while (iterator3.hasNext()) {
  175.        Map.Entry<String, ArrayList<Object>> entry = iterator3.next();
  176.         iterator3.remove();
  177.    }
  178. }
  179.  
  180. catch (SQLException ex) {
  181.     System.err.println("SQLException: " + ex);
  182. }
  183.  
  184. %>
  185. </BODY>
  186. </HTML>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top