diff --git a/init/full.sql b/init/full.sql index 7786b4c7..ec020e99 100644 --- a/init/full.sql +++ b/init/full.sql @@ -22,7 +22,7 @@ create table Food ( food_status bit not null, food_rate tinyint not null, discount_percent tinyint not null, - food_img_url varchar(255) null, + food_img_url varchar(255) not null, food_type_id tinyint not null foreign key references FoodType(food_type_id) ); @@ -52,7 +52,11 @@ go create table Voucher ( voucher_id tinyint identity(1,1) not null primary key, voucher_name nvarchar(200) not null, - voucher_discount_percent tinyint not null + voucher_code char(16) not null, + voucher_discount_percent tinyint not null, + voucher_quantity tinyint not null, + voucher_status bit not null, + voucher_date datetime not null ); go @@ -378,12 +382,10 @@ insert into OrderStatus (order_status) values (N'Đã giao'); insert into OrderStatus (order_status) values (N'Đã hủy'); -- Voucher -insert into Voucher (voucher_name , voucher_discount_percent) values ( N'Quốc tế phụ nữ', 30); -insert into Voucher (voucher_name , voucher_discount_percent) values ( N'Khách hàng may mắn', 20); -insert into Voucher (voucher_name , voucher_discount_percent) values (N'Quà tặng Noel', 40); -insert into Voucher (voucher_name , voucher_discount_percent) values ( N'Người đặc biệt', 50); - - +insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values ( N'Quốc tế phụ nữ', 'ADASD2FD23123DBE', 30, 100, 0,'20231019 00:00:01 AM' ); +insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values ( N'Khách hàng may mắn', 'ADEF38BDYOQM875V', 20, 10, 0,'20230808 00:00:01 AM'); +insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values (N'Quà tặng Noel', 'DUEMAHWOPUNH62GH', 40, 50, 1,'20231215 00:00:01 AM' ); +insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values ( N'Người đặc biệt', 'DJWOA975N4B92BH6', 50, 5, 1,'20231108 00:00:01 AM' ); -- Cart, CartItem, Order test data insert into Cart (customer_id) values (1); diff --git a/src/main/java/Controllers/AdminController.java b/src/main/java/Controllers/AdminController.java index 772f82be..97f0048e 100644 --- a/src/main/java/Controllers/AdminController.java +++ b/src/main/java/Controllers/AdminController.java @@ -22,7 +22,10 @@ import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; +import java.sql.Timestamp; +import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -196,6 +199,7 @@ private void doPostUpdateUser(HttpServletRequest request, HttpServletResponse re int result = accountDAO.update(account); + if (result == 1) { response.sendRedirect("/admin"); return; @@ -209,10 +213,16 @@ private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse re throws ServletException, IOException { String voucherName = (String) request.getParameter("txtvoucher_name"); - byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtAddVoucher_discount_percent")); - + String voucherCode = (String) request.getParameter("txtvoucher_code"); + Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent")); + Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity")); + Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status")); + String datetimelocal = request.getParameter("txtvoucher_time"); + + Timestamp datetime = Timestamp.valueOf(datetimelocal.replace("T"," ")+":00"); + VoucherDAO voucherDAO = new VoucherDAO(); - Voucher voucher = new Voucher(voucherName, voucher_discount_percent); + Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime); int result = voucherDAO.add(voucher); @@ -227,12 +237,19 @@ private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse re private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id")); + Byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id")); String voucherName = (String) request.getParameter("txtvoucher_name"); - byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent")); - + String voucherCode = (String) request.getParameter("txtvoucher_code"); + Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent")); + Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity")); + Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status")); + + String datetimelocal = request.getParameter("txtvoucher_time"); + + Timestamp datetime = Timestamp.valueOf(datetimelocal.replace("T"," ")+":00"); + VoucherDAO voucherDAO = new VoucherDAO(); - Voucher voucher = new Voucher(voucherName, voucher_discount_percent); + Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime); voucher.setVoucherID(voucherID); int result = voucherDAO.update(voucher); diff --git a/src/main/java/Controllers/PromotionManagerController.java b/src/main/java/Controllers/PromotionManagerController.java index 18dd790c..e8d79b95 100644 --- a/src/main/java/Controllers/PromotionManagerController.java +++ b/src/main/java/Controllers/PromotionManagerController.java @@ -2,7 +2,6 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template */ - package Controllers; import DAOs.AccountDAO; @@ -19,34 +18,38 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.sql.Timestamp; +import java.time.LocalDateTime; import java.util.List; public class PromotionManagerController extends HttpServlet { - - /** - * Processes requests for both HTTP GET and POST methods. + + /** + * Processes requests for both HTTP GET and POST + * methods. + * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { + try ( PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println(""); - out.println("Servlet PromotionManagerController"); + out.println("Servlet PromotionManagerController"); out.println(""); out.println(""); - out.println("

Servlet PromotionManagerController at " + request.getContextPath () + "

"); + out.println("

Servlet PromotionManagerController at " + request.getContextPath() + "

"); out.println(""); out.println(""); } - } - + } + private void doGetVoucher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getRequestURI(); @@ -59,14 +62,22 @@ private void doGetVoucher(HttpServletRequest request, HttpServletResponse respon } response.sendRedirect("/promotionManager"); } - + private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String voucherName = (String) request.getParameter("txtvoucher_name"); - byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtAddVoucher_discount_percent")); + String voucherCode = (String) request.getParameter("txtvoucher_code"); + Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent")); + Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity")); + Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status")); + // Lấy thời gian hiện tại + LocalDateTime currentTime = LocalDateTime.now(); + // Chuyển đổi thời gian hiện tại thành Timestamp + Timestamp datetime = Timestamp.valueOf(currentTime); VoucherDAO voucherDAO = new VoucherDAO(); - Voucher voucher = new Voucher(voucherName, voucher_discount_percent); + Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime); int result = voucherDAO.add(voucher); @@ -78,15 +89,23 @@ private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse re return; } } - - private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse response) + + private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id")); + byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id")); String voucherName = (String) request.getParameter("txtvoucher_name"); - byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent")); + String voucherCode = (String) request.getParameter("txtvoucher_code"); + Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent")); + Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity")); + Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status")); + + // Lấy thời gian hiện tại + LocalDateTime currentTime = LocalDateTime.now(); + // Chuyển đổi thời gian hiện tại thành Timestamp + Timestamp datetime = Timestamp.valueOf(currentTime); VoucherDAO voucherDAO = new VoucherDAO(); - Voucher voucher = new Voucher(voucherName, voucher_discount_percent); + Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime); voucher.setVoucherID(voucherID); int result = voucherDAO.update(voucher); @@ -101,8 +120,9 @@ private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse } // - /** + /** * Handles the HTTP GET method. + * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs @@ -110,10 +130,10 @@ private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { String path = request.getRequestURI(); if (path.endsWith("/promotionManager")) { - + VoucherDAO voucherDAO = new VoucherDAO(); List voucherList = voucherDAO.getAllList(); @@ -127,10 +147,11 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) // response.setContentType("text/css"); request.getRequestDispatcher("/promotionManager.jsp").forward(request, response); } - } + } - /** + /** * Handles the HTTP POST method. + * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs @@ -138,9 +159,9 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { if (request.getParameter("btnSubmit") != null) { - switch (request.getParameter("btnSubmit")) { + switch (request.getParameter("btnSubmit")) { case "SubmitAddVoucher": doPostAddVoucher(request, response); break; @@ -153,8 +174,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) } } - /** + /** * Returns a short description of the servlet. + * * @return a String containing servlet description */ @Override diff --git a/src/main/java/DAOs/VoucherDAO.java b/src/main/java/DAOs/VoucherDAO.java index c589dd73..4816c799 100644 --- a/src/main/java/DAOs/VoucherDAO.java +++ b/src/main/java/DAOs/VoucherDAO.java @@ -43,7 +43,7 @@ public Voucher getVoucher(byte id) { ps.setByte(1, id); rs = ps.executeQuery(); if (rs.next()) { - voucher = new Voucher(rs.getByte("voucher_id"), rs.getString("voucher_name"), rs.getByte("voucher_discount_percent")); + voucher = new Voucher(rs.getByte("voucher_id"), rs.getString("voucher_name"), rs.getString("voucher_code"),rs.getByte("voucher_discount_percent"),rs.getByte("voucher_quantity"), rs.getByte("voucher_status"), rs.getTimestamp("voucher_date")); } } catch (SQLException ex) { Logger.getLogger(VoucherDAO.class.getName()).log(Level.SEVERE, null, ex); @@ -58,8 +58,13 @@ public List getAllList() { while (voucherRS.next()) { Voucher voucher = new Voucher( voucherRS.getByte("voucher_id"), - voucherRS.getString("voucher_name"), - voucherRS.getByte("voucher_discount_percent")); + voucherRS.getString("voucher_name"), + voucherRS.getString("voucher_code"), + voucherRS.getByte("voucher_discount_percent"), + voucherRS.getByte("voucher_quantity"), + voucherRS.getByte("voucher_status"), + voucherRS.getTimestamp("voucher_date") + ); voucherList.add(voucher); } } catch (SQLException ex) { @@ -69,12 +74,16 @@ public List getAllList() { } public int add(Voucher voucher) { - String sql = "insert into Voucher values (?,?)"; + String sql = "insert into Voucher values (?,?,?,?,?,?)"; int result = 0; try { PreparedStatement ps = conn.prepareStatement(sql); - ps.setString(1, voucher.getName()); - ps.setInt(2, voucher.getVoucher_discount_percent()); + ps.setString(1, voucher.getVoucher_name()); + ps.setString(2, voucher.getVoucher_code()); + ps.setByte(3, voucher.getVoucher_discount_percent()); + ps.setByte(4, voucher.getVoucher_quantity()); + ps.setByte(5, voucher.getVoucher_status()); + ps.setTimestamp(6, voucher.getVoucher_date()); result = ps.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(VoucherDAO.class.getName()).log(Level.SEVERE, null, ex); @@ -96,13 +105,17 @@ public int delete(byte id) { } public int update(Voucher voucher) { - String sql = "update Voucher set voucher_name = ?, voucher_discount_percent = ? where voucher_id = ?"; + String sql = "update Voucher set voucher_name = ?, voucher_code = ?,voucher_discount_percent = ?, voucher_quantity = ?, voucher_status = ?, voucher_date = ? where voucher_id = ?"; int result = 0; try { PreparedStatement ps = conn.prepareStatement(sql); - ps.setString(1, voucher.getName()); - ps.setByte(2, voucher.getVoucher_discount_percent()); - ps.setByte(3, voucher.getVoucherID()); + ps.setString(1, voucher.getVoucher_name()); + ps.setString(2, voucher.getVoucher_code()); + ps.setByte(3, voucher.getVoucher_discount_percent()); + ps.setByte(4, voucher.getVoucher_quantity()); + ps.setByte(5, voucher.getVoucher_status()); + ps.setTimestamp(6, voucher.getVoucher_date()); + ps.setByte(7, voucher.getVoucherID()); result = ps.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(VoucherDAO.class.getName()).log(Level.SEVERE, null, ex); diff --git a/src/main/java/Models/Voucher.java b/src/main/java/Models/Voucher.java index 5b743bef..8382f1a7 100644 --- a/src/main/java/Models/Voucher.java +++ b/src/main/java/Models/Voucher.java @@ -4,31 +4,50 @@ */ package Models; +import java.sql.Timestamp; +import java.util.Date; + public class Voucher { private byte voucherID; - private String name; + private String voucher_name; + private String voucher_code; private byte voucher_discount_percent; + private byte voucher_quantity; + private byte voucher_status; + private Timestamp voucher_date; public Voucher() { } - public Voucher(byte voucherID, String name, byte voucher_discount_percent) { + public Voucher(byte voucherID, String voucher_name, byte voucher_discount_percent) { this.voucherID = voucherID; - this.name = name; + this.voucher_name = voucher_name; this.voucher_discount_percent = voucher_discount_percent; } - public Voucher(String name, byte voucher_discount_percent) { - this.name = name; + public Voucher(String voucher_name, byte voucher_discount_percent) { + this.voucher_name = voucher_name; this.voucher_discount_percent = voucher_discount_percent; } - - public byte getVoucher_discount_percent() { - return voucher_discount_percent; + + public Voucher(String voucher_name, String voucher_code, byte voucher_discount_percent, byte voucher_quantity, byte voucher_status, Timestamp voucher_date) { + this.voucherID = voucherID; + this.voucher_name = voucher_name; + this.voucher_code = voucher_code; + this.voucher_discount_percent = voucher_discount_percent; + this.voucher_quantity = voucher_quantity; + this.voucher_status = voucher_status; + this.voucher_date = voucher_date; } - public void setVoucher_discount_percent(byte voucher_discount_percent) { + public Voucher(byte voucherID, String voucher_name, String voucher_code, byte voucher_discount_percent, byte voucher_quantity, byte voucher_status, Timestamp voucher_date) { + this.voucherID = voucherID; + this.voucher_name = voucher_name; + this.voucher_code = voucher_code; this.voucher_discount_percent = voucher_discount_percent; + this.voucher_quantity = voucher_quantity; + this.voucher_status = voucher_status; + this.voucher_date = voucher_date; } public byte getVoucherID() { @@ -39,13 +58,52 @@ public void setVoucherID(byte voucherID) { this.voucherID = voucherID; } - public String getName() { - return name; + public String getVoucher_name() { + return voucher_name; + } + + public void setVoucher_name(String voucher_name) { + this.voucher_name = voucher_name; + } + + public String getVoucher_code() { + return voucher_code; + } + + public void setVoucher_code(String voucher_code) { + this.voucher_code = voucher_code; + } + + public byte getVoucher_quantity() { + return voucher_quantity; + } + + public void setVoucher_quantity(byte voucher_quantity) { + this.voucher_quantity = voucher_quantity; + } + + public byte getVoucher_status() { + return voucher_status; + } + + public void setVoucher_status(byte voucher_status) { + this.voucher_status = voucher_status; + } + + public byte getVoucher_discount_percent() { + return voucher_discount_percent; + } + + public void setVoucher_discount_percent(byte voucher_discount_percent) { + this.voucher_discount_percent = voucher_discount_percent; } - public void setName(String name) { - this.name = name; + public Timestamp getVoucher_date() { + return voucher_date; } + public void setVoucher_date(Timestamp voucher_date) { + this.voucher_date = voucher_date; + } } diff --git a/src/main/webapp/WEB-INF/jspf/admin/components/addVoucher.jspf b/src/main/webapp/WEB-INF/jspf/admin/components/addVoucher.jspf index 4cc7182a..f7b1c4ef 100644 --- a/src/main/webapp/WEB-INF/jspf/admin/components/addVoucher.jspf +++ b/src/main/webapp/WEB-INF/jspf/admin/components/addVoucher.jspf @@ -1,35 +1,55 @@ <%-- any content can be specified here e.g.: --%> <%@ page pageEncoding="UTF-8" %> -