-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank_account.py
More file actions
71 lines (56 loc) · 2.1 KB
/
bank_account.py
File metadata and controls
71 lines (56 loc) · 2.1 KB
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
from datetime import datetime
from src.exceptions import (
InsufficientFoudsError,
WithdrawTimeRestrictionError,
WithdrawWeekendError,
)
class BankAccount:
def __init__(self, balance=0, log_file=None, file_log_withdraw=None) -> None:
self.balance = balance
self.log_file = log_file
self._log_transaction("Cuenta creada")
self.log_file_withdraw = file_log_withdraw
def grabar_log(self, fileName, message):
if fileName:
with open(fileName, "a") as f:
f.write(f"{message}\n")
def _log_transaction(self, message):
self.grabar_log(self.log_file, message)
def _log_transaction_fail(self, message):
self.grabar_log(self.log_file_withdraw, message)
def deposit(self, amount):
"""Deposit method
Args:
amount (number): Amount that we will deposit
Returns:
number: New balance after deposit
>>> account = BankAccount(100)
>>> account.deposit(50)
150
"""
if amount < 0:
raise ValueError("Can't deposit negative amounts")
if amount > 0:
self.balance += amount
self._log_transaction(f"Deposited {amount}- New balance: {self.balance}")
return self.balance
def withdraw(self, amount):
now = datetime.now()
if now.hour < 8 or now.hour > 17:
raise WithdrawTimeRestrictionError(
"Withdrawals are only allowed between 8:00 and 17:00"
)
if now.weekday() in [5, 6]:
raise WithdrawWeekendError("Withdrawals not allowed in weekends")
if amount > 0:
if amount <= self.balance:
self.balance -= amount
self._log_transaction(f"Withdraw {amount}- New balance: {self.balance}")
else:
self._log_transaction_fail(
f"Saldo Insuficiente {amount} : {self.balance}"
)
return self.balance
def get_balance(self):
self._log_transaction(f"Balance cheched {self.balance}")
return self.balance