본문 바로가기
study/Spring

[Spring] 24. Spring (장바구니[ItemSet, Cart, cart, CartController])

by 금이패런츠 2022. 5. 6.
728x90
반응형

ItemSet.java

package logic;

public class ItemSet {
	private Item item;
	private Integer quantity;
	public ItemSet(Item item, Integer quantity) {
		this.item = item;
		this.quantity = quantity;
	}
	//getter, setter, toString
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	@Override
	public String toString() {
		return "ItemSet [item=" + item + ", quantity=" + quantity + "]";
	}
}

Cart.java

package logic;

import java.util.ArrayList;
import java.util.List;

public class Cart {
	private List<ItemSet> itemSetList = new ArrayList<>();
	//getter
	public List<ItemSet> getItemSetList() {
		return itemSetList;
	}
	//같은 상품인 경우 수량만 증가하도록 프로그램 수정
	public void push(ItemSet itemSet) {
		for(ItemSet is : itemSetList) {
			if(itemSet.getItem().getId() == is.getItem().getId()) {
				is.setQuantity(is.getQuantity() + itemSet.getQuantity());
				return;
			}
		}
		itemSetList.add(itemSet);
	}
}

cart.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- /springmvc1/src/main/webapp/WEB-INF/view/cart/cart.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>장바구니</title>
</head>
<body>
<h2>장바구니</h2>
<table>
	<tr>
		<td colspan="4">장바구니 상품 목록</td>
	</tr>
	<tr>
		<th>상품명</th>
		<th>가격</th>
		<th>수량</th>
		<th>합계</th>
	</tr>
	<c:set var="tot" value="${0}" />
	<c:forEach items="${cart.itemSetList}" var="itemSet" varStatus="stat">
		<tr>
			<td>${itemSet.item.name}</td>
			<td>${itemSet.item.price}</td>
			<td>${itemSet.quantity}</td>
			<td>
				${itemSet.quantity * itemSet.item.price}
				<c:set var="tot" value="${tot + (itemSet.quantity * itemSet.item.price)}" />
				<a href="cartDelete?index=${stat.index}">ⓧ</a>
			</td>
		</tr>
	</c:forEach>
	<tr>
		<td colspan="4" align="right">총 구입 금액 : ${tot}원</td>
	</tr>
</table>
<br>
${message}
<br>
<a href="../item/list">상품목록</a>
<a href="checkout">주문하기</a>
</body>
</html>

CartController.java

package controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import logic.Cart;
import logic.Item;
import logic.ItemSet;
import logic.Sale;
import logic.ShopService;
import logic.User;

@Controller
@RequestMapping("cart")
public class CartController {
	@Autowired
	private ShopService service;
	
	//http://localhost:8088/springmvc1/cart/cartAdd?id=1&quantity=5
	@RequestMapping("cartAdd")
	public ModelAndView add(Integer id, Integer quantity, HttpSession session) {
		//장바구니 정보를 session에 등록함
		ModelAndView mav = new ModelAndView("cart/cart");
		//1. id에 해당하는 상품 조회
		Item item = service.getItem(id);
		//2. session에 등록된 Cart 객체 조회
		Cart cart = (Cart)session.getAttribute("CART");
		if(cart == null) {
			cart = new Cart();
			session.setAttribute("CART", cart);
		}
		//같은 상품인 경우 수량만 증가하도록 프로그램 수정
		cart.push(new ItemSet(item, quantity));
		mav.addObject("cart", cart);
		mav.addObject("message", item.getName() + ":" + quantity + "개 장바구니 추가");
		return mav;
	}
	/*
	 * index 파라미터 : cart.itemSetList 객체의 순서
	 */
	@RequestMapping("cartDelete")
	public ModelAndView delete(int index, HttpSession session) {
		ModelAndView mav = new ModelAndView("cart/cart");
		Cart cart = (Cart)session.getAttribute("CART");
		//delSet : index에 해당하는 삭제된 객체
		ItemSet delSet = cart.getItemSetList().remove(index);
		mav.addObject("cart",cart);
		mav.addObject("message",delSet.getItem().getName() + "이(가) 장바구니에서 삭제");
		return mav;
	}
	@RequestMapping("cartView")
	public ModelAndView view(HttpSession session) {
		ModelAndView mav = new ModelAndView("cart/cart");
		Cart cart = (Cart)session.getAttribute("CART");
		mav.addObject("cart",cart);
		return mav;
	}
	//로그인이 되어야 실행가능하도록 AOP 부분 추가하기
	//장바구니에 주문상품이 없는 경우 실행 불가 AOP 부분 추가하기
	@RequestMapping("checkout")
	public String checkout(HttpSession session) { //CartAspect 클래스에서 설정한 pointcut 메서드
		return null;
	}
	/*
	 * 주문확정 : end 요청
	 * 1. 로그인, 장바구니상품 검증 필요 => aop로 설정
	 * 2. 장바구니 상품을 saleitem 테이블에 저장하기
	 * 3. 로그인 정보로 주문정보 (sale)테이블에 저장
	 * 4. 장바구니 상품 제거
	 * 5. 주문 정보 end.jsp
	 */
	@RequestMapping("end")
	public ModelAndView checkend(HttpSession session) {
		ModelAndView mav = new ModelAndView();
		Cart cart = (Cart)session.getAttribute("CART");
		User loginUser = (User)session.getAttribute("loginUser");
		Sale sale = service.checkend(loginUser, cart); // sale, saleitem 테이블에 데이터 저장
//		session.removeAttribute("CART");
		cart.getItemSetList().clear(); //장바구니 상품 제거
		mav.addObject("sale", sale);
		return mav;
	}
}
728x90
반응형