BasketFrontController.java
의 doProcess()의 주소비교 후 처리부분에 코드 추가
1 2 3 4 5 6 7 8 9 10
| if(command.equals("/BasketAdd.ba")){ System.out.println("C: /BasketAdd.ba 호출"); action = new BasketAddAction(); try { forward = action.execute(request, response); } catch (Exception e) { e.printStackTrace(); } }
|
BasketAddAction.java
생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public class BasketAddAction implements Action {
@Override public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("M : BasketAddAction의 execute() 호출"); HttpSession session = request.getSession(); String id = (String) session.getAttribute("id"); ActionForward forward = new ActionForward(); if(id == null){ forward.setPath("./MemberLogin.me"); forward.setRedirect(false); return forward; } request.setCharacterEncoding("UTF-8"); BasketDTO bdto = new BasketDTO(); bdto.setB_g_num(Integer.parseInt(request.getParameter("gno"))); bdto.setB_g_amount(Integer.parseInt(request.getParameter("amount"))); bdto.setB_g_size(request.getParameter("size")); bdto.setB_g_color(request.getParameter("color")); bdto.setB_m_id(id); System.out.println("장바구니 객체정보: "+bdto); BasketDAO bdao = new BasketDAO(); int result = bdao.checkGoods(bdto); if(result != 1){ bdao.basketAdd(bdto); } forward.setPath("./BasketList.ba"); forward.setRedirect(true); return forward; } }
|
BasketDAO.java
에 checkGoods()메서드 코드 추가
이미 장바구니에 담았던 상품을 또 장바구니담기를 선택한 경우 모든 옵션이 같다면 수량을 1 증가시키는 메서드를 구현해보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public int checkGoods(BasketDTO bkdto) { int result = 0;
try{ getCon(); sql ="select * from itwill_basket " + "where b_m_id=? and b_g_num=? and b_g_size=? and b_g_color=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, bkdto.getB_m_id()); pstmt.setInt(2, bkdto.getB_g_num()); pstmt.setString(3, bkdto.getB_g_size()); pstmt.setString(4, bkdto.getB_g_color()); rs = pstmt.executeQuery(); if(rs.next()){ result = 1; sql ="update itwill_basket set b_g_amount=b_g_amount+? " + "where b_m_id=? and b_g_num=? and " + "b_g_size=? and b_g_color=?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, bkdto.getB_g_amount()); pstmt.setString(2, bkdto.getB_m_id()); pstmt.setInt(3, bkdto.getB_g_num()); pstmt.setString(4, bkdto.getB_g_size()); pstmt.setString(5, bkdto.getB_g_color()); pstmt.executeUpdate(); System.out.println("기존의 상품에 수량 변경완료!"); } System.out.println("기존의 상품 확인 결과: "+(result==1? "중복상품이 있다":"중복상품이 없다")); sql = "insert into itwill_basket"; }catch(Exception e){ e.printStackTrace(); }finally { closeDB(); } return result; }
|
BasketDAO.java
에 basketAdd()메서드 코드 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public void basketAdd(BasketDTO bkdto) { int b_num = 0; try{ getCon(); sql = "select max(b_num) from itwill_basket"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ b_num = rs.getInt(1)+1; } System.out.println("DAO : b_num은 "+b_num);
sql = "insert into itwill_basket values(?,?,?,?,?," + "?,now())"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, b_num); pstmt.setString(2, bkdto.getB_m_id()); pstmt.setInt(3, bkdto.getB_g_num()); pstmt.setInt(4, bkdto.getB_g_amount()); pstmt.setString(5, bkdto.getB_g_size()); pstmt.setString(6, bkdto.getB_g_color()); pstmt.executeUpdate(); System.out.println("장바구니 담기 성공"); }catch(Exception e){ e.printStackTrace(); }finally { closeDB(); } }
|