본문 바로가기

하루코딩/python 하루코딩

[Pythone] 제품에 따른 출력 부모 자식 클래스 상속

728x90
반응형
class Product:
    def __init__(self, name, price, quantity):
        self.name = name
        self.price = price
        self.quantity = quantity

    # 재고 업데이트 메서드
    def update_quantity(self, amount):
        self.quantity += amount
        print(
            f"{self.name} 재고가 {amount}만큼 {'증가' if amount > 0 else '감소'}했습니다. 현재 재고: {self.quantity}")

    # 상품 정보 출력 메서드
    def display_info(self):
        print(f"상품명: {self.name}")
        print(f"가격: {self.price}원")
        print(f"재고: {self.quantity}개")


class Electronic(Product):
    def __init__(self, name, price, quantity, warranty_period=12):
        super().__init__(name, price, quantity)
        self.warranty_period = warranty_period

    def extend_warranty(self, month):
        total_warranty = self.warranty_period + month
        print(
            f"보증 기간이 {self.warranty_period}개월 연장되었습니다. 현재 보증 기간: {total_warranty}")

    def display_info(self):
        super().display_info()
        print(f"보증 기간: {self.warranty_period}")


class Food(Product):
    def __init__(self, name, price, quantity, expiration_date):
        super().__init__(name, price, quantity)
        self.expiration_date = expiration_date

    def is_expired(self, current_date):
        if current_date > self.expiration_date:
            print(f"{self.name}은 유통기한이 지났습니다. ")
        else:
            print(f"{self.name}은 유통기한이 지나지 않았습니다.")

    def display_info(self):
        super().display_info()
        print(f"유통기한 : {self.expiration_date}")


e1 = Electronic("스마트TV", 150000, 5, 24)
e1.display_info()
e1.extend_warranty(12)

f1 = Food("사과", 3000, 50, "2023-12-29")
f1.display_info()
f1.is_expired("2024-11-28")
반응형