|
1 | 1 | import datetime as dt
|
2 | 2 |
|
3 |
| - |
4 |
| -class Calculator: |
5 |
| - pass |
| 3 | +now = dt.datetime.now() |
| 4 | +date_format = '%d.%m.%Y' |
6 | 5 |
|
7 | 6 |
|
8 | 7 | class Record:
|
9 |
| - pass |
| 8 | + def __init__(self, amount, comment, date=None): |
| 9 | + self.amount = amount |
| 10 | + self.comment = comment |
| 11 | + if date is None: |
| 12 | + # Указываем сегодняшнюю дату по-умолчанию |
| 13 | + self.date = now.date() |
| 14 | + else: |
| 15 | + # Приводим полученную дату к виду date_format |
| 16 | + self.date = dt.datetime.strptime(date, date_format).date() |
| 17 | + |
| 18 | + |
| 19 | +class Calculator(): |
| 20 | + def __init__(self, limit): |
| 21 | + self.limit = limit |
| 22 | + self.week_later = dt.date.today() - dt.timedelta(days=7) |
| 23 | + self.records = [] |
| 24 | + |
| 25 | + def add_record(self, record): |
| 26 | + self.records.append(record) |
| 27 | + |
| 28 | + def get_today_stats(self): |
| 29 | + t_amount = 0 |
| 30 | + for record in self.records: |
| 31 | + if record.date == now.date(): |
| 32 | + t_amount += record.amount |
| 33 | + return t_amount |
| 34 | + |
| 35 | + def get_week_stats(self): |
| 36 | + total_week_amount = 0 |
| 37 | + for record in self.records: |
| 38 | + if now.date() >= record.date > self.week_later: |
| 39 | + total_week_amount += record.amount |
| 40 | + return total_week_amount |
| 41 | + |
| 42 | + |
| 43 | +class CashCalculator(Calculator): |
| 44 | + USD_RATE = 73.20 |
| 45 | + EURO_RATE = 86.64 |
| 46 | + |
| 47 | + def __init__(self, limit): |
| 48 | + self.limit = limit |
| 49 | + self.records = [] |
| 50 | + |
| 51 | + def get_today_cash_remained(self, currency): |
| 52 | + self.currency = currency |
| 53 | + |
| 54 | + rub_currency = 'руб' |
| 55 | + usd_currency = 'USD' |
| 56 | + eur_currency = 'Euro' |
| 57 | + |
| 58 | + t_amount = 0 |
| 59 | + for record in self.records: |
| 60 | + if record.date == now.date(): |
| 61 | + t_amount += record.amount |
| 62 | + t_amount = self.limit - t_amount |
| 63 | + |
| 64 | + wrong_currency_message = 'Указана неверная валюта. Повторите ввод.' |
| 65 | + positive_message = 'На сегодня осталось' |
| 66 | + neutral_message = 'Денег нет, держись' |
| 67 | + negative_message = 'Денег нет, держись: твой долг -' |
| 68 | + |
| 69 | + if t_amount > 0: |
| 70 | + if self.currency == 'rub': |
| 71 | + return (f'{positive_message} {t_amount} {rub_currency}') |
| 72 | + elif self.currency == 'usd': |
| 73 | + t_amount = round((t_amount / CashCalculator.USD_RATE), 2) |
| 74 | + return (f'{positive_message} {t_amount} {usd_currency}') |
| 75 | + elif self.currency == 'eur': |
| 76 | + t_amount = round((t_amount / CashCalculator.EURO_RATE), 2) |
| 77 | + return (f'{positive_message} {t_amount} {eur_currency}') |
| 78 | + else: |
| 79 | + return (f'{wrong_currency_message}') |
| 80 | + elif t_amount == 0: |
| 81 | + return (f'{neutral_message}') |
| 82 | + else: |
| 83 | + if self.currency == 'rub': |
| 84 | + return (f'{negative_message} {abs(t_amount)} {rub_currency}') |
| 85 | + elif self.currency == 'usd': |
| 86 | + t_amount = round((t_amount / CashCalculator.USD_RATE), 2) |
| 87 | + return (f'{negative_message} {abs(t_amount)} {usd_currency}') |
| 88 | + elif self.currency == 'eur': |
| 89 | + t_amount = round((t_amount / CashCalculator.EURO_RATE), 2) |
| 90 | + return (f'{negative_message} {abs(t_amount)} {eur_currency}') |
| 91 | + else: |
| 92 | + return (f'{wrong_currency_message}') |
| 93 | + |
| 94 | + |
| 95 | +class CaloriesCalculator(Calculator): |
| 96 | + def __init__(self, limit): |
| 97 | + self.limit = limit |
| 98 | + self.records = [] |
| 99 | + |
| 100 | + def get_calories_remained(self): |
| 101 | + positive_message = ('Сегодня можно съесть что-нибудь ещё, но с ' |
| 102 | + 'общей калорийностью не более') |
| 103 | + neutral_message = 'Хватит есть!' |
| 104 | + |
| 105 | + t_amount = 0 |
| 106 | + for record in self.records: |
| 107 | + if record.date == now.date(): |
| 108 | + t_amount += record.amount |
| 109 | + t_amount = self.limit - t_amount |
| 110 | + |
| 111 | + if t_amount > 0: |
| 112 | + return (f'{positive_message} {t_amount} кКал') |
| 113 | + else: |
| 114 | + return (f'{neutral_message}') |
0 commit comments