Back to Tutorials
Java

ATM Simulation System in Java

Build a console-based ATM system with PIN login, balance check, deposit, and withdrawal - using Object-Oriented Programming concepts.

~35 mins Intermediate Java OOP
Skills & Concepts You'll Gain
Tech Used: Java JDK 8+, Scanner class, OOP concepts, any Java IDE or terminal
What You Need
  • Java JDK 8 or later
  • Any text editor or IDE
  • Terminal / Command Prompt
  • No external libraries needed
Recommended IDE
  • IntelliJ IDEA (Community)
  • Eclipse IDE
  • VS Code + Java Extension
  • Notepad++ + terminal
Step 1

Setup - Check Java & Create Project

Open your terminal and check if Java is installed:

Terminal / CMD
java -version

You should see something like java version "17.0.x". If not, download Java from adoptium.net and install it.

Create a folder called ATMSystem and inside it create a file called Main.java. All three classes will go in this one file.

In Java, you can have multiple classes in one file, but only one public class - and it must match the filename. Our public class will be Main, so the file must be Main.java.
Step 2

Understand the Design - 3 Classes

We split the program into 3 classes, each with a specific job:

This separation is called Separation of Concerns - each class does one thing well. This is a core principle of good software design.
Step 3

Write the Code

Copy this complete code into your Main.java file:

Main.java
// ATM Simulation System - NKDevSpace Tutorial
import java.util.Scanner;

// ============================================================
// CLASS 1: Account - Manages the bank balance
// ============================================================
class Account {
    private double balance;  // private = only accessible inside this class

    // Constructor - sets the starting balance
    public Account(double initialBalance) {
        this.balance = initialBalance;
    }

    // Deposit money into the account
    public void deposit(double amount) {
        if (amount <= 0) {
            System.out.println("  [ERROR] Deposit amount must be greater than zero.");
            return;
        }
        balance += amount;
        System.out.printf("  [SUCCESS] ₹%.2f deposited successfully.%n", amount);
    }

    // Withdraw money from the account
    public void withdraw(double amount) {
        if (amount <= 0) {
            System.out.println("  [ERROR] Withdrawal amount must be greater than zero.");
            return;
        }
        if (amount > balance) {
            System.out.println("  [ERROR] Insufficient balance!");
            return;
        }
        balance -= amount;
        System.out.printf("  [SUCCESS] ₹%.2f withdrawn successfully.%n", amount);
    }

    // Display the current balance
    public void checkBalance() {
        System.out.printf("  Current Balance: ₹%.2f%n", balance);
    }
}

// ============================================================
// CLASS 2: ATM - Handles login and the menu
// ============================================================
class ATM {
    private Account account;
    private int correctPin = 1234;  // Default PIN
    private Scanner scanner;

    public ATM(Account account) {
        this.account = account;
        this.scanner = new Scanner(System.in);
    }

    // Start the ATM - first verify PIN, then show menu
    public void start() {
        System.out.println("========================================");
        System.out.println("       Welcome to NKDevSpace ATM        ");
        System.out.println("========================================");

        // PIN verification - allow 3 attempts
        int attempts = 0;
        while (attempts < 3) {
            System.out.print("  Enter your PIN: ");
            int enteredPin = scanner.nextInt();

            if (enteredPin == correctPin) {
                System.out.println("  [SUCCESS] PIN verified. Welcome!\n");
                showMenu();
                return;
            } else {
                attempts++;
                int remaining = 3 - attempts;
                if (remaining > 0) {
                    System.out.println("  [ERROR] Wrong PIN. " + remaining + " attempt(s) remaining.");
                } else {
                    System.out.println("  [BLOCKED] Too many wrong attempts. Card blocked.");
                }
            }
        }
    }

    // Show the main menu and handle user choices
    private void showMenu() {
        int choice;
        do {
            System.out.println("========================================");
            System.out.println("  1. Check Balance");
            System.out.println("  2. Deposit Money");
            System.out.println("  3. Withdraw Money");
            System.out.println("  4. Exit");
            System.out.println("========================================");
            System.out.print("  Choose an option (1-4): ");

            choice = scanner.nextInt();
            System.out.println();

            switch (choice) {
                case 1:
                    account.checkBalance();
                    break;

                case 2:
                    System.out.print("  Enter deposit amount: ₹");
                    double depositAmt = scanner.nextDouble();
                    account.deposit(depositAmt);
                    break;

                case 3:
                    System.out.print("  Enter withdrawal amount: ₹");
                    double withdrawAmt = scanner.nextDouble();
                    account.withdraw(withdrawAmt);
                    break;

                case 4:
                    System.out.println("  Thank you for using NKDevSpace ATM. Goodbye!");
                    break;

                default:
                    System.out.println("  [ERROR] Invalid option. Please choose 1 to 4.");
            }
            System.out.println();

        } while (choice != 4);
    }
}

// ============================================================
// CLASS 3: Main - Entry point of the program
// ============================================================
public class Main {
    public static void main(String[] args) {
        Account myAccount = new Account(1000.00);  // Start with ₹1000 balance
        ATM atm = new ATM(myAccount);
        atm.start();
    }
}
Step 4

Code Explanation

private double balance;
private means this variable can only be accessed inside the Account class. Nobody outside can directly change the balance - they must use the deposit() or withdraw() methods. This is called Encapsulation - one of the 4 pillars of OOP.
this.balance = initialBalance;
this refers to the current object. When the constructor parameter name is the same as the field name, we use this.balance to distinguish the field from the parameter. It means "set THIS object's balance to the value passed in."
System.out.printf("₹%.2f%n", amount)
printf is a formatted print. %.2f means "print a decimal number with exactly 2 decimal places." So 1000 prints as 1000.00. %n is a newline character (like pressing Enter).
switch (choice) { case 1: ... break; }
A switch statement is a cleaner alternative to multiple if-else blocks when checking one variable against many values. Each case handles one option. The break is important - without it, Java would "fall through" and execute the next case too.
do { ... } while (choice != 4);
A do-while loop always runs at least once before checking the condition. This is perfect for menus - we always want to show the menu at least once. The loop continues until the user chooses option 4 (Exit).
Account myAccount = new Account(1000.00);
This creates a new Account object with a starting balance of ₹1000. new allocates memory for the object and calls the constructor. We then pass this account to the ATM so the ATM can use it.
Step 5

Compile & Run

  1. Open your terminal and go to your project folder:
    CMD / Terminal
    cd ATMSystem
  2. Compile the Java file:
    CMD / Terminal
    javac Main.java
    If successful, you'll see Main.class, Account.class, ATM.class files created. No output = success!
  3. Run the program:
    CMD / Terminal
    java Main
  4. Enter PIN: 1234
  5. Try all 4 options - check balance, deposit, withdraw, and exit
Try entering the wrong PIN 3 times to see the "Card blocked" message. Then run the program again and enter the correct PIN 1234.
Step 6

Try It Yourself - Challenges

Previous Tutorial Next Tutorial