forked from momentum-cohort-2019-02/w3d2--money
/
money.py
89 lines (74 loc) · 2.35 KB
/
money.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class DifferentCurrencyError(Exception):
pass
class Currency:
"""
Represents a currency. Does not contain any exchange rate info.
"""
def __init__(self, name, code, symbol=None, digits=2):
"""
Parameters:
- name -- the English name of the currency
- code -- the ISO 4217 three-letter code for the currency
- symbol - optional symbol used to designate currency
- digits -- number of significant digits used
"""
pass
def __str__(self):
"""
Should return the currency code, or code with symbol in parentheses.
"""
pass
def __eq__(self, other):
"""
All fields must be equal to for the objects to be equal.
"""
return (type(self) == type(other) and self.name == other.name
and self.code == other.code and self.symbol == other.symbol
and self.digits == other.digits)
class Money:
"""
Represents an amount of money. Requires an amount and a currency.
"""
def __init__(self, amount, currency):
"""
Parameters:
- amount -- quantity of currency
- currency -- type of currency
"""
pass
def __str__(self):
"""
Should use the currency symbol if available, else use the code.
Use the currency digits to determine number of digits to show.
"""
pass
def __repr__(self):
return f"<Money {str(self)}>"
def __eq__(self, other):
"""
All fields must be equal to for the objects to be equal.
"""
return (type(self) == type(other) and self.amount == other.amount
and self.currency == other.currency)
def add(self, other):
"""
Add two money objects of the same currency. If they have different
currencies, raise a DifferentCurrencyError.
"""
pass
def sub(self, other):
"""
Subtract two money objects of the same currency. If they have different
currencies, raise a DifferentCurrencyError.
"""
pass
def mul(self, multiplier):
"""
Multiply a money object by a number to get a new money object.
"""
pass
def div(self, divisor):
"""
Divide a money object by a number to get a new money object.
"""
pass