-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.jsp
125 lines (115 loc) · 4.73 KB
/
cart.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
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.text.NumberFormat" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Based Records</title>
<!-- Stylesheet -->
<link rel="stylesheet" href="styles.css" />
<!-- Font links -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700;900&display=swap"
rel="stylesheet"
/>
</head>
<!-------------------------------->
<!-- HEADER (Logo & Navigation) -->
<!-------------------------------->
<body>
<div class="header">
<div class="navbar">
<div class="logo">
<a href="index.jsp">
<img src="Assets/Based Records Logo.png" width="400px" />
</a>
</div>
<nav>
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="index.jsp#records">Shop</a></li>
<li><a href="listorder.jsp">Orders</a></li>
<li><a href="account.jsp"><%= session.getAttribute("userid") == null ? "Login" : session.getAttribute("userid") %></a></li>
</ul>
</nav>
<a href="cart.jsp">
<img src="Assets/shopping-cart.png" width="40px" height="40px" />
</a>
</div>
</div>
<!--------------------->
<!-- SHOPPING CART ---->
<!--------------------->
<%
// Get the current list of products
@SuppressWarnings({"unchecked"})
HashMap<String, ArrayList<Object>> productList = (HashMap<String, ArrayList<Object>>) session.getAttribute("productList");
if (productList == null)
{ out.println("<H1 class=\"heading\">Your shopping cart is empty!</H1>");
productList = new HashMap<String, ArrayList<Object>>();
}
else
{
NumberFormat currFormat = NumberFormat.getCurrencyInstance();
out.println("<h1 class=\"cart\" style=\"color: white\">Your Shopping Cart</h1>");
out.print("<div class=\"cart\"><table id=\"cart\"><tr><th>Product Id</th><th>Product Name</th><th>Price</th><th>Quantity</th><th>Subtotal</th><th>Remove</th>");
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();
if (product.size() < 4)
{
out.println("Expected product with four entries. Got: "+product);
continue;
}
// Setting the product ID and product Name
out.print("<tr><td>"+product.get(0)+"</td>");
out.print("<td>"+product.get(1)+"</td>");
Object price = product.get(2);
Object itemqty = product.get(3);
double pr = 0;
int qty = 0;
try
{
pr = Double.parseDouble(price.toString());
}
catch (Exception e)
{
out.println("Invalid price for product: "+product.get(0)+" price: "+price);
}
try
{
qty = Integer.parseInt(itemqty.toString());
}
catch (Exception e)
{
out.println("Invalid quantity for product: "+product.get(0)+" quantity: "+qty);
}
out.print("<td align=\"right\">"+currFormat.format(pr)+"</td>");
out.print("<td align=\"right\"><form method=\"get\" action=\"updateItem.jsp\"><input type=\"hidden\" id=\"id\" name=\"id\" value=" + product.get(0).toString() + "><input type=\"number\" min=1 name=\"quantity\" value=\"" + qty + "\"/><button type=\"submit\" value=\"Submit\">Update Quantity</button></form></td>");
out.print("<td align=\"right\">"+currFormat.format(pr*qty)+"</td>");
out.print("<td align=\"center\"><a href=\"removeItem.jsp?id=" + product.get(0) + "\"><img src=\"Assets/trash-can.png\" width=\"40px\" height=\"40px\"/></a></td></tr>");
out.println("</tr>");
total = total +pr*qty;
}
out.println("<tr><td colspan=\"4\" align=\"right\"><b>Order Total</b></td>"
+"<td align=\"right\">"+currFormat.format(total)+"</td></tr>");
out.println("</table></div>");
}
%>
<div class="end-cart-options">
<a href="order.jsp"><h1>Checkout</h1></a>
<h2>//</h2>
<a href="index.jsp"><h1>Continue Shopping</h1></a>
</div>
</body>
</html>