This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
261 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/com/bakaqc/flower/controller/CartCookieController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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 com.bakaqc.flower.controller; | ||
|
||
import com.bakaqc.flower.dao.ProductDAO; | ||
import com.bakaqc.flower.model.Item; | ||
import com.bakaqc.flower.model.Product; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
/** | ||
* | ||
* @author ACER | ||
*/ | ||
@WebServlet(name = "CartCookieController", urlPatterns = {"/cookie"}) | ||
public class CartCookieController extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
request.setCharacterEncoding("UTF-8"); | ||
response.setContentType("text/html; charset=UTF-8"); | ||
|
||
String action = request.getParameter("action"); | ||
if (action == null) { | ||
doGet_DisplayCart(request, response); | ||
} else { | ||
if (action.equalsIgnoreCase("buy")) { | ||
doGet_Buy(request, response); | ||
} else if (action.equalsIgnoreCase("remove")) { | ||
doGet_Remove(request, response); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
protected void doGet_DisplayCart(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
request.getRequestDispatcher("/view/cart.jsp").forward(request, response); | ||
} | ||
|
||
protected void doGet_Remove(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
HttpSession session = request.getSession(); | ||
List<Item> cart = (List<Item>) session.getAttribute("cart"); | ||
int index = isExisting(request.getParameter("id"), cart); | ||
cart.remove(index); | ||
session.setAttribute("cart", cart); | ||
response.sendRedirect("cart"); | ||
} | ||
|
||
protected void doGet_Buy(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
ProductDAO productDao = new ProductDAO(); | ||
HttpSession session = request.getSession(); | ||
if (session.getAttribute("cart") == null) { | ||
List<Item> cart = new ArrayList<>(); | ||
cart.add(new Item((Product) productDao.getById(request.getParameter("id")), 1)); | ||
session.setAttribute("cart", cart); | ||
} else { | ||
List<Item> cart = (List<Item>) session.getAttribute("cart"); | ||
int index = isExisting(request.getParameter("id"), cart); | ||
if (index == -1) { | ||
cart.add(new Item((Product) productDao.getById(request.getParameter("id")), 1)); | ||
} else { | ||
int quantity = cart.get(index).getQuantity() + 1; | ||
cart.get(index).setQuantity(quantity); | ||
} | ||
session.setAttribute("cart", cart); | ||
} | ||
response.sendRedirect("cart"); | ||
} | ||
|
||
private int isExisting(String id, List<Item> cart) { | ||
for (int i = 0; i < cart.size(); i++) { | ||
if (id.equals(cart.get(i).getProduct().getId())) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.