[Servlet] μλ° μλΈλ¦Ώμ νμ©ν μΌν μΉ΄νΈ μν μλ μ‘°μ κΈ°λ₯ ꡬν π
β μΉμ¬μ΄νΈμ λ°μμ±
• μ¬μ©μ κ²½νμ κ·Ήλννλ μμ μ€ νλ
→ μ¬μ©μκ° μν μλμ λ³κ²½ν λλ§λ€ νμ΄μ§λ₯Ό μλ‘κ³ μΉ¨νμ§ μκ³ λ λ³κ²½ μ¬νμ΄ μ¦μ λ°μ
→ μ¬μ©μλ λ λΉ λ₯΄κ³ μννκ² μ§ν
1οΈβ£ κΈ°μ μ€ν
• μ£Όμ κΈ°μ μ μλ° μλΈλ¦Ώ
→ μλ° μλΈλ¦Ώμ μλ² μ¬μ΄λμμ μ€νλλ μλ° νλ‘κ·Έλ¨
→ ν΄λΌμ΄μΈνΈμ μμ²μ μ²λ¦¬νκ³ μλ΅μ λλ €μ£Όλ μν
2οΈβ£ μμ² νλΌλ―Έν° μ²λ¦¬
• cartProductNumber : μνμ κ³ μ λ²νΈ
• productCount : μ‘°μ νκ³ μ νλ μλ
• cartProductCondition : μλ μ‘°μ λ°©ν₯ (upCartProductCount λλ downCartProductCount)
int cartProductNumber = Integer.parseInt(request.getParameter("cartProductNumber"));
int newProductCount = Integer.parseInt(request.getParameter("productCount"));
String cartProductCondition = request.getParameter("cartProductCondition");
3οΈβ£ μν μλ μ‘°μ λ‘μ§
• cartProductConditionμ λ°λΌ μν μλμ μ¦κ°μν€κ±°λ κ°μμν€λ λ‘μ§μ μ€ν
→ upCartProductCountλ μλμ μ¦κ°
→ downCartProductCountλ μλμ κ°μ
→ ν΄λΉ μνμ μΌν μΉ΄νΈμμ μ°Ύμ μλμ μ‘°μ , κ²°κ³Όλ₯Ό ν΄λΌμ΄μΈνΈμκ² μ¦μ λ°μ
if ("upCartProductCount".equals(cartProductCondition)) {
// μλ μ¦κ° λ‘μ§
for (Map<String, Object> cartItem : shoppingCart) {
if ((int) cartItem.get("productNumber") == cartProductNumber) {
int currentCount = (int) cartItem.get("cartProductCount");
cartItem.put("cartProductCount", currentCount + newProductCount);
out.print("true");
out.flush();
break;
}
}
} else if ("downCartProductCount".equals(cartProductCondition)) {
// μλ κ°μ λ‘μ§
for (Map<String, Object> cartItem : shoppingCart) {
if ((int) cartItem.get("productNumber") == cartProductNumber) {
int currentCount = (int) cartItem.get("cartProductCount");
cartItem.put("cartProductCount", currentCount - newProductCount);
out.print("true");
out.flush();
break;
}
}
}
4οΈβ£ μΌν μΉ΄νΈ μ λ°μ΄νΈ λ‘μ§
μ¬μ©μμ μμ²μ λ°μ ν, μλΈλ¦Ώμ μΈμ μμ μΌν μΉ΄νΈλ₯Ό μ°Ύκ³ μν λ²νΈκ° μ‘΄μ¬νλμ§ νμΈ
→ λ§μ½ μ‘΄μ¬νκ³ μλμ μ°¨μ΄κ° μλ€λ©΄, μλ‘μ΄ μλμΌλ‘ μ λ°μ΄νΈ
HashMap<Integer, Integer> cart = (HashMap<Integer, Integer>) session.getAttribute("shoppingCart");
if (cart.containsKey(cartProductNumber) && cart.get(cartProductNumber) != newProductCount) {
cart.put(cartProductNumber, newProductCount);
}
5οΈβ£ κ²°κ³Ό λ°μ λ° μ¬μ©μ νΌλλ°±
• λ³κ²½λ κ²°κ³Όλ μ¦μ μ¬μ©μμκ² λ°μ
• μΆκ°μ μΈ νμ΄μ§ λ‘λ© μμ΄ μ΄λ£¨μ΄μ§ → μλΉμ€κ° λμ± λΉ λ₯΄κ³ λ°μμ