Net Eng

파이썬을 이용한 CoffeeMachine 본문

Language/Python

파이썬을 이용한 CoffeeMachine

欲心 2024. 6. 8. 01:33
import sys
class Coffee:
    total_amount = 10
    total_amount_price = 5000
    coffee_price = 300
    put_price = 0                   # 넣은 금액
    req_coffee_nums = 0             # 주문한 커피 개수
    remaining_price = 0             # 거스름 돈

    def request(self):
        print("+------------------가격표---------------------")
        print("|                커피 : 300                  |")
        print("+--------------------------------------------")
        try:
            self.put_price = int(input("금액을 투입해 주세요 : "))
            self.req_coffee_nums = int(input("커피 몇 잔을 주문하시겠습니까? : "))
        except ValueError:
            print("[ ERROR ] 올바른 값을 입력해주세요.\n")
            self.request()

    def info(self):
        print("+--------------------------------------------")
        print("| 자판기 보유 금액:", self.total_amount_price)
        print("| 주문 가능한 커피 개수:", self.total_amount)
        print("+--------------------------------------------")

    def check_amount(self):
        if self.total_amount <= 0:
            return False
        elif self.total_amount_price <= 0:
            return False
        else:
            return True

    def order(self, num):
        if self.req_coffee_nums > self.total_amount:
            sys.exit("재고가 부족합니다.")
        if self.remaining_price > self.total_amount_price:
            sys.exit("자판기 거스름돈이 부족합니다.")
        if self.remaining_price == 0:
            print('\n커피 %d잔이 나왔습니다. \n거스름돈은 없습니다.' % self.req_coffee_nums)
            self.total_amount -= self.req_coffee_nums
        else:
            print('\n커피 %d잔이 나왔습니다.' % num)
            print('거스름돈은 %d 입니다.' % self.remaining_price)
            self.total_amount -= num
            self.total_amount_price -= self.remaining_price


    def get(self, put_price, req_coffee_nums):
        # 돈을 충분히 넣은 경우
        price = req_coffee_nums * self.coffee_price

        if put_price >= price:
            num = req_coffee_nums
            self.remaining_price = put_price - price
            self.order(num)
        # 돈을 반정도 넣은 경우
        elif self.coffee_price <= put_price < price:
            num = put_price // self.coffee_price
            self.remaining_price = put_price - (self.coffee_price * num)
            self.order(num)
        # 돈을 불충분 하게 넣은 경우
        else:
            print('\n투입한 금액이 부족합니다. \n투입한 금액 %d이 반환됩니다.' % put_price)


def main():
    c = Coffee()
    while True:
        c.info()
        c.request()
        if c.check_amount():
            c.get(c.put_price, c.req_coffee_nums)
            c.check_amount()
        else:
            sys.exit('남은 커피 개수 또는 자판기 보유 잔액이 부족합니다.')
        input('\n<Enter> 입력')

if __name__ == '__main__':
    main()