ํ์ ๊ธฐ๋ฅ ๊ตฌํ ์ดํ Enum, ๋๋ค, ์คํธ๋ฆผ์ ์ค์ ๋ก ์ ์ฉํด๋ณด์๋ค.
- ์ฅ๋ฐ๊ตฌ๋์ ์ฃผ๋ฌธ ํ๋ฆ์ ์ค์ ์๋น์ค์ฒ๋ผ ์ค๊ณํ๊ธฐ
- ๊ด๋ฆฌ์ ๋ชจ๋๋ฅผ ํตํ ์ํ ๊ด๋ฆฌ ๊ธฐ๋ฅ ์ถ๊ฐ
- Enum์ ํ์ฉํ ๊ณ ๊ฐ ๋ฑ๊ธ ๊ด๋ฆฌ
- ๋๋ค์ ์คํธ๋ฆผ์ ํ์ฉํ ๋ฐ์ดํฐ ์กฐํ ๋ฐ ๊ด๋ฆฌ
์๊ฐํด๋ณด๊ธฐ
CommerceSystem์ ๋ชจ๋ ๋ก์ง์ด ๋ชฐ๋ฆฌ์ง ์๋๋ก ํ๋ ๊ฒ ๋ชฉํ์๋ค.
์ฅ๋ฐ๊ตฌ๋ ๊ด๋ จ ๋ก์ง์ Cart๊ฐ ์ฑ
์์ง๋๋ก ๋ถ๋ฆฌํ๊ณ
์ํ ๊ด๋ฆฌ ๋ก์ง์ Category์ Product๊ฐ ๋ด๋นํ๋๋ก ํ๋ค.
Product ํด๋์ค
์ํ์ ๊ธฐ๋ณธ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด์ด๋ค.
์ํ๋ช
, ๊ฐ๊ฒฉ, ์ค๋ช
, ์ฌ๊ณ ์๋์ ํ๋๋ก ๊ฐ์ง๊ณ
์ฌ๊ณ ์ฐจ๊ฐ์ decreaseStock() ๋ฉ์๋๋ฅผ ํตํด์๋ง ๊ฐ๋ฅํ๋๋ก ์ ํํ๋ค.
public void decreaseStock(int quantity) {
if (stock < quantity) {
throw new IllegalArgumentException("์ฌ๊ณ ๊ฐ ๋ถ์กฑํฉ๋๋ค.");
}
stock -= quantity;
}
์ฌ๊ณ ๋ณ๊ฒฝ ๋ก์ง์ ํ ๊ณณ์ ๋ชจ์ ์๋ชป๋ ์ํ ๋ณ๊ฒฝ์ ๋ฐฉ์งํ๋ ค๊ณ ํ๋ค.
Product.java ์ ์ฒด ์ฝ๋
package kr.spartaclub.com.example.commerce;
public class Product {
private final String name;
private int price;
private String description;
private int stock;
public Product(String name, int price, String description, int stock) {
this.name = name;
this.price = price;
this.description = description;
this.stock = stock;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public String getDescription() {
return description;
}
public int getStock() {
return stock;
}
// ์ฃผ๋ฌธ ํ์ ์ ์ฌ๊ณ ๊ฐ์๋ฅผ ์ํ ๋ฉ์๋
public void decreaseStock(int quantity) {
if (quantity <= 0) {
throw new IllegalArgumentException("์ฐจ๊ฐ ์๋์ 0๋ณด๋ค ์ปค์ผ ํฉ๋๋ค.");
}
if (stock < quantity) {
throw new IllegalArgumentException("์ฌ๊ณ ๊ฐ ๋ถ์กฑํฉ๋๋ค.");
}
stock -= quantity;
}
public void setPrice(int price) {
if (price <= 0) throw new IllegalArgumentException("๊ฐ๊ฒฉ์ 0์๋ณด๋ค ์ปค์ผ ํฉ๋๋ค.");
this.price = price;
}
public void setDescription(String description) {
if (description == null || description.isBlank())
throw new IllegalArgumentException("์ค๋ช
์ ๋น์ด ์์ ์ ์์ต๋๋ค.");
this.description = description;
}
public void setStock(int stock) {
if (stock < 0) throw new IllegalArgumentException("์ฌ๊ณ ๋ 0๊ฐ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
this.stock = stock;
}
}
Category ํด๋์ค
์นดํ
๊ณ ๋ฆฌ๋ณ๋ก ์ํ์ ๊ด๋ฆฌํ๋ ์ญํ ์ ๋ด๋นํ๋ค.
์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ๊ณผ ์ํ ๋ฆฌ์คํธ๋ฅผ ๋ณด์ ํ๊ณ
์นดํ
๊ณ ๋ฆฌ ๋จ์๋ก ์ํ ์กฐํ ๋ฐ ๊ด๋ฆฌ ๊ธฐ๋ฅ์ด ์๋ค.
์นดํ ๊ณ ๋ฆฌ๋ฅผ ๋์ ํ๋ฉด์ ์ํ ์กฐํ ๋ก์ง์ ๋ ๋ช ํํ๊ฒ ๋ถ๋ฆฌํ ์ ์์๋ค.
Category.java ์ ์ฒด ์ฝ๋
package kr.spartaclub.com.example.commerce.challenge;
import kr.spartaclub.com.example.commerce.Product;
import java.util.List;
public class Category {
private final String name;
private final List<Product> products;
public Category(String name, List<Product> products) {
this.name = name;
this.products = products;
}
public String getName() {
return name;
}
public List<Product> getProducts() {
return products;
}
}
Cart / CartItem ํด๋์ค
์ฅ๋ฐ๊ตฌ๋ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ ํต์ฌ ๊ฐ์ฒด์ด๋ค.
- Cart๋ ์ฅ๋ฐ๊ตฌ๋ ์ ์ฒด๋ฅผ ๊ด๋ฆฌ
- CartItem์ ์ํ + ์๋ ํ ์ค์ ์๋ฏธ
public void add(Product product, int quantity) {
for (CartItem item : items) {
if (item.getProduct() == product) {
item.addQuantity(quantity);
return;
}
}
items.add(new CartItem(product, quantity));
}
์ด๋ฏธ ๋ด๊ธด ์ํ์ผ ๊ฒฝ์ฐ ์๋๋ง ์ฆ๊ฐ์ํค๋๋ก ๊ตฌํํด
์ฅ๋ฐ๊ตฌ๋ ๊ตฌ์กฐ๋ฅผ ๋จ์ํ๊ฒ ์ ์งํ๋ค.
Cart.java ์ ์ฒด ์ฝ๋
package kr.spartaclub.com.example.commerce.challenge;
import kr.spartaclub.com.example.commerce.Product;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Cart {
private List<CartItem> items = new ArrayList<>();
public boolean isEmpty() {
return items.isEmpty();
}
public List<CartItem> getItems() {
return Collections.unmodifiableList(items);
}
public void add(Product product, int quantity) {
if (quantity <= 0) throw new IllegalArgumentException("์๋์ 1๊ฐ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
for (CartItem item : items) {
if (item.getProduct() == product) {
item.addQuantity(quantity);
return;
}
}
items.add(new CartItem(product, quantity));
}
public int getTotalAmount() {
int sum = 0;
for (CartItem item : items) {
sum += item.getTotalPrice();
}
return sum;
}
public void clear() {
items.clear();
}
public void removeProduct(Product product) {
items.removeIf(item -> item.getProduct() == product);
}
public boolean removeByProductName(String name) {
int before = items.size();
items = items.stream()
.filter(item -> !item.getProduct().getName().equalsIgnoreCase(name.trim()))
.toList();
items = new ArrayList<>(items);
return items.size() != before;
}
}
CartItem.java ์ ์ฒด ์ฝ๋
package kr.spartaclub.com.example.commerce.challenge;
import kr.spartaclub.com.example.commerce.Product;
public class CartItem {
private final Product product;
private int quantity;
public CartItem(Product product, int quantity) {
if (product == null) throw new IllegalArgumentException("์ํ์ด null์ผ ์ ์์ต๋๋ค.");
if (quantity <= 0) throw new IllegalArgumentException("์๋์ 1๊ฐ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
public void addQuantity(int add) {
if (add <= 0) throw new IllegalArgumentException("์ถ๊ฐ ์๋์ 1๊ฐ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
this.quantity += add;
}
public int getTotalPrice() {
return product.getPrice() * quantity;
}
}
CommerceSystem ํด๋์ค
ํ๋ก๊ทธ๋จ ์ ์ฒด ํ๋ฆ์ ์ ์ดํ๋ ํด๋์ค์ด๋ค.
๋ฉ์ธ ๋ฉ๋ด ์ถ๋ ฅ, ์ฌ์ฉ์ ์
๋ ฅ ์ฒ๋ฆฌ, ์นดํ
๊ณ ๋ฆฌ ์ด๋, ์ฃผ๋ฌธ ํ๋ฆ ์ ์ด, ๊ด๋ฆฌ์ ๋ชจ๋ ์ง์
๋ฑ
์ค์ ๋ก์ง์ ๊ฐ ๊ฐ์ฒด์ ์์ํ๊ณ , CommerceSystem์ ํ๋ฆ ์ ์ด์ ์ญํ ์ ์ง์คํ๋๋ก ์ค๊ณํ๋ค.
์ ์ฒด ์ฝ๋๋ ๋๋ฌด ๊ธธ์ด์ GitHub๋ฅผ ํตํด ํ์ธํ๋ค.
@jiiim_ni CommerceProject - github ๋งํฌ
ํ ์คํธ๋ฅผ ์ํด ๋ค์๊ณผ ๊ฐ์ ๊ณ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ค.
customers.put("bronze@test.com", new Customer("๋ธ๋ก ์ฆ", "bronze@test.com", CustomerGrade.BRONZE, 0));
customers.put("silver@test.com", new Customer("์ค๋ฒ", "silver@test.com", CustomerGrade.SILVER, 600_000));
customers.put("gold@test.com", new Customer("๊ณจ๋", "gold@test.com", CustomerGrade.GOLD, 1_200_000));
customers.put("platinum@test.com", new Customer("ํ๋ํฐ๋", "platinum@test.com", CustomerGrade.PLATINUM, 2_500_000));
ํต์ฌ ๋ก์ง
Lv1. ์ฅ๋ฐ๊ตฌ๋ + ์ฃผ๋ฌธ ํ๋ฆ
- ์ํ ์ ํ → ์๋ ์ ๋ ฅ → ์ฅ๋ฐ๊ตฌ๋ ๋ด๊ธฐ
- ์ฅ๋ฐ๊ตฌ๋ ์กฐํ ์ ์ด ๊ธ์ก ๊ณ์ฐ
- ์ฃผ๋ฌธ ํ์ ์ ์ฌ๊ณ ์ฐจ๊ฐ
- ์ฃผ๋ฌธ ์ทจ์ ์ ์ฅ๋ฐ๊ตฌ๋ ์ด๊ธฐํ
๊ฐ์ฅ ์ค์ํ๊ฒ ๊ณ ๋ คํ ์ ์ ์ฌ๊ณ ์ฐจ๊ฐ ์์ ์ด์๋ค.
์ฅ๋ฐ๊ตฌ๋์ ๋ด๋ ์๊ฐ์ด ์๋๋ผ ์ฃผ๋ฌธ ํ์ ์์ ์๋ง ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ๋๋ก ๊ตฌํํ๋ค.
Lv2. ๊ด๋ฆฌ์ ๋ชจ๋
- ๋น๋ฐ๋ฒํธ ์ธ์ฆ (3ํ ์คํจ ์ ๋ฉ์ธ ๋ฉ๋ด ๋ณต๊ท)
- ์ํ ์ถ๊ฐ / ์์ / ์ญ์
- ์นดํ ๊ณ ๋ฆฌ ์ ํ ํ ์ํ ๊ด๋ฆฌ
- ์ญ์ ๋ ์ํ์ด ์ฅ๋ฐ๊ตฌ๋์ ์์ ๊ฒฝ์ฐ ํจ๊ป ์ ๊ฑฐ
์ํ ์์ ์์๋ ๊ฐ๊ฒฉ, ์ค๋ช , ์ฌ๊ณ ๋ฅผ ๊ฐ๊ฐ ์ ํ์ ์ผ๋ก ์์ ํ ์ ์๊ฒ ๊ตฌํํ๋ค.
Lv3. Enum, ๋๋ค & ์คํธ๋ฆผ ํ์ฉ
๊ณ ๊ฐ ๋ฑ๊ธ ํ ์ธ(Enum)์ ์ ์ํด ๋ฑ๊ธ๋ณ ํ ์ธ์จ์ ๊ด๋ฆฌํ๋ค.
public enum CustomerGrade {
BRONZE(0),
SILVER(5),
GOLD(10),
PLATINUM(15);
}
CustomerGrade.java ์ ์ฒด ์ฝ๋
package kr.spartaclub.com.example.commerce.challenge;
public enum CustomerGrade {
BRONZE(0),
SILVER(5),
GOLD(10),
PLATINUM(15);
private final int discountPercent;
CustomerGrade(int discountPercent) {
this.discountPercent = discountPercent;
}
public int getDiscountPercent() {
return discountPercent;
}
public double getDiscountRate() {
return discountPercent / 100.0;
}
}
์คํธ๋ฆผ ํ์ฉ
๊ฐ๊ฒฉ๋๋ณ ์ํ ํํฐ๋ง
products.stream()
.filter(p -> p.getPrice() <= 1_000_000)
.forEach(...);
์ฅ๋ฐ๊ตฌ๋ ์ํ ์ ๊ฑฐ
items = items.stream()
.filter(item -> !item.getProduct().getName().equalsIgnoreCase(name))
.toList();
๊ฒฐ๊ณผ
์ฌ๊ณ ๋ณ๊ฒฝ + ์ฃผ๋ฌธ ์ทจ์




๊ด๋ฆฌ์ ๋ชจ๋



์ ์ฒด ๊ธฐ๋ฅ ๊ฒฐ๊ณผ







ํธ๋ฌ๋ธ์ํ ๋ฐ ํ๊ณ
1) ๋์ ๊ธฐ๋ฅ ๊ตฌํ ๋์ด๋
๋์ ๊ธฐ๋ฅ๋ถํฐ๋ ์ค์ ์๋น์ค ํ๋ฆ์ ๊ณ ๋ คํด์ผ ํด์ ๋จ์ ๊ธฐ๋ฅ ์ถ๊ฐ๋ก๋ ํด๊ฒฐ์ด ์ด๋ ค์ ๋ค.
๊ธฐ๋ฅ ํ๋๊ฐ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ฐ์์ ์ผ๋ก ์ํฅ์ ์ค๋ค๋ ์ ์ ์ฒด๊ฐํ๋ค.
2) CommerceSystem ์ฝ๋ ๋น๋ํ
๋ก์ง์ด ๋ชฐ๋ฆฌ๋ฉด์ ์์ ํฌ์ธํธ๋ฅผ ์ฐพ๊ธฐ ์ด๋ ค์ ๋ค.
๋ฉ๋ด ํ๋ ์์ ํด๋ ์ฌ๋ฌ ๋ฉ์๋๋ฅผ ๋์์ ๋ด์ผ ํ๋ค.
3) ์ปค๋ฐ ๊ด๋ฆฌ ์คํจ
๊ธฐ๋ฅ ๋จ์๋ก ์ปค๋ฐํ์ง ์์ ๋์ค์ ์ ๋ฆฌํ ๋ ๋งค์ฐ ํท๊ฐ๋ ธ๋ค.
์์ ๋จ์, ์คํ ๊ฐ๋ฅํ ์ํ์์ ์ปค๋ฐํ๋ ์ต๊ด์ ์ค์์ฑ์ ๋๊ผ๋ค.
4) AI ํ์ฉ์ ๋ํ ํ๊ณ
AI๋ฅผ ์ ๋ต ์์ฑ๊ธฐ๊ฐ ์๋๋ผ ๋ฐฉํฅ ํํธ์ฉ์ผ๋ก ํ์ฉํ๋ ค๊ณ ๋
ธ๋ ฅํ๋ค.
์ฝ๋๋ฅผ ๊ทธ๋๋ก ์ฐ๊ธฐ๋ณด๋ค๋ ์ดํด ํ ์ง์ ์์ฑํ๋ ๋ฐฉ์์ ์ ์งํ๋ค.
ํ๋ก์ ํธ ํ์ผ ์ค์
