Skip to content
Related Articles

Related Articles

Java Program to Display the ATM Transaction
  • Last Updated : 09 Nov, 2020

Lets Build a Java Program, to represent ATM Transaction, where a User has to choose input from the options displayed on the Screen. The available options on the Screen include operations such as Withdraw, deposit, balance.

Following are the basic operations available in the ATM

  1. Withdraw
  2. Deposit
  3. Check Balance
  4. Exit

Approach to each Option

A. Withdraw:

  1. Take the amount user desires to withdraw as input.
  2. If the balance amount greater than or equal to the withdrawal amount then Perform the transaction and give the user the desired amount.
  3. Else print Insufficient Funds message.

B. Deposit:



  1. Take the amount user desires to deposit as input.
  2. Add the received input from the user to balance and update its value.
  3. balance = balance + deposit.
  4. Print a message on screen stating deposit transaction has been successful.

C. Check Balance:

  • Print a message on screen showing the value of balance amount.

D. Exit:

  • Exit the current Transaction mode and return the user to the home page or initial screen.

Below is the implementation of the above approach.

Java




// Java Program to Display the ATM Transaction
import java.io.*;
public class GFG {
  
    // Display current balance in account
    public static void displayBalance(int balance)
    {
        System.out.println("Current Balance : " + balance);
        System.out.println();
    }
  
    // Withdraw amount and update the balance
    public static int amountWithdrawing(int balance,
                                        int withdrawAmount)
    {
        System.out.println("Withdrawn Operation:");
        System.out.println("Withdrawing Amount : "
                           + withdrawAmount);
        if (balance >= withdrawAmount) {
            balance = balance - withdrawAmount;
            System.out.println(
                "Please collect your money and collect the card");
            displayBalance(balance);
        }
        else {
            System.out.println("Sorry! Insufficient Funds");
            System.out.println();
        }
        return balance;
    }
  
    // Deposit amount and update the balance
    public static int amountDepositing(int balance,
                                       int depositAmount)
    {
        System.out.println("Deposit Operation:");
        System.out.println("Depositing Amount : "
                           + depositAmount);
        balance = balance + depositAmount;
        System.out.println(
            "Your Money has been successfully deposited");
        displayBalance(balance);
        return balance;
    }
  
    public static void main(String args[])
    {
        int balance = 10000;
        int withdrawAmount = 5000;
        int depositAmount = 2000;
  
        // calling display balance
        displayBalance(balance);
        // withdrawing amount
        balance
            = amountWithdrawing(balance, withdrawAmount);
        // depositing amount
        balance = amountDepositing(balance, depositAmount);
    }
}
Output
Current Balance : 10000

Withdrawn Operation:
Withdrawing Amount : 5000
Please collect your money and collect the card
Current Balance : 5000

Deposit Operation:
Depositing Amount : 2000
Your Money has been successfully deposited
Balance : 7000

Time Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :