| <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
| pageEncoding="ISO-8859-1"%>
|
| <%@ page import="com.entity.Customer" %>
|
| <%@ page import="java.sql.PreparedStatement" %>
|
| <%@ page import="com.helper.DBHelper" %>
|
| <%@ page import="java.sql.*" %>
|
| <html lang="en">
|
| <head>
|
| <meta charset="UTF-8">
|
| <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| <title>Shopping Cart</title>
|
| <style>
|
| @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap");
|
| * {
|
| margin: 0;
|
| padding: 0;
|
| box-sizing: border-box;
|
| font-family: "Poppins", sans-serif;
|
| }
|
| body {
|
| background-color: #ffffff;
|
| color: #333;
|
| }
|
| nav {
|
| background-color: #ff6f00;
|
| color: #fff;
|
| padding: 15px 20px;
|
| display: flex;
|
| justify-content: space-between;
|
| align-items: center;
|
| box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
| }
|
| nav .logo {
|
| font-size: 24px;
|
| font-weight: 600;
|
| }
|
| .logo a {
|
| text-decoration: none;
|
| color: inherit;
|
| font-weight: bold;
|
| font-size: 1.5rem;
|
| }
|
| nav ul {
|
| list-style: none;
|
| display: flex;
|
| gap: 20px;
|
| }
|
| nav ul li a {
|
| color: #fff;
|
| text-decoration: none;
|
| font-weight: 500;
|
| transition: color 0.3s ease;
|
| }
|
| nav ul li a:hover {
|
| color: #fdd835;
|
| }
|
| header {
|
| text-align: center;
|
| margin: 20px 0;
|
| font-size: 28px;
|
| font-weight: 500;
|
| }
|
| .cart-grid {
|
| display: grid;
|
| grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| gap: 20px;
|
| padding: 20px;
|
| max-width: 1200px;
|
| margin: 0 auto;
|
| }
|
| .cart-card {
|
| background-color: #f9f9f9;
|
| border: 1px solid #ddd;
|
| border-radius: 10px;
|
| padding: 15px;
|
| text-align: center;
|
| box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
| transition: transform 0.3s ease, box-shadow 0.3s ease;
|
| }
|
| .cart-card:hover {
|
| transform: translateY(-5px);
|
| box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
|
| }
|
| .cart-card img {
|
| width: 63%;
|
| height: 200px;
|
| object-fit: cover;
|
| border-radius: 5px;
|
| margin-bottom: 15px;
|
| }
|
| .cart-card h3 {
|
| font-size: 18px;
|
| margin: 10px 0;
|
| }
|
| .quantity-controls {
|
| display: flex;
|
| justify-content: center;
|
| align-items: center;
|
| margin: 10px 0;
|
| }
|
| .quantity-controls button {
|
| background-color: #ff6f00;
|
| color: #fff;
|
| border: none;
|
| border-radius: 5px;
|
| padding: 5px 10px;
|
| font-size: 14px;
|
| cursor: pointer;
|
| margin: 0 5px;
|
| }
|
| .quantity-controls button:hover {
|
| background-color: #e65c00;
|
| }
|
| .quantity-controls span {
|
| font-size: 16px;
|
| margin: 0 10px;
|
| }
|
| .confirm-button {
|
| background-color: #4caf50;
|
| color: #fff;
|
| border: none;
|
| border-radius: 5px;
|
| padding: 10px 15px;
|
| font-size: 14px;
|
| cursor: pointer;
|
| margin-top: 10px;
|
| transition: background-color 0.3s ease;
|
| }
|
| .confirm-button:hover {
|
| background-color: #45a049;
|
| }
|
| </style>
|
| </head>
|
| <body>
|
| <%
|
| if (session.getAttribute("customer") != null) {
|
| Customer customer = (Customer) session.getAttribute("customer");
|
| %>
|
| <jsp:include page="./commons/publicNavBar.jsp"/>
|
| <header>Welcome, <%= customer.getName() %> to your Shopping Cart</header>
|
| <section class="cart-grid">
|
| <%
|
| String sql = "SELECT * FROM CART WHERE customerId = ?";
|
| try (PreparedStatement pstmt = DBHelper.getPreparedStatement(sql)) {
|
| pstmt.setInt(1, customer.getCustomerId());
|
| ResultSet rs = pstmt.executeQuery();
|
|
|
| while (rs.next()) {
|
| int productId = rs.getInt("productId");
|
| String productQuery = "SELECT * FROM PRODUCTS WHERE productId = ?";
|
| try (PreparedStatement productStmt = DBHelper.getPreparedStatement(productQuery)) {
|
| productStmt.setInt(1, productId);
|
| ResultSet productRs = productStmt.executeQuery();
|
| if (productRs.next()) {
|
| %>
|
| <div class="cart-card">
|
| <img src="<%= productRs.getString("imgUrl") %>" alt="Product Image">
|
| <h3><%= productRs.getString("productName") %></h3>
|
| <h4><%= productRs.getString("productDescription") %></h4>
|
| <p><%= productRs.getDouble("productPrice") %> per kg</p>
|
| <div class="quantity-controls">
|
| <button onclick="decrementQuantity(<%= productId %>, this)">-</button>
|
| <span id="quantity-<%= productId %>"><%= rs.getInt("quantity") %></span>
|
| <button onclick="incrementQuantity(<%= productId %>, this)">+</button>
|
| </div>
|
| <button class="confirm-button" onclick="updateCart(<%= productId %>, <%= rs.getInt("quantity") %>)">Update Cart</button>
|
| </div>
|
| <%
|
| }
|
| }
|
| }
|
| } catch (SQLException e) {
|
| out.println("Error: " + e.getMessage());
|
| }
|
| %>
|
| </section>
|
| <%
|
| } else {
|
| response.sendRedirect("LoginPage.jsp");
|
| }
|
| %>
|
| <script>
|
| function incrementQuantity(productId, button) {
|
| const quantitySpan = document.getElementById(`quantity-${productId}`);
|
| let quantity = parseInt(quantitySpan.textContent) || 0;
|
| quantity++;
|
| quantitySpan.textContent = quantity;
|
| }
|
|
|
| function decrementQuantity(productId, button) {
|
| const quantitySpan = document.getElementById(`quantity-${productId}`);
|
| let quantity = parseInt(quantitySpan.textContent) || 0;
|
| if (quantity > 0) {
|
| quantity--;
|
| quantitySpan.textContent = quantity;
|
| }
|
| }
|
|
|
| function updateCart(productId, quantity) {
|
| const newQuantity = document.getElementById(`quantity-${productId}`).textContent;
|
| if (newQuantity > 0) {
|
| // Use an AJAX request to update the quantity in the cart
|
| const xhr = new XMLHttpRequest();
|
| xhr.open("POST", "UpdateCartServlet", true);
|
| xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
| xhr.onreadystatechange = function() {
|
| if (xhr.readyState === 4 && xhr.status === 200) {
|
| alert("Cart updated successfully!");
|
| }
|
| };
|
| xhr.send(`productId=${productId}&quantity=${newQuantity}`);
|
| } else {
|
| alert("Quantity must be at least 1.");
|
| }
|
| }
|
| </script>
|
| </body>
|
| </html>
|
|
|
|
|
|
|
|
|
|
|
|
|
| @WebServlet("/UpdateCartServlet")
|
| public class UpdateCartServlet extends HttpServlet {
|
| private static final long serialVersionUID = 1L;
|
|
|
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
| int productId = Integer.parseInt(request.getParameter("productId"));
|
| int quantity = Integer.parseInt(request.getParameter("quantity"));
|
|
|
| // Get the customer ID from the session
|
| HttpSession session = request.getSession();
|
| Customer customer = (Customer) session.getAttribute("customer");
|
| if (customer != null) {
|
| int customerId = customer.getCustomerId();
|
|
|
| String updateQuery = "UPDATE CART SET quantity = ? WHERE productId = ? AND customerId = ?";
|
| try (Connection conn = DBHelper.getConnection();
|
| PreparedStatement pstmt = conn.prepareStatement(updateQuery)) {
|
| pstmt.setInt(1, quantity);
|
| pstmt.setInt(2, productId);
|
| pstmt.setInt(3, customerId);
|
| int rowsUpdated = pstmt.executeUpdate();
|
|
|
| if (rowsUpdated > 0) {
|
| response.setStatus(HttpServletResponse.SC_OK);
|
| } else {
|
| response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
| }
|
| } catch (SQLException e) {
|
| response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
| e.printStackTrace();
|
| }
|
| } else {
|
| response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
| }
|
| }
|
| }
|