Learn how to create a simple ATM simulation program in Java. This beginner-friendly guide includes source code, complete explanation, and practical examples.
Creating an ATM simulation program is one of the most common Java projects for beginners. It helps you understand key programming concepts like:
Conditional statements
Loops
User input
Methods (functions)
Basic object-oriented programming (OOP)
In this blog, we’ll build a simple ATM simulation in Java with full code and step-by-step explanation in simple terms.
This ATM simulation will:
Ask the user to log in using a PIN.
Display a menu:
Check Balance
Deposit Money
Withdraw Money
Exit
Perform operations based on the user’s choice.
Continue until the user chooses to exit.
import java.util.Scanner;
public class ATMSimulator {
// Initial account balance
private static double balance = 1000.0;
// Default PIN
private static final int PIN = 1234;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int enteredPin;
// Step 1: Ask user to enter the PIN
System.out.print("Enter your 4-digit PIN: ");
enteredPin = scanner.nextInt();
if (enteredPin == PIN) {
int choice;
// Loop until user chooses to exit
do {
// Display menu
System.out.println("\n===== ATM Menu =====");
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.print("Choose an option: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
checkBalance();
break;
case 2:
depositMoney(scanner);
break;
case 3:
withdrawMoney(scanner);
break;
case 4:
System.out.println("Thank you for using our ATM. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
} else {
System.out.println("Incorrect PIN. Access Denied.");
}
scanner.close();
}
// Method to check balance
public static void checkBalance() {
System.out.println("Your current balance is: ₹" + balance);
}
// Method to deposit money
public static void depositMoney(Scanner scanner) {
System.out.print("Enter amount to deposit: ₹");
double amount = scanner.nextDouble();
if (amount > 0) {
balance += amount;
System.out.println("₹" + amount + " deposited successfully.");
checkBalance();
} else {
System.out.println("Invalid deposit amount.");
}
}
// Method to withdraw money
public static void withdrawMoney(Scanner scanner) {
System.out.print("Enter amount to withdraw: ₹");
double amount = scanner.nextDouble();
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("₹" + amount + " withdrawn successfully.");
checkBalance();
} else {
System.out.println("Invalid amount or insufficient balance.");
}
}
}
private static double balance = 1000.0;
private static final int PIN = 1234;
We store the balance and PIN.
The PIN is final
so it can’t be changed.
System.out.print("Enter your 4-digit PIN: ");
enteredPin = scanner.nextInt();
This checks if the person trying to use the ATM knows the correct PIN.
do {
// Display menu
} while (choice != 4);
A loop is used to keep showing the menu until the user chooses option 4
(Exit).
switch (choice) {
case 1: checkBalance(); break;
case 2: depositMoney(scanner); break;
case 3: withdrawMoney(scanner); break;
case 4: // exit
Based on the user's selection, the relevant method is called.
checkBalance()
Shows current balance.
depositMoney(scanner)
Asks user for amount, adds it to balance.
withdrawMoney(scanner)
Checks if enough balance is there. If yes, subtracts amount.
You just created a basic ATM simulation in Java! 🚀
This project helps you understand input/output, decision making, loops, and methods—all essential Java concepts. You can improve this program by adding features like:
Multiple users/accounts
Transaction history
Login attempts limit
GUI using Java Swing
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!