generated from yandex-praktikum/hw_python_oop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework.py
73 lines (57 loc) · 2.2 KB
/
homework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import datetime as dt
class Record:
def __init__(self, amount, comment, date=None):
format_date = '%d.%m.%Y'
self.amount = amount
self.comment = comment
if date is None:
self.date = dt.date.today()
else:
self.date = dt.datetime.strptime(date, format_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()
today_amount = [record.amount for record in self.records
if record.date == today]
return sum(amount for amount in today_amount)
def get_week_stats(self):
today = dt.date.today()
week = dt.date.today() - dt.timedelta(days=7)
week_amount = [record.amount for record in self.records
if week < record.date <= today]
return sum(amount for amount in week_amount)
def get_calculate(self):
return self.limit - self.get_today_stats()
class CashCalculator(Calculator):
USD_RATE = 60.00
EURO_RATE = 70.00
def get_today_cash_remained(self, currency):
self.currency = currency
self.currencies = {
'rub': (1.00, 'руб'),
'eur': (self.EURO_RATE, 'Euro'),
'usd': (self.USD_RATE, 'USD')
}
cur_rate, cur_name = self.currencies[self.currency]
result = self.get_calculate() / cur_rate
txt = 'Денег нет, держись'
if result > 0:
return f'На сегодня осталось {result:.2f} {cur_name}'
elif result == 0:
return txt
else:
return f'{txt}: твой долг - {abs(result):.2f} {cur_name}'
class CaloriesCalculator(Calculator):
def get_calories_remained(self):
result = self.get_calculate()
txt = ('Сегодня можно съесть что-нибудь ещё, '
'но с общей калорийностью не более')
if result <= 0:
return 'Хватит есть!'
else:
return f'{txt} {result} кКал'