ATM program Java

In Java, we can create an ATM program for representing ATM transection. In the ATM program, the user has to select an option from the options displayed on the screen. The options are related to withdraw the money, deposit the money, check the balance, and exit.

To withdraw the money, we simply get the withdrawal amount from the user and remove that amount from the total balance and print the successful message.

To deposit the money, we simply get the deposit amount from the user, add it to the total balance and print the successful message.

To check balance, we simply print the total balance of the user.

We use the exit(0) method to exit from the current Transaction mode and return the user to the home page or initial screen.

ATMExample.java

  1. //import required classes and packages   
  2. import java.util.Scanner;  
  3.   
  4. //create ATMExample class to implement the ATM functionality  
  5. public class ATMExample  
  6. {  
  7.     //main method starts   
  8.     public static void main(String args[] )  
  9.     {  
  10.         //declare and initialize balance, withdraw, and deposit  
  11.         int balance = 100000, withdraw, deposit;  
  12.           
  13.         //create scanner class object to get choice of user  
  14.         Scanner sc = new Scanner(System.in);  
  15.           
  16.         while(true)  
  17.         {  
  18.             System.out.println(“Automated Teller Machine”);  
  19.             System.out.println(“Choose 1 for Withdraw”);  
  20.             System.out.println(“Choose 2 for Deposit”);  
  21.             System.out.println(“Choose 3 for Check Balance”);  
  22.             System.out.println(“Choose 4 for EXIT”);  
  23.             System.out.print(“Choose the operation you want to perform:”);  
  24.               
  25.             //get choice from user  
  26.             int choice = sc.nextInt();  
  27.             switch(choice)  
  28.             {  
  29.                 case 1:  
  30.         System.out.print(“Enter money to be withdrawn:”);  
  31.                       
  32.         //get the withdrawl money from user  
  33.         withdraw = sc.nextInt();  
  34.                       
  35.         //check whether the balance is greater than or equal to the withdrawal amount  
  36.         if(balance >= withdraw)  
  37.         {  
  38.             //remove the withdrawl amount from the total balance  
  39.             balance = balance – withdraw;  
  40.             System.out.println(“Please collect your money”);  
  41.         }  
  42.         else  
  43.         {  
  44.             //show custom error message   
  45.             System.out.println(“Insufficient Balance”);  
  46.         }  
  47.         System.out.println(“”);  
  48.         break;  
  49.    
  50.                 case 2:  
  51.                       
  52.         System.out.print(“Enter money to be deposited:”);  
  53.                       
  54.         //get deposite amount from te user  
  55.         deposit = sc.nextInt();  
  56.                       
  57.         //add the deposit amount to the total balanace  
  58.         balance = balance + deposit;  
  59.         System.out.println(“Your Money has been successfully depsited”);  
  60.         System.out.println(“”);  
  61.         break;  
  62.    
  63.                 case 3:  
  64.         //displaying the total balance of the user  
  65.         System.out.println(“Balance : “+balance);  
  66.         System.out.println(“”);  
  67.         break;  
  68.    
  69.                 case 4:  
  70.         //exit from the menu  
  71.         System.exit(0);  
  72.             }  
  73.         }  
  74.     }  
  75. }  

Output:

ATM program Java
ATM program Java

Leave a Comment