-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.jsp
177 lines (166 loc) · 7.33 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*, java.util.*, javax.servlet.http.*, javax.servlet.*" %>
<%
// Database connection
String dbURL = "jdbc:mysql://localhost:3306/u260447614_ab";
String dbUser = "u260447614_cd";
String dbPassword = "Midterm711";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
HttpSession session = request.getSession();
Integer userId = (Integer) session.getAttribute("user_id"); // User ID from session
String sessionToken = session.getId(); // Session token for non-logged-in users
if (userId == null) {
userId = 0; // Default to 0 if not logged in
}
List<Map<String, Object>> cartItems = new ArrayList<>();
double subtotal = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dbURL, dbUser, dbPassword);
// Fetch cart items
String sql = "SELECT c.id AS cart_id, p.name, p.price, p.image, c.quantity " +
"FROM cart c " +
"JOIN products p ON c.product_id = p.id " +
"WHERE c.user_id = ? OR c.session_token = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, userId);
stmt.setString(2, sessionToken);
rs = stmt.executeQuery();
while (rs.next()) {
Map<String, Object> item = new HashMap<>();
item.put("cart_id", rs.getInt("cart_id"));
item.put("name", rs.getString("name"));
item.put("price", rs.getDouble("price"));
item.put("image", rs.getString("image"));
item.put("quantity", rs.getInt("quantity"));
subtotal += rs.getDouble("price") * rs.getInt("quantity");
cartItems.add(item);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {}
if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Cart Page</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="<%= request.getContextPath() %>/design.css">
<style>
.quantity-container {
display: flex;
align-items: center;
}
.quantity-btn {
width: 30px;
height: 30px;
display: inline-flex;
justify-content: center;
align-items: center;
border: 1px solid #ddd;
cursor: pointer;
font-size: 16px;
background-color: #f0f0f0;
}
.quantity {
margin: 0 10px;
font-size: 16px;
}
.remove-btn {
background-color: #f00;
color: #fff;
border: none;
padding: 5px 10px;
cursor: pointer;
}
.cart-subtotal {
margin-top: 45px;
text-align: center;
}
</style>
</head>
<body>
<header>
<h1 class="header">SAVICLE</h1>
<nav>
<a href="<%= request.getContextPath() %>/index.jsp">Home</a>
<div class="dropdown">
<h2 class="dropbtn">Furniture</h2>
<div class="dropdown-content">
<a href="<%= request.getContextPath() %>/office.jsp">Office</a>
<a href="<%= request.getContextPath() %>/livingroom.jsp">Living Room</a>
<a href="<%= request.getContextPath() %>/diningroom.jsp">Dining Room</a>
<a href="<%= request.getContextPath() %>/bedroom.jsp">Bedroom</a>
<a href="<%= request.getContextPath() %>/bathroom.jsp">Bathroom</a>
</div>
</div>
<a href="<%= request.getContextPath() %>/wishlist.jsp">Wishlist</a>
<a href="<%= request.getContextPath() %>/cart.jsp" class="active">Cart</a>
<% if (session.getAttribute("user") != null) { %>
<a href="<%= request.getContextPath() %>/login-regis/logout.jsp" class="logout">Logout</a>
<% } else { %>
<a href="<%= request.getContextPath() %>/login-regis/login.jsp" class="login">Login</a>
<% } %>
</nav>
</header>
<h2 class="Title2">CART</h2>
<table class="cart-table">
<thead>
<tr>
<th>Item Image</th>
<th>Item Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% if (!cartItems.isEmpty()) { %>
<% for (Map<String, Object> item : cartItems) { %>
<tr>
<td>
<img src="<%= request.getContextPath() %>/admin/uploads/<%= item.get("image") %>" alt="<%= item.get("name") %>" style="width: 150px; height: auto;">
</td>
<td><%= item.get("name") %></td>
<td>Rp <%= String.format("%,.0f", item.get("price")) %></td>
<td>
<div class="quantity-container">
<form method="post" action="<%= request.getContextPath() %>/cart.jsp" class="quantity-form">
<input type="hidden" name="cart_id" value="<%= item.get("cart_id") %>">
<input type="hidden" name="action" value="decrease">
<button type="submit" class="quantity-btn">-</button>
</form>
<span class="quantity"><%= item.get("quantity") %></span>
<form method="post" action="<%= request.getContextPath() %>/cart.jsp" class="quantity-form">
<input type="hidden" name="cart_id" value="<%= item.get("cart_id") %>">
<input type="hidden" name="action" value="increase">
<button type="submit" class="quantity-btn">+</button>
</form>
</div>
</td>
<td>
<form method="post" action="<%= request.getContextPath() %>/cart.jsp">
<input type="hidden" name="remove_cart_id" value="<%= item.get("cart_id") %>">
<button type="submit" class="remove-btn">Remove</button>
</form>
</td>
</tr>
<% } %>
<% } else { %>
<tr>
<td colspan="5">Your cart is empty.</td>
</tr>
<% } %>
</tbody>
</table>
<div class="cart-subtotal">
<h3>Subtotal: Rp <%= String.format("%,.0f", subtotal) %></h3>
</div>
</body>
</html>