Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions bank_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class BankAccount:
def __init__(self, account_holder: str, initial_balance: float = 0):
# Validation: holder name
if not isinstance(account_holder, str) or not account_holder.strip():
raise ValueError("account_holder must be a non-empty string")

# If initial_balance not provided, default is 0 (already)
if not isinstance(initial_balance, (int, float)):
raise TypeError("initial_balance must be a number")

if initial_balance < 0:
raise ValueError("initial_balance cannot be negative")

self._account_holder = account_holder.strip() # private-ish attribute
self._balance = float(initial_balance)

def deposit(self, amount: float) -> float:
# Validation
if not isinstance(amount, (int, float)):
raise TypeError("deposit amount must be a number")

if amount <= 0:
raise ValueError("deposit amount must be greater than 0")

self._balance += float(amount)
return self._balance

def withdraw(self, amount: float) -> float:
# Validation
if not isinstance(amount, (int, float)):
raise TypeError("withdraw amount must be a number")

if amount <= 0:
raise ValueError("withdraw amount must be greater than 0")

# Business rule: must have enough money
if amount > self._balance:
raise Exception("Insufficient funds")

self._balance -= float(amount)
return self._balance

def get_balance(self) -> float:
return self._balance

def get_account_holder(self) -> str:
return self._account_holder
23 changes: 23 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from bank_account import BankAccount

# Create account
account = BankAccount("Razan", 100)

print("Account holder:", account.get_account_holder())
print("Balance:", account.get_balance())

# Deposit
new_balance = account.deposit(50)
print("After deposit:", new_balance)

# Withdraw (successful)
new_balance = account.withdraw(30)
print("After withdraw:", new_balance)

# Withdraw (insufficient funds) + handle exception
try:
account.withdraw(500)
except Exception as e:
print("Withdraw failed:", e)

print("Final balance:", account.get_balance())