Skip to content

Commit 00a58a7

Browse files
committed
It's done, sir!
1 parent 90a22ca commit 00a58a7

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

homework.py

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
import datetime as dt
22

3-
now = dt.datetime.now()
4-
date_format = '%d.%m.%Y'
3+
DATE_FORMAT = '%d.%m.%Y'
54

65

76
class Record:
87
def __init__(self, amount, comment, date=None):
98
self.amount = amount
109
self.comment = comment
1110
if date is None:
12-
# Указываем сегодняшнюю дату по-умолчанию
13-
self.date = now.date()
11+
self.date = dt.date.today()
1412
else:
15-
# Приводим полученную дату к виду date_format
16-
self.date = dt.datetime.strptime(date, date_format).date()
13+
self.date = dt.datetime.strptime(date, DATE_FORMAT).date()
1714

1815

19-
class Calculator():
16+
class Calculator:
2017
def __init__(self, limit):
2118
self.limit = limit
22-
self.week_later = dt.date.today() - dt.timedelta(days=7)
2319
self.records = []
2420

21+
def get_today_date(self):
22+
return dt.date.today()
23+
24+
def get_week_date(self):
25+
return self.get_today_date() - dt.timedelta(days=7)
26+
2527
def add_record(self, record):
2628
self.records.append(record)
2729

2830
def get_today_stats(self):
2931
t_amount = 0
3032
for record in self.records:
31-
if record.date == now.date():
33+
if record.date == self.get_today_date():
3234
t_amount += record.amount
3335
return t_amount
3436

3537
def get_week_stats(self):
3638
total_week_amount = 0
3739
for record in self.records:
38-
if now.date() >= record.date > self.week_later:
40+
if self.get_today_date() >= record.date > self.get_week_date():
3941
total_week_amount += record.amount
4042
return total_week_amount
4143

@@ -54,38 +56,22 @@ def __init__(self, limit):
5456
def get_today_cash_remained(self, currency):
5557
self.currency = currency
5658
t_amount = self.get_today_limit()
57-
rub_currency = 'руб'
58-
usd_currency = 'USD'
59-
eur_currency = 'Euro'
60-
wrong_currency_message = 'Указана неверная валюта. Повторите ввод.'
61-
positive_message = 'На сегодня осталось'
62-
neutral_message = 'Денег нет, держись'
63-
negative_message = 'Денег нет, держись: твой долг -'
59+
currencies = {'rub': ('руб', 1),
60+
'usd': ('USD', CashCalculator.USD_RATE),
61+
'eur': ('Euro', CashCalculator.EURO_RATE)}
6462

63+
c_type, c_rate = currencies[currency]
64+
65+
if self.currency not in currencies:
66+
return 'Указана неверная валюта. Повторите ввод.'
67+
if t_amount == 0:
68+
return 'Денег нет, держись'
6569
if t_amount > 0:
66-
if self.currency == 'rub':
67-
return (f'{positive_message} {t_amount} {rub_currency}')
68-
elif self.currency == 'usd':
69-
t_amount = round((t_amount / CashCalculator.USD_RATE), 2)
70-
return (f'{positive_message} {t_amount} {usd_currency}')
71-
elif self.currency == 'eur':
72-
t_amount = round((t_amount / CashCalculator.EURO_RATE), 2)
73-
return (f'{positive_message} {t_amount} {eur_currency}')
74-
else:
75-
return (f'{wrong_currency_message}')
76-
elif t_amount == 0:
77-
return (f'{neutral_message}')
70+
return (f'На сегодня осталось '
71+
f'{abs(round((t_amount / c_rate), 2))} {c_type}')
7872
else:
79-
if self.currency == 'rub':
80-
return (f'{negative_message} {abs(t_amount)} {rub_currency}')
81-
elif self.currency == 'usd':
82-
t_amount = round((t_amount / CashCalculator.USD_RATE), 2)
83-
return (f'{negative_message} {abs(t_amount)} {usd_currency}')
84-
elif self.currency == 'eur':
85-
t_amount = round((t_amount / CashCalculator.EURO_RATE), 2)
86-
return (f'{negative_message} {abs(t_amount)} {eur_currency}')
87-
else:
88-
return (f'{wrong_currency_message}')
73+
return (f'Денег нет, держись: твой долг - '
74+
f'{abs(round((t_amount / c_rate), 2))} {c_type}')
8975

9076

9177
class CaloriesCalculator(Calculator):
@@ -94,17 +80,11 @@ def __init__(self, limit):
9480
self.records = []
9581

9682
def get_calories_remained(self):
97-
positive_message = ('Сегодня можно съесть что-нибудь ещё, но с '
98-
'общей калорийностью не более')
99-
neutral_message = 'Хватит есть!'
83+
positive_message = ('Сегодня можно съесть что-нибудь ещё, '
84+
'но с общей калорийностью не более')
10085

101-
t_amount = 0
102-
for record in self.records:
103-
if record.date == now.date():
104-
t_amount += record.amount
105-
t_amount = self.limit - t_amount
86+
t_amount = self.get_today_limit()
10687

10788
if t_amount > 0:
10889
return (f'{positive_message} {t_amount} кКал')
109-
else:
110-
return (f'{neutral_message}')
90+
return ('Хватит есть!')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy