SimpleTuts.com

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:

Main Function

The main function manages user interaction and the overall flow of the banking system.

Key Components:

Menu Options: