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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# LAB_OOP_102


Create a Python class called `BankAccount` that simulates a simple bank account. The class should have the following functionalities:

1. It should have a constructor that accepts the `account_holder` name and initial balance (`initial_balance`), setting the balance to zero if the initial balance is not provided.
Expand All @@ -10,7 +9,8 @@ Create a Python class called `BankAccount` that simulates a simple bank account.
5. A method called `get_account_holder` that returns the name of the account holder.

**NOTE:**
- Do validation, and other OOP principles as needed.

- Do validation, and other OOP principles as needed.
- arrange your code in at least 2 files.

---------
---
33 changes: 33 additions & 0 deletions bank_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class BankAccount:
def __init__(self, account_holder, initial_balance=0):
if not account_holder or not isinstance(account_holder, str):
raise ValueError("Account holder name must be a valid string.")

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

self.__account_holder = account_holder
self.__balance = initial_balance

def deposit(self, amount):
if amount <= 0:
raise ValueError("Deposit amount must be greater than zero.")

self.__balance += amount
return self.__balance

def withdraw(self, amount):
if amount <= 0:
raise ValueError("Withdrawal amount must be greater than zero.")

if amount > self.__balance:
raise Exception("Insufficient funds.")

self.__balance -= amount
return self.__balance

def get_balance(self):
return self.__balance

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

def main():
try:
account = BankAccount("Sadeem", 1000)

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

# Deposit
print("After Deposit (500):", account.deposit(500))

# Withdraw
print("After Withdraw (300):", account.withdraw(300))

# Attempt to withdraw more than balance
print("Attempting to withdraw 2000...")
print(account.withdraw(2000))

except Exception as e:
print("Error:", e)


if __name__ == "__main__":
main()