Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
2.7 kB
1
Indexable
Never
import datetime as dt


class Record:
    DATE_FORMAT = '%d.%m.%Y'

    def __init__(self, amount, comment, date=None):
        if date is None:
            date = dt.date.today()
        else:
            date = dt.datetime.strptime(date, self.DATE_FORMAT).date()
        self.amount = amount
        self.comment = comment
        self.date = date


class Calculator:
    def __init__(self, limit):
        self.limit = limit
        self.records = []

    def add_record(self, record):
        self.records.append(record)

    def get_today_stats(self):
        today = dt.date.today()
        day_amount = sum([item.amount for item in self.records
                          if item.date == today])
        return day_amount

    def get_today_balance(self):
        today_balance = self.limit - self.get_today_stats()
        return today_balance

    def get_week_stats(self):
        today = dt.date.today()
        week_ago_date = today - dt.timedelta(days=7)
        week_amount = sum(item.amount for item in self.records
                          if week_ago_date < item.date <= today)
        return week_amount


class CashCalculator(Calculator):
    USD_RATE = 70
    EURO_RATE = 90
    RUB_RATE = 1

    def __init__(self, limit):
        super().__init__(limit)
        self.accepted_currencies = {'rub': (self.RUB_RATE, 'руб'),
                                    'usd': (self.USD_RATE, 'USD'),
                                    'eur': (self.EURO_RATE, 'Euro')}

    def get_today_cash_remained(self, currency):
        try:
            self.accepted_currencies[currency]
        except KeyError:
            return 'Неправильная валюта!'
        rate, title = self.accepted_currencies[currency]
        today_cash_balance = round(super().get_today_balance() / rate, 2)
        if today_cash_balance > 0:
            return f'На сегодня осталось {today_cash_balance} {title}'
        if today_cash_balance < 0:
            today_cash_balance = abs(today_cash_balance)
            return (f'Денег нет, держись: твой долг - {today_cash_balance} '
                    f'{title}')
        return 'Денег нет, держись'


class CaloriesCalculator(Calculator):
    def get_calories_remained(self):
        today_calories_balance = super().get_today_balance()
        if today_calories_balance > 0:
            return (f'Сегодня можно съесть что-нибудь ещё, но с общей '
                    f'калорийностью не более {today_calories_balance} кКал')
        return 'Хватит есть!'