class Product:
def __init__(self, name, price):
self.name = name
self.price = price
class Cart:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def get_total_amount(self):
return sum(product.price for product in self.products)
class User:
def __init__(self, name, points):
self.name = name
self.points = points
self.cart = Cart()
def checkout(self, coupon_rate=0):
total_amount = self.cart.get_total_amount()
discount_amount = int(total_amount * coupon_rate)
after_coupon_amount = total_amount - discount_amount
max_point_usage = int(after_coupon_amount * 0.1)
point_used = min(self.points, max_point_usage)
final_amount = after_coupon_amount - point_used
self.points -= point_used
return (
f"총액 {total_amount:,}원에서 쿠폰 할인({discount_amount:,}원)이 적용되었습니다. "
f"포인트 {point_used:,}점을 추가 사용하여 최종 결제 금액은 {final_amount:,}원입니다."
)
user = User("홍길동", 15000)
user.cart.add_product(Product("상품A", 50000))
user.cart.add_product(Product("상품B", 30000))
result = user.checkout(coupon_rate=0.1)
print(result)
To embed this project on your website, copy the following code and paste it into your website's HTML: