Write a python program to bank management system


This Python program simulates a simple bank management system using a Bank class. It allows users to create accounts, deposit money, withdraw money, check balances, and exit the program. Here's an explanation of the program:

  • class Bank: This is the main class representing the bank. It has the following methods:
    • __init__(self): The constructor initializes an empty accounts dictionary to store account information.
    • create_account(self, account_number, account_holder, initial_balance): This method creates a new account and stores it in the accounts dictionary. It checks for account number duplication and negative initial balances.
    • deposit(self, account_number, amount): This method allows deposits into an existing account, updating the account balance.
    • withdraw(self, account_number, amount): This method allows withdrawals from an existing account, updating the account balance. It also checks for sufficient balance and non-negative withdrawal amounts.
    • check_balance(self, account_number): This method checks the balance of an existing account and returns the account holder's name and balance as a formatted string.
  • The program creates a Bank object named bank.
  • The program presents a menu to the user with the following options: create account, deposit money, withdraw money, check balance, and exit.
  • In a loop, the program reads the user's choice and performs the selected operation:
    • For "Create Account," it collects account details and calls the create_account method.
    • For "Deposit Money," it collects the account number and deposit amount and calls the deposit method.
    • For "Withdraw Money," it collects the account number and withdrawal amount and calls the withdraw method.
    • For "Check Balance," it collects the account number and calls the check_balance method.
    • For "Exit," it exits the program.
  • The program ensures input validation and provides appropriate feedback to the user.

When you run this program, it allows you to simulate a basic bank management system by creating accounts, depositing money, withdrawing money, and checking balances. It uses the Bank class to manage accounts and account operations.


Source Code

class Bank:
    def __init__(self):
        self.accounts = {}
 
    def create_account(self, account_number, account_holder, initial_balance):
        if account_number in self.accounts:
            return "Account already exists"
        if initial_balance < 0:
            return "Initial balance must be non-negative"
 
        self.accounts[account_number] = {
            'account_holder': account_holder,
            'balance': initial_balance
        }
        return "Account created successfully"
 
    def deposit(self, account_number, amount):
        if account_number not in self.accounts:
            return "Account does not exist"
        if amount <= 0:
            return "Amount to deposit must be positive"
 
        self.accounts[account_number]['balance'] += amount
        return "Deposited " + str(amount) + " successfully. New balance : " + str(self.accounts[account_number]['balance'])
 
    def withdraw(self, account_number, amount):
        if account_number not in self.accounts:
            return "Account does not exist"
        if amount <= 0:
            return "Amount to withdraw must be positive"
 
        if self.accounts[account_number]['balance'] < amount:
            return "Insufficient balance."
 
        self.accounts[account_number]['balance'] -= amount
        return "Withdrew " + str(amount) + " successfully. New balance : " + str(self.accounts[account_number]['balance'])
 
    def check_balance(self, account_number):
        if account_number not in self.accounts:
            return "Account does not exist"
 
        return "Account Holder : " + self.accounts[account_number]['account_holder'] + "\nBalance : " + str(self.accounts[account_number]['balance'])
 
bank = Bank()	# Create a Bank object
 
print("\n****** Bank Management System ******")
print("\n1. Create Account")
print("2. Deposit Money")
print("3. Withdraw Money")
print("4. Check Balance")
print("5. Exit")
 
while True:
 
    choice = input("\nEnter your Choice (1 to 5): ")
 
    if choice == '1':
        account_number = input("Enter Account Number : ")
        account_holder = input("Enter Account Holder's Name : ")
        initial_balance = float(input("Enter Initial Balance : "))
        result = bank.create_account(account_number, account_holder, initial_balance)
        print(result)
 
    elif choice == '2':
        account_number = input("Enter Account Number : ")
        amount = float(input("Enter Amount to Deposit : "))
        result = bank.deposit(account_number, amount)
        print(result)
 
    elif choice == '3':
        account_number = input("Enter Account Number : ")
        amount = float(input("Enter Amount to Withdraw : "))
        result = bank.withdraw(account_number, amount)
        print(result)
 
    elif choice == '4':
        account_number = input("Enter Account Number : ")
        result = bank.check_balance(account_number)
        print(result)
 
    elif choice == '5':
        print("Exiting the program.")
        break
 
    else:
        print("Invalid Choice. Please Try Again !!!")
 

Output

****** Bank Management System ******

1. Create Account
2. Deposit Money
3. Withdraw Money
4. Check Balance
5. Exit

Enter your Choice (1 to 5): 1
Enter Account Number : 123456789
Enter Account Holder's Name : Riya
Enter Initial Balance : 10000
Account created successfully

Enter your Choice (1 to 5): 2
Enter Account Number : 123456789
Enter Amount to Deposit : 25000
Deposited 25000.0 successfully. New balance : 35000.0

Enter your Choice (1 to 5): 3
Enter Account Number : 123456789
Enter Amount to Withdraw : 5000
Withdrew 5000.0 successfully. New balance : 30000.0

Enter your Choice (1 to 5): 4
Enter Account Number : 123456789
Account Holder : Riya
Balance : 30000.0

Enter your Choice (1 to 5): 5
Exiting the program.

Example Programs