-
Notifications
You must be signed in to change notification settings - Fork 1
/
order.jsp
140 lines (123 loc) · 5 KB
/
order.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<%@ page import="java.sql.*,java.util.Locale" %>
<%@ page import="java.text.NumberFormat" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Map,java.math.BigDecimal" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="style.css" rel="stylesheet" type="text/css">
<title>Ray's Grocery Order Processing</title>
</head>
<body>
<%
// Get customer id
String custId = request.getParameter("customerId");
@SuppressWarnings({"unchecked"})
HashMap<String, ArrayList<Object>> productList = (HashMap<String, ArrayList<Object>>) session.getAttribute("productList");
if (custId == null || custId.equals(""))
out.println("<h1>Invalid customer id. Go back to the previous page and try again.</h1>");
else if (productList == null)
out.println("<h1>Your shopping cart is empty!</h1>");
else
{
// Check if customer id is a number
int num=-1;
try
{
num = Integer.parseInt(custId);
}
catch(Exception e)
{
out.println("<h1>Invalid customer id. Go back to the previous page and try again.</h1>");
return;
}
String sql = "SELECT customerId, firstName+' '+lastName FROM Customer WHERE customerId = ?";
String url = "jdbc:sqlserver://db:1433;DatabaseName=tempdb;";
String uid = "SA";
String pw = "YourStrong@Passw0rd";
NumberFormat currFormat = NumberFormat.getCurrencyInstance(Locale.US);
try
{ // Load driver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (java.lang.ClassNotFoundException e)
{
out.println("ClassNotFoundException: " +e);
}
try ( Connection con = DriverManager.getConnection(url, uid, pw);)
{
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
ResultSet rst = pstmt.executeQuery();
int orderId=0;
String custName = "";
if (!rst.next())
out.println("<h1>Invalid customer id. Go back to the previous page and try again.</h1>");
else
{ custName = rst.getString(2);
// Enter order information into database
sql = "INSERT INTO OrderSummary (customerId, totalAmount, orderDate) VALUES(?, 0, ?);";
// Retrieve auto-generated key for orderId
pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, num);
pstmt.setTimestamp(2, new java.sql.Timestamp(new Date().getTime()));
pstmt.executeUpdate();
ResultSet keys = pstmt.getGeneratedKeys();
keys.next();
orderId = keys.getInt(1);
out.println("<h1>Order Summary</h1>");
out.println("<table><tr><th>Product Id</th><th>Product Name</th><th>Quantity</th><th>Price</th><th>Subtotal</th></tr>");
double total =0;
Iterator<Map.Entry<String, ArrayList<Object>>> iterator = productList.entrySet().iterator();
while (iterator.hasNext())
{
Map.Entry<String, ArrayList<Object>> entry = iterator.next();
ArrayList<Object> product = (ArrayList<Object>) entry.getValue();
String productId = (String) product.get(0);
out.print("<tr><td>"+productId+"</td>");
out.print("<td>"+product.get(1)+"</td>");
out.print("<td align=\"center\">"+product.get(3)+"</td>");
String price = (String) product.get(2);
double pr = Double.parseDouble(price);
int qty = ( (Integer)product.get(3)).intValue();
out.print("<td align=\"right\">"+currFormat.format(pr)+"</td>");
out.print("<td align=\"right\">"+currFormat.format(pr*qty)+"</td></tr>");
out.println("</tr>");
total = total +pr*qty;
sql = "INSERT INTO OrderProduct (orderId, productId, quantity, price) VALUES(?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, orderId);
pstmt.setInt(2, Integer.parseInt(productId));
pstmt.setInt(3, qty);
pstmt.setString(4, price);
pstmt.executeUpdate();
}
out.println("<tr><td colspan=\"4\" align=\"right\"><b>Order Total</b></td>"
+"<td aling=\"right\">"+currFormat.format(total)+"</td></tr>");
out.println("</table>");
// Update order total
sql = "UPDATE OrderSummary SET totalAmount=? WHERE orderId=?";
pstmt = con.prepareStatement(sql);
pstmt.setDouble(1, total);
pstmt.setInt(2, orderId);
pstmt.executeUpdate();
out.println("<h2>Order completed. Will be shipped soon...</h2>");
out.println("<h2>Your order reference number is: "+orderId+"</h2>");
out.println("<h2>Shipping to customer: "+custId+" Name: "+custName+"</h2>");
out.println("<h2><a href=\"shop.html\">Return to shopping</a></h2>");
// Clear session variables (cart)
session.setAttribute("productList", null);
}
}
catch (SQLException ex)
{ out.println(ex);
}
}
%>
</body>
</html>