How to build an atm program in python with classes and objects



atm program in python will give you good practice on how to use classes and objects, functions, while loops, modules, and conditional statements in general. It’s a good question to pick as a practice question since it covers almost all the concepts you need to know as a beginner in programming.

This program simulates how the ATM (Automatic Teller Machine) of the Bank of Bhutan works. Therefore, the program does the following basic ATM requirements:

  1. Account creation
  2. Check Account Details
  3. Check Balance
  4. Deposit Amount
  5. Withdraw Amount
  6. Exit with a transaction receipt

Video demonstration of the program:



Source Code:

import random
import sys

class ATM():
    def __init__(self, name, account_number, balance = 0):
        self.name = name
        self.account_number = account_number
        self.balance = balance
        
    def account_detail(self):
        print("\n----------ACCOUNT DETAIL----------")
        print(f"Account Holder: {self.name.upper()}")
        print(f"Account Number: {self.account_number}")
        print(f"Available balance: Nu.{self.balance}\n")
        
    def deposit(self, amount):
        self.amount = amount
        self.balance = self.balance + self.amount
        print("Current account balance: Nu.", self.balance)
        print()

    def withdraw(self, amount):
        self.amount = amount
        if self.amount > self.balance:
            print("Insufficient fund!")
            print(f"Your balance is Nu.{self.balance} only.")
            print("Try with lesser amount than balance.")
            print()
        else:
            self.balance = self.balance - self.amount
            print(f"Nu.{amount} withdrawal successful!")
            print("Current account balance: Nu.", self.balance)
            print()

    def check_balance(self):
        print("Available balance: Nu.", self.balance)
        print()

    def transaction(self):
        print("""
            TRANSACTION 
        *********************
            Menu:
            1. Account Detail
            2. Check Balance
            3. Deposit
            4. Withdraw
            5. Exit
        *********************
        """)
       
        while True:
            try:
                option = int(input("Enter 1, 2, 3, 4 or 5:"))
            except:
                print("Error: Enter 1, 2, 3, 4, or 5 only!\n")
                continue
            else:
                if option == 1:
                    atm.account_detail()
                elif option == 2:
                    atm.check_balance()
                elif option == 3:
                    amount = int(input("How much you want to deposit(Nu.):"))
                    atm.deposit(amount)
                elif option == 4:
                    amount = int(input("How much you want to withdraw(Nu.):"))
                    atm.withdraw(amount)
                elif option == 5:
                    print(f"""
                printing receipt..............
          ******************************************
              Transaction is now complete.                         
              Transaction number: {random.randint(10000, 1000000)} 
              Account holder: {self.name.upper()}                  
              Account number: {self.account_number}                
              Available balance: Nu.{self.balance}                    

              Thanks for choosing us as your bank                  
          ******************************************
          """)
                    sys.exit()
                

print("*******WELCOME TO BANK OF BHUTAN*******")
print("___________________________________________________________\n")
print("----------ACCOUNT CREATION----------")
name = input("Enter your name: ")
account_number = input("Enter your account number: ")
print("Congratulations! Account created successfully......\n")

atm = ATM(name, account_number)

while True:
    trans = input("Do you want to do any transaction?(y/n):")
    if trans == "y":
        atm.transaction()
    elif trans == "n":
        print("""
    -------------------------------------
   | Thanks for choosing us as your bank |
   | Visit us again!                     |
    -------------------------------------
        """)
        break
    else:
        print("Wrong command!  Enter 'y' for yes and 'n' for NO.\n")
    


Output:

*******WELCOME TO BANK OF BHUTAN*******
___________________________________________________________

----------ACCOUNT CREATION----------
Enter your name: Dawa Penjor
Enter your account number: 23435465
Congratulations! Account created successfully......

Do you want to do any transaction?(y/n):f
Wrong command!  Enter 'y' for yes and 'n' for NO.

Do you want to do any transaction?(y/n):y

            TRANSACTION 
        *********************
            Menu:
            1. Account Detail
            2. Check Balance
            3. Deposit
            4. Withdraw
            5. Exit
        *********************
        
Enter 1, 2, 3, 4 or 5:1

----------ACCOUNT DETAIL----------
Account Holder: DAWA PENJOR
Account Number: 23435465
Available balance: Nu.0

Enter 1, 2, 3, 4 or 5:2
Available balance: Nu. 0

Enter 1, 2, 3, 4 or 5:3
How much you want to deposit(Nu.):7000
Current account balance: Nu. 7000

Enter 1, 2, 3, 4 or 5:3
How much you want to deposit(Nu.):5000
Current account balance: Nu. 12000

Enter 1, 2, 3, 4 or 5:2
Available balance: Nu. 12000

Enter 1, 2, 3, 4 or 5:4
How much you want to withdraw(Nu.):15000
Insufficient fund!
Your balance is Nu.12000 only.
Try with lesser amount than balance.

Enter 1, 2, 3, 4 or 5:4
How much you want to withdraw(Nu.):10000
Nu.10000 withdrawal successful!
Current account balance: Nu. 2000

Enter 1, 2, 3, 4 or 5:1

----------ACCOUNT DETAIL----------
Account Holder: DAWA PENJOR
Account Number: 23435465
Available balance: Nu.2000

Enter 1, 2, 3, 4 or 5:5

                printing receipt..............
          ******************************************
              Transaction is now complete.                         
              Transaction number: 754981 
              Account holder: DAWA PENJOR                  
              Account number: 23435465                
              Available balance: Nu.2000                    

              Thanks for choosing us as your bank                  
          ******************************************