본문 바로가기

JSP/Shopping Cart 06

Shopping Cart 06

장바구니 보기 페이지에서 이용자는 특정 항목의 수량을 다시 수정하고 '장바구니 새로고침' 버튼을 눌러 장바구니에 반영할 수 있게 한다.

cartView.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<%@ page import="java.util.*, shopping.*" %>
<jsp:useBean id="cart" class="shopping.Cart" scope="session"/>
<html>
<head>
<title>장바구니 보기</title>
<script>
function submit(name){
 ok = confirm("정말로 삭제하시겠습니까?");
 if(ok) {
  document.removeForm.name.value=name;
  document.removeForm.submit();
 }
}
function submit2(name, qty){
 ok = confirm("정말로 수정하시겠습니까?");
 if(ok) {
  document.updateForm.name.value=name;
  document.updateForm.qty.value=document.getElementById(qty).value;
  document.updateForm.submit();
 }
}
</script>

<style type="text/css">
.textbox {
 border:1px solid;
 width:30px;
}
</style>

</head>
<body><p><center>
장바구니 내용 보기<hr width=60%>
<div style="font-size:65%;">
<table border=0><tr><td bgcolor="orange">
<table border=0 cellspacing=0 cellpadding=3 bgcolor="white" style="font-size:65%">
<%
 Vector<Goods> v = cart.getGoods();
 String bgColor = null;
 java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
 
 for(int i=0;i<v.size();i++){
  if(i%2==0)bgColor="FFFFCC";
  else bgColor="FFFFFF";
 %>
  <tr bgColor="<%=bgColor%>"><th align=right>상품명</th><td align=left><%=v.get(i).getName()%>
  <input type="button" value="삭제" onClick="submit('<%= v.get(i).getName()%>');"></td></tr>
  <tr bgColor="<%=bgColor%>"><th align=right>제조사</th><td align=left><%=v.get(i).getCompany()%></td></tr>
  <tr bgColor="<%=bgColor%>"><th align=right>단가</th><td align=left><%=nf.format(v.get(i).getPrice())%></td></tr>
  <tr bgColor="<%=bgColor%>"><th align=right>수량</th><td align=left>
  <input class="textbox" type="text" id="qty<%=i%>" value="<%=v.get(i).getQty()%>">
  <input type="button" value="새로고침" onClick="submit2('<%=v.get(i).getName()%>','<%="qty"+i%>');">
  </td></tr>
  <tr bgColor="<%=bgColor%>"><th align=right>단가x수량</th><td align=left><%=nf.format(v.get(i).getSumPrice())%></td></tr>
  <tr><td colspan=2><hr></td></tr>
<%}%>
  <tr><th align=right>총액</th><th align=left><%=nf.format(cart.getTotalPrice())%>원</th></tr>
</table></tr></table>

<a href="notebook.jsp">노트북</a> | <a href="navi.jsp">네비게이션</a> | <a href="mp3.jsp">MP3</a><p>
<a href="#" onClick="if(confirm('정말로 비우시겠어요?')) location.href='cartRemoveAll.jsp';">장바구니 비우기</a>
</div>
</center>
<!-- 삭제 버튼을 누를 때 동적으로 값이 할당되는 폼 -->
<form action="cartRemove.jsp" method="post" name="removeForm">
<input type="hidden" name="name">
</form>
<!-- '장바구니새로고침' 버튼을 누를 때 동적으로 값이 할당되는 폼 -->
<form name="updateForm" action="cartUpdate.jsp" method="post">
<input type="hidden" name="name">
<input type="hidden" name="qty">
</form>

</body>
</html>


cartUpdate.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<jsp:useBean id="cart" class="shopping.Cart" scope="session"/>
<%
 String name = request.getParameter("name");
 String qty = request.getParameter("qty");
 if(name==null || name.equals("") || qty==null || qty.equals("")){  %>
  <script>
   alert("'장바구니 새로고침' 버튼을 눌러 실행해 주세요");
   location.href="cartView.jsp";
  </script>
<%
  return;
 }
 cart.update(name, Integer.parseInt(qty));
 response.sendRedirect("cartView.jsp");
%>


Cart.java

package shopping;
import java.util.*;

public class Cart {
 
 Vector<Goods> v = new Vector<Goods>();
 
 public Cart() {}
 
 public void add(Goods g){
 boolean found = false;
 for(int i=0;i<v.size();i++){
  if(v.get(i).getName().equals(g.getName())){
   v.get(i).setQty(v.get(i).getQty()+g.getQty());
   found=true;
   return;
  }
 }
 if(!found) v.add(g);
 }
 
 public void deleteAll(){
  v.removeAllElements();
 }
 
 public void remove(String name){
 for(int i=0;i<v.size();i++){
     if(v.get(i).getName().equals(name)){
      v.remove(i);
     }
 } 
 }

 public void update(String name, int qty){
  for(int i=0;i<v.size();i++){
   if(v.get(i).getName().equals(name)){
    v.get(i).setQty(qty);
   }
  }
 }
 
 public int getTotalPrice(){
  int total=0;
  for(int i=0;i<v.size();i++){
   total += v.get(i).getPrice()*v.get(i).getQty();
  }
  return total;
 }
 
 public Vector<Goods> getGoods(){
  return v;
 }
}