Create a Program for ATM Simulation in Java Programming
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.
π§ What Will This ATM Program Do?
This ATM simulation will:
-
Ask the user to log in using a PIN.
-
Display a menu:
-
Check Balance
-
Deposit Money
-
Withdraw Money
-
Google Advertisement
Exit
-
-
Perform operations based on the user’s choice.
-
Continue until the user chooses to exit.
π» Java Code: ATM Simulation
π Step-by-Step Explanation
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.");
}
}
}
β Step 1: Define Required Variables
-
We store the balance and PIN.
-
The PIN is
final
so it can’t be changed.
β Step 2: Ask User to Enter PIN
-
This checks if the person trying to use the ATM knows the correct PIN.
β Step 3: Show ATM Menu Using Loop
-
A loop is used to keep showing the menu until the user chooses option
4
(Exit).
β Step 4: Perform Actions Based on User Choice
-
Google Advertisement
Based on the user's selection, the relevant method is called.
β Step 5: Create Helper Methods
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.
π Conclusion
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