반응형
각 클라이언트의 요청을 처리하는 실질적인 비즈니스 로직이 구현되는 곳이다.
📌상품 등록 요청 처리 비즈니스 로직
ClothesRegistService.java
package service; import static db.JdbcUtil.*; import java.sql.Connection; import dao.ClothesDAO; import vo.Clothes; public class ClothesRegistService { public boolean registDog(Clothes cloth) { ClothesDAO clothesDAO = ClothesDAO.getInstance(); Connection con = getConnection(); clothesDAO.setConnection(con); boolean isRegistSuccess = false; //상품 등록 int insertCount = clothesDAO.insertClothes(cloth); if(insertCount>0){ commit(con); isRegistSuccess=true; }else{ rollback(con); } close(con); return isRegistSuccess; } }
📌상품 목록 보기 비즈니스 로직
ClothesListService.java
package service; import static db.JdbcUtil.*; import java.sql.Connection; import java.util.ArrayList; import dao.ClothesDAO; import vo.Clothes; public class ClothesListService { public ArrayList<Clothes> getClothesList() { ClothesDAO clothesDAO = ClothesDAO.getInstance(); Connection con = getConnection(); clothesDAO.setConnection(con); ArrayList<Clothes> clothesList = clothesDAO.selectClothesList(); close(con); return clothesList; } }
📌상품 상세 보기 비즈니스 로직
ClothesViewService.java
package service; import static db.JdbcUtil.*; import java.sql.Connection; import vo.Clothes; import dao.ClothesDAO; public class ClothesViewService { public Clothes getDogView(int id) { Connection con = getConnection(); ClothesDAO clothesDAO = ClothesDAO.getInstance(); clothesDAO.setConnection(con); //상품의 조회수를 증가 int updateCount = clothesDAO.updateReadCount(id); if(updateCount>0){ commit(con); }else{ rollback(con); } //id 값에 해당하는 상품 얻어오는 것. Clothes cloth = clothesDAO.selectClothes(id); close(con); return cloth; } }
📌장바구니 담기 요청(항목 추가) 비즈니스 로직
ClothesCartAddService.java
package service; import static db.JdbcUtil.*; import java.sql.Connection; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import vo.Cart; import vo.Clothes; import dao.ClothesDAO; public class ClothesCartAddService { public Clothes getCartClothes(int id) { Connection con = getConnection(); ClothesDAO clothesDAO = ClothesDAO.getInstance(); clothesDAO.setConnection(con); Clothes cloth = clothesDAO.selectDog(id); close(con); return cloth; } public void addCart(HttpServletRequest request, Clothes cartClothes) { //세션 영역에 저장되어 있는 장바구니 목록을 얻어오는 것 HttpSession session = request.getSession(); ArrayList<Cart> cartList = (ArrayList<Cart>)session.getAttribute("cartList"); //만약 세션 영역에 장바구니 목록 객체가 존재하지 않는다면(장바구니 요청을 처음 실행하는 경우) //장바구니 항목을 요소로 추가할 ArrayList 객체를 생성해 해당 객체를 세션 영역의 속성으로 공유 if(cartList == null){ cartList = new ArrayList<Cart>(); session.setAttribute("cartList", cartList); } //지금 장바구니에 담는 항목이 새로 추가되는 항목인지를 저장할 변수 boolean isNewCart = true; for (int i = 0; i < cartList.size(); i++) { if(cartClothes.getName().equals(cartList.get(i).getName())){ isNewCart = false; cartList.get(i).setQty(cartList.get(i).getQty()+1); break; } } if(isNewCart){ Cart cart = new Cart(); cart.setImage(cartClothes.getImage()); cart.setName(cartClothes.getName()); cart.setPrice(cartClothes.getPrice()); cart.setQty(1); cartList.add(cart); } } }
📌장바구니 목록 List 보기 비즈니스 로직
ClothesCartListService.java
package service; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import vo.Cart; public class ClothesCartListService { public ArrayList<Cart> getCartList(HttpServletRequest request) { HttpSession session = request.getSession(); ArrayList<Cart> cartList = (ArrayList<Cart>)session.getAttribute("cartList"); return cartList; } }
📌장바구니 삭제 요청 비즈니스 로직
ClothesCartRemoveService.java
package service; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import vo.Cart; public class ClothesCartRemoveService { public void cartRemove(HttpServletRequest request, String[] nameArray) { HttpSession session = request.getSession(); ArrayList<Cart> cartList = (ArrayList<Cart>)session.getAttribute("cartList"); for (int i = 0; i < nameArray.length; i++) { for (int j = 0; j < cartList.size(); j++) { if(cartList.get(j).getName().equals(nameArray[i])){ cartList.remove(cartList.get(j)); } } } } }
📌장바구니 항목 수량 증가/감소 비즈니스 로직
ClothesCartQtyUpService.java
package service; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import vo.Cart; public class ClothesCartQtyUpService { public void upCartQty(String name, HttpServletRequest request) { HttpSession session = request.getSession(); ArrayList<Cart> cartList = (ArrayList<Cart>)session.getAttribute("cartList"); for (int i = 0; i < cartList.size(); i++) { if(cartList.get(i).getName().equals(name)){ cartList.get(i).setQty(cartList.get(i).getQty()+1); } } } }
ClothesCartQtyDownService.java
package service; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import vo.Cart; public class ClothesCartQtyDownService { public void downCartQty(String name, HttpServletRequest request) { HttpSession session = request.getSession(); ArrayList<Cart> cartList = (ArrayList<Cart>)session.getAttribute("cartList"); for (int i = 0; i < cartList.size(); i++) { if(cartList.get(i).getName().equals(name)){ cartList.get(i).setQty(cartList.get(i).getQty()-1); } } } }
📌장바구니 목록에서 가격 범위 설정하여 상품 출력 비즈니스 로직
ClothesCartSearchService.java
package service; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import vo.Cart; public class ClothesCartSearchService { public ArrayList<Cart> getCartSearchList(int start_money, int end_money, HttpServletRequest request) { HttpSession session = request.getSession(); ArrayList<Cart> oldCartList = (ArrayList<Cart>)session.getAttribute("cartList"); ArrayList<Cart> cartList = new ArrayList<Cart>(); for (int i = 0; i < oldCartList.size(); i++) { if(oldCartList.get(i).getPrice()>=start_money && oldCartList.get(i).getPrice()<=end_money){ cartList.add(oldCartList.get(i)); } } return cartList; } }
반응형
'JSP & Servlet > 실습' 카테고리의 다른 글
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - View (0) | 2022.07.02 |
---|---|
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - DAO (0) | 2022.07.01 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - Action (0) | 2022.07.01 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - controller (0) | 2022.07.01 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - 테이블 생성 및 vo(Value Object) + 기본 세팅 (0) | 2022.07.01 |