From e9cd8ad88601194f71f66b647287f51adca8bdcd Mon Sep 17 00:00:00 2001 From: Atheer Alshahrani Date: Mon, 2 Mar 2026 09:48:50 +0300 Subject: [PATCH 1/2] bank_account.py --- bank_account.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 bank_account.py diff --git a/bank_account.py b/bank_account.py new file mode 100644 index 0000000..66d5add --- /dev/null +++ b/bank_account.py @@ -0,0 +1,35 @@ +class BankAccount: + + def __init__(self, account_holder, initial_balance=0): + self.__account_holder = account_holder # Encapsulation + if initial_balance >= 0: + self.__balance = initial_balance + else: + self.__balance = 0 + + # Deposit Method + def deposit(self, amount): + if amount <= 0: + raise ValueError("Deposit amount must be positive.") + + self.__balance += amount + return self.__balance + + # Withdraw Method + def withdraw(self, amount): + if amount <= 0: + raise ValueError("Withdrawal amount must be positive.") + + if amount > self.__balance: + raise Exception("Insufficient funds.") + + self.__balance -= amount + return self.__balance + + # Get Balance + def get_balance(self): + return self.__balance + + # Get Account Holder + def get_account_holder(self): + return self.__account_holder \ No newline at end of file From b4e82cd910f1c0672aad881d13c4c426b1ba2c08 Mon Sep 17 00:00:00 2001 From: Atheer Alshahrani Date: Mon, 2 Mar 2026 09:51:50 +0300 Subject: [PATCH 2/2] main.py --- main.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..3799fb5 --- /dev/null +++ b/main.py @@ -0,0 +1,29 @@ +from bank_account import BankAccount + +# Create account +account1 = BankAccount("Reema", 1000) + +print("Account Holder:", account1.get_account_holder()) +print("Initial Balance:", account1.get_balance()) + +# Deposit +try: + new_balance = account1.deposit(500) + print("Balance after deposit:", new_balance) +except ValueError as e: + print("Error:", e) + +# Withdraw +try: + new_balance = account1.withdraw(200) + print("Balance after withdrawal:", new_balance) +except Exception as e: + print("Error:", e) + +# Withdraw more than balance (to test exception) +try: + account1.withdraw(5000) +except Exception as e: + print("Error:", e) + +print("Final Balance:", account1.get_balance()) \ No newline at end of file