Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 01_Money_result.py #3585

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions Module3/home_work/01_Money_result.py
Original file line number Diff line number Diff line change
@@ -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)}коп."