-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.py
More file actions
40 lines (34 loc) · 1.48 KB
/
BankAccount.py
File metadata and controls
40 lines (34 loc) · 1.48 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
# BankAccount.py
import datetime
class InvalidTransactionError(Exception):
"""Custom exception for invalid banking transactions."""
pass
class BankAccount:
def __init__(self, name, acc_number, initial_balance=0):
self.name = name
self.acc_number = acc_number
self.balance = initial_balance
self.transactions = [] # List of tuples: (timestamp, type, amount)
def deposit(self, amount):
if amount <= 0:
raise InvalidTransactionError("Deposit amount must be positive.")
self.balance += amount
self.transactions.append((datetime.datetime.now(), "Deposit", amount))
def withdraw(self, amount):
if amount <= 0:
raise InvalidTransactionError("Withdrawal amount must be positive.")
if amount > self.balance:
raise InvalidTransactionError("Insufficient funds.")
self.balance -= amount
self.transactions.append((datetime.datetime.now(), "Withdraw", amount))
def show_balance(self):
print(f"Account holder: {self.name}")
print(f"Current balance: ${self.balance:.2f}")
def show_transactions(self):
if not self.transactions:
print("No transactions yet.")
return
print(f"Transaction history for {self.name}:")
for t in self.transactions:
timestamp, t_type, amount = t
print(f"{timestamp.strftime('%Y-%m-%d %H:%M:%S')} - {t_type} - ${amount:.2f}")