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
38 changes: 38 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from my_class import BankAccount

# ask user for account holder name
name = input("Enter account holder name: ")
initial = float(input("Enter initial balance (or 0): "))

account = BankAccount(name, initial)

while True:
print("\nOptions: deposit, withdraw, balance, account holder, exit")
choice = input("Choose an option: ").strip().lower()

if choice == "deposit":
amount = float(input("Enter amount to deposit: "))
try:
print(account.deposit(amount))
except Exception as e:
print(e)

elif choice == "withdraw":
amount = float(input("Enter amount to withdraw: "))
try:
print(account.withdraw(amount))
except Exception as e:
print(e)

elif choice == "balance":
print(account.get_balance())

elif choice == "account holder":
print(account.get_account_holder())

elif choice == "exit":
print("Exiting program.")
break

else:
print("Invalid option, try again.")
30 changes: 30 additions & 0 deletions my_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# create BankAccount class
class BankAccount:
# initializer / constructor
def __init__(self, account_holder: str, initial_balance: float = 0.0):
self.__account_holder = account_holder # private attribute -> encapsulation
self.__balance = initial_balance # private attribute -> encapsulation

# deposit method with validation
def deposit(self, amount: float):
if amount <= 0:
raise ValueError("deposit amount must be positive")
self.__balance += amount
return self.__balance

# withdraw method with validation and exception
def withdraw(self, amount: float):
if amount <= 0:
raise ValueError("withdraw amount must be positive")
if amount > self.__balance:
raise Exception("insufficient funds")
self.__balance -= amount
return self.__balance

# get current balance
def get_balance(self):
return self.__balance

# get account holder name
def get_account_holder(self):
return self.__account_holder