From f821d8656fbb11c6c6d197e474f8e7b5e19438e2 Mon Sep 17 00:00:00 2001 From: Pluzhnikov Mikhail <106968636+mihaisVFR@users.noreply.github.com> Date: Thu, 27 Apr 2023 11:32:52 +0300 Subject: [PATCH] Update 01_Money_result.py --- Module3/home_work/01_Money_result.py | 62 +++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/Module3/home_work/01_Money_result.py b/Module3/home_work/01_Money_result.py index e154a0798..4636ce9b7 100644 --- a/Module3/home_work/01_Money_result.py +++ b/Module3/home_work/01_Money_result.py @@ -1,3 +1,61 @@ +import json +import urllib.request + + class Money: - pass - # TODO: your code here + def __init__(self, notes: int, coins: int): + self.total_in_coins = notes * 100 + coins + self.coins = int(self.total_in_coins % 100) + self.notes = int((self.total_in_coins - self.coins) / 100) + + if self.coins >= 100 and self.total_in_coins >= 0: + self.notes += self.coins // 100 + self.coins = self.coins % 100 + + if self.coins <= 100 and self.total_in_coins < 0: + if self.coins != 0: + self.coins = (100 - self.coins % 100)*-1 + else: + self.coins = 0 + self.notes = int((self.total_in_coins - self.coins) / 100) + + def __str__(self): + if self.total_in_coins > 0: + return f"{self.notes}руб {abs(self.coins)}коп" + elif self.total_in_coins < 0: + return f"{self.notes}руб {abs(self.coins)}коп" + elif self.total_in_coins == 0: + return f"0руб 0коп" + + def __add__(self, other): + return Money(self.notes + other.notes, self.coins + other.coins) + + def __sub__(self, other): + return Money(self.notes - other.notes, self.coins - other.coins) + + def __mul__(self, other): + + return Money(self.notes * other, self.coins * other) + + def __gt__(self, other): + return self.total_in_coins > other.total_in_coins + + def __lt__(self, other): + return self.total_in_coins < other.total_in_coins + + def __eq__(self, other): + return self.total_in_coins == other.total_in_coins + + def __ne__(self, other): + return self.total_in_coins != other.total_in_coins + + def __mod__(self, other): + percent = round((self.total_in_coins/10000)*other, 2) + return Money(percent, 0) + + def convert(self, currency: str): + data = urllib.request.urlopen('https://www.cbr-xml-daily.ru/daily_json.js').read() + data_dict = json.loads(data) + currency_rate = data_dict["Valute"][f'{currency.upper()}']['Value'] + exchanged = round(self.total_in_coins / currency_rate/ 100, 2) + return f"{exchanged}({currency.upper()}) за {self.notes}руб. {abs(self.coins)}коп."