YP sprint 2

mail@pastecode.io avatar
unknown
python
3 years ago
3.1 kB
1
Indexable
Never
import datetime as dt
from forex_python.converter import CurrencyRates


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):
        return self.limit - self.get_today_stats()

    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):
    c = CurrencyRates()
    USD_RATE = c.get_rate('USD', 'RUB')
    EURO_RATE = c.get_rate('EUR', 'RUB')
    RUB_RATE = 1

    def get_today_stats(self):
        today_amount = super().get_today_stats()
        return today_amount

    def get_week_stats(self):
        week_amount = super().get_week_stats()
        return week_amount

    def get_today_cash_remained(self, currency):
        accepted_currencies = {'rub': (self.RUB_RATE, 'руб'),
                               'usd': (self.USD_RATE, 'USD'),
                               'eur': (self.EURO_RATE, 'EUR')}
        try:
            accepted_currencies[currency]
        except KeyError:
            return 'Неправильная валюта!'
        rate, title = accepted_currencies[currency]
        today_balance = super().get_today_balance() / rate
        if today_balance > 0:
            return f'На сегодня осталось {today_balance} {title}'
        if today_balance < 0:
            return (f'Денег нет, держись: твой долг - {today_balance} '
                    f'{title}')
        return 'Денег нет, держись'


class CaloriesCalculator(Calculator):
    def get_today_stats(self):
        today_amount = super().get_today_stats()
        return f'За сегодня потреблено {today_amount} кКал'

    def get_week_stats(self):
        week_amount = super().get_week_stats()
        return f'За неделю потреблено {week_amount} кКал'

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