-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path212 rollback2.py
42 lines (34 loc) · 1.38 KB
/
212 rollback2.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
from decimal import *
class Account(object):
_qb = Decimal("0.00")
def __init__(self, name: str, opening_balance: float = 0.0):
self.name = name
self._balance = Decimal(opening_balance).quantize(Account._qb)
print("Account created for {}".format(self.name), end="")
self.show_balance()
def deposit(self, amount: float) -> Decimal:
decimal_amount = Decimal(amount).quantize(Account._qb)
if decimal_amount > Account._qb:
self._balance = self._balance + decimal_amount
print("{} deposited".format(decimal_amount))
return self._balance
def withdraw(self, amount: float) -> Decimal:
decimal_amount = Decimal(amount).quantize(Account._qb)
if Account._qb < decimal_amount <= self._balance:
self._balance = self._balance - decimal_amount
print("{} withdrawn".format(decimal_amount))
return decimal_amount
else:
print("Amount must be greater than 0 and less than your account balance")
return Account._qb
def show_balance(self):
print("Balance on account {} is {}".format(self.name, self._balance))
if __name__ == "__main__":
mario = Account("Mario")
mario.deposit(10)
mario.deposit(0.1)
mario.deposit(0.1)
mario.deposit(0.10)
mario.withdraw(0.30)
mario.withdraw(0)
mario.show_balance()