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
35 changes: 35 additions & 0 deletions bank_account.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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())