From 13d5a011863c9dccc2333c0c2677f48c3ed0a5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D8=B1=D8=B2=D8=A7=D9=86=20=D8=A7=D9=84=D9=85=D8=B7=D9=8A?= =?UTF-8?q?=D8=B1=D9=8A?= Date: Mon, 2 Mar 2026 10:20:35 +0300 Subject: [PATCH] lad 102 done --- bank_account.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ main.py | 23 +++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 bank_account.py create mode 100644 main.py diff --git a/bank_account.py b/bank_account.py new file mode 100644 index 0000000..02c59a6 --- /dev/null +++ b/bank_account.py @@ -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 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..4a3a930 --- /dev/null +++ b/main.py @@ -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()) \ No newline at end of file