Build a console-based ATM system with PIN login, balance check, deposit, and withdrawal - using Object-Oriented Programming concepts.
Open your terminal and check if Java is installed:
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.
We split the program into 3 classes, each with a specific job:
Copy this complete code into your Main.java file:
// 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();
}
}
private double balance;this.balance = initialBalance;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)switch (choice) { case 1: ... break; }do { ... } while (choice != 4);Account myAccount = new Account(1000.00);cd ATMSystem
javac Main.java
java Main