-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.py
33 lines (27 loc) · 883 Bytes
/
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
import dataclasses
# 他国通貨オブジェクト
@dataclasses.dataclass(frozen=True)
class Money:
amount: int
currency: str
def times(self, multiplier):
return Money(self.amount * multiplier, self.currency)
def add(self, added):
if self.currency == added.currency:
return Money(self.amount + added.amount, self.currency)
else:
# ドル換算で足し算を行う
augend = Bank.exchange(self)
added = Bank.exchange(added)
return Money(augend.amount + added.amount, "USD")
# 銀行オブジェクト
class Bank:
# 為替レート
rates = {"CHF": 0.5}
# 為替関数
@classmethod
def exchange(self, money):
if money.currency == "USD":
return money
else:
return Money(money.amount * self.rates[money.currency], "USD")