파이썬 - 5일차

2020. 1. 29. 15:28카테고리 없음

import math
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):
        self.put_price = int(input("돈을 넣으시오: "))
        self.req_coffee_nums = int(input("원하는 커피 개수를 입력하시오: "))

    def get(self, put_price, req_coffee_nums):
        # put_price = 700, coffee_nums = 2
        # print(put_price, req_coffee_nums)
        price = req_coffee_nums * self.coffee_price
        if put_price >= price:
            remaining_price = put_price - price
            if remaining_price == 0:
                print("커피를 %d개 드립니다." % req_coffee_nums)
                print("거스름돈은 없습니다.")
            else:
                print("커피를 %d개 드립니다." % req_coffee_nums)
                print("거스름돈 %d를 받으십시오" % remaining_price)
                self.total_amount_price -= remaining_price
            self.total_amount -= req_coffee_nums
        elif self.coffee_price <= put_price < price:
            # put_price = 400, coffee_nums = 2
            num = math.trunc(put_price / self.coffee_price)
            self.remaining_price = put_price - (self.coffee_price * num)
            if self.remaining_price == 0:
                print("커피를 %d개 드립니다." % num)
                print("거스름돈은 없습니다.")
            else:
                print("커피를 %d개 드립니다." % num)
                print("거스름돈 %d를 받으십시오" % self.remaining_price)
                self.total_amount_price -= self.remaining_price
            self.total_amount -= num
        else:
            # put_price = 200, coffee_nums = 2
            print("돈이 충분하지 못합니다. 돈을 더 넣어 주세요.")

    def info(self):
        print("*" * 60)
        print('자판기가 보유한 총 금액:', self.total_amount_price)
        print('자판기가 보유한 총 커피 개수:', self.total_amount)
        print("*" * 60)

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


c = Coffee()

while True:
    c.request()
    # print(c.put_price)
    # print(c.req_coffee_nums)
    if c.check_amount():
        c.get(c.put_price, c.req_coffee_nums)
        c.info()
    else:
        sys.exit("자판기 커피 개수 또는 자판기 보유 잔액이 부족합니다.")

    input("Press any key ....")