Banking Program in Python
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"${amount} deposited. New balance: ${self.balance}")
else:
print("Deposit amount must be positive.")
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
print(f"${amount} withdrawn. New balance: ${self.balance}")
else:
print("Invalid withdrawal amount or insufficient funds.")
def get_balance(self):
return self.balance
def transfer(self, amount, other_account):
if 0 < amount <= self.balance:
self.balance -= amount
other_account.balance += amount
print(f"${amount} transferred to {other_account.owner}. New balance: ${self.balance}")
else:
print("Invalid transfer amount or insufficient funds.")
def __str__(self):
return f"Account owner: {self.owner}\nAccount balance: ${self.balance}"
def main():
print("Welcome to the Banking System")
accounts = {}
while True:
print("\nOptions:")
print("1. Create Account")
print("2. Deposit")
print("3. Withdraw")
print("4. Check Balance")
print("5. Transfer")
print("6. Exit")
choice = input("Choose an option: ")
if choice == "1":
owner = input("Enter the account owner's name: ")
if owner in accounts:
print("Account already exists.")
else:
accounts[owner] = BankAccount(owner)
print(f"Account created for {owner}.")
elif choice == "2":
owner = input("Enter the account owner's name: ")
if owner in accounts:
amount = float(input("Enter the amount to deposit: "))
accounts[owner].deposit(amount)
else:
print("Account not found.")
elif choice == "3":
owner = input("Enter the account owner's name: ")
if owner in accounts:
amount = float(input("Enter the amount to withdraw: "))
accounts[owner].withdraw(amount)
else:
print("Account not found.")
elif choice == "4":
owner = input("Enter the account owner's name: ")
if owner in accounts:
print(f"Current balance: ${accounts[owner].get_balance()}")
else:
print("Account not found.")
elif choice == "5":
from_owner = input("Enter the sender's account owner's name: ")
to_owner = input("Enter the receiver's account owner's name: ")
if from_owner in accounts and to_owner in accounts:
amount = float(input("Enter the amount to transfer: "))
accounts[from_owner].transfer(amount, accounts[to_owner])
else:
print("One or both accounts not found.")
elif choice == "6":
print("Thank you for using the Banking System. Goodbye!")
break
else:
print("Invalid option. Please choose again.")
if __name__ == "__main__":
main()
Python Online Compiler
Explanation
BankAccount Class
The BankAccount
class encapsulates the details and operations for a single bank account.
Methods:
-
__init__(self, owner, balance=0)
: This is the constructor method that initializes the account with the owner's name and an optional starting balance (default is 0).self.owner
stores the account owner's name.self.balance
stores the account balance.
-
deposit(self, amount)
: Adds the specified amount to the account balance if the amount is positive.- Prints a confirmation message showing the deposited amount and the new balance.
- Prints an error message if the deposit amount is not positive.
-
withdraw(self, amount)
: Subtracts the specified amount from the account balance if the amount is positive and less than or equal to the current balance.- Prints a confirmation message showing the withdrawn amount and the new balance.
- Prints an error message if the withdrawal amount is invalid or if there are insufficient funds.
-
get_balance(self)
: Returns the current balance of the account. -
transfer(self, amount, other_account)
: Transfers the specified amount from the current account to another account if the amount is positive and less than or equal to the current balance.- Prints a confirmation message showing the transferred amount, the recipient's account owner, and the new balance.
- Prints an error message if the transfer amount is invalid or if there are insufficient funds.
-
__str__(self)
: Returns a string representation of the account, including the owner's name and the balance.
Main Function
The main
function manages user interaction and the overall flow of the banking system.
Key Components:
- Welcome Message: Prints a welcome message when the program starts.
- Accounts Dictionary: A dictionary named
accounts
is used to store multipleBankAccount
objects. The keys are the account owners' names, and the values are the correspondingBankAccount
objects. - Main Menu Loop: The program enters an infinite loop that displays a menu of options and processes user input.
Menu Options:
-
Option 1: Create Account
- Prompts the user to enter the account owner's name.
- Checks if the account already exists. If not, it creates a new
BankAccount
object and adds it to theaccounts
dictionary.
-
Option 2: Deposit
- Prompts the user to enter the account owner's name and the deposit amount.
- If the account exists, it calls the
deposit
method on the correspondingBankAccount
object.
-
Option 3: Withdraw
- Prompts the user to enter the account owner's name and the withdrawal amount.
- If the account exists, it calls the
withdraw
method on the correspondingBankAccount
object.
-
Option 4: Check Balance
- Prompts the user to enter the account owner's name.
- If the account exists, it calls the
get_balance
method on the correspondingBankAccount
object and prints the balance.
-
Option 5: Transfer
- Prompts the user to enter the sender's account owner's name, the recipient's account owner's name, and the transfer amount.
- If both accounts exist, it calls the
transfer
method on the sender'sBankAccount
object, passing the recipient'sBankAccount
object and the transfer amount as arguments.
-
Option 6: Exit
- Exits the program with a goodbye message.
-
Invalid Option
- Prints an error message if the user enters an invalid menu option.