Advertisement
Guest User

Untitled

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