Skip to content
Java project #02Beginner

ATM Machine Simulator

PIN verification with lock-out, balance enquiry, cash withdrawal in notes, deposits, a daily limit, PIN change and a five-entry mini statement.

Estimated time
~3 hours to build
Files
4 files · 358 lines
Technologies
Java 21, Scanner, ArrayDeque, Switch expressions

Introduction

An ATM is a small state machine full of rules: three PIN attempts, withdrawals in multiples of 100, a daily limit, dispensing the fewest notes. Simulating one is an excellent exercise in menus, validation and keeping state in an object.

Problem statement

Simulate an ATM for one account. The user has three attempts to enter the correct PIN, after which the card is blocked. Once in, a menu offers balance, withdraw, deposit, mini statement, change PIN and exit. Withdrawals must be positive multiples of 100, within the balance and within a daily limit of ₹25,000, and are paid with the fewest ₹500/₹200/₹100 notes. The mini statement shows the latest five transactions, newest first. Invalid input must never crash the program.

Objectives

  • Drive a program with a menu loop and switch
  • Keep account state in a class
  • Use ArrayDeque as a bounded history
  • Implement a greedy note-selection algorithm

Features

  • Three PIN attempts and card blocking
  • Withdrawal rules: multiples of 100, balance, daily limit
  • Greedy note breakdown (500/200/100)
  • Validated deposits and 4-digit PIN change
  • Mini statement of the last five transactions

Concepts demonstrated

  • Menu-driven programs
  • Encapsulated state
  • Validation
  • Greedy algorithms
  • Deque as a bounded list

System architecture

A nested Account class holds the holder's name, PIN, balance, amount withdrawn today and a Deque<String> history. record adds an entry at the front and drops the oldest when there are more than five. main handles PIN entry, then loops over the menu with an arrow-form switch that calls withdraw, deposit or changePin. readAmount centralises parsing and validation of amounts. notesFor divides the amount by each note value in turn — the greedy method, which is optimal for these denominations.

Folder structure

TextProject tree
atm-machine/
├── README.md
├── src/Main.java
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

src/Main.java

153 lines
Javasrc/Main.java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
/**
* ATM Machine — PIN verification with lock-out, balance enquiry, cash withdrawal in notes
* of 500/200/100, deposits, PIN change and a mini statement of the last five transactions.
* Money is held in whole rupees as a long.
*/
public class Main {
static final int MAX_PIN_ATTEMPTS = 3;
static final long DAILY_LIMIT = 25_000;
static final int[] NOTES = {500, 200, 100};
static class Account {
private final String holder;
private String pin;
private long balance;
private long withdrawnToday;
private final Deque<String> history = new ArrayDeque<>();
private int txCounter = 0;
Account(String holder, String pin, long balance) {
this.holder = holder;
this.pin = pin;
this.balance = balance;
}
boolean checkPin(String attempt) { return pin.equals(attempt); }
void record(String entry) {
history.addFirst(String.format("#%03d %-28s balance %,d", ++txCounter, entry, balance));
if (history.size() > 5) history.removeLast(); // keep only the latest five
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Account acc = new Account("Asha Rao", "4321", 40_000);
System.out.println("=== KENHOLT Bank ATM ===");
int attempts = 0;
boolean authenticated = false;
while (attempts < MAX_PIN_ATTEMPTS && in.hasNextLine()) {
System.out.print("Enter PIN: ");
String pin = in.nextLine().trim();
System.out.println();
attempts++;
if (acc.checkPin(pin)) {
authenticated = true;
break;
}
int left = MAX_PIN_ATTEMPTS - attempts;
System.out.println(left > 0 ? "Wrong PIN. " + left + " attempt(s) left." : "Wrong PIN. Card blocked.");
}
if (!authenticated) return;
System.out.println("Welcome, " + acc.holder + "!");
while (true) {
System.out.print("\n1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit\nChoice: ");
if (!in.hasNextLine()) break;
String choice = in.nextLine().trim();
System.out.println();
switch (choice) {
case "1" -> System.out.printf("Available balance: Rs %,d%n", acc.balance);
case "2" -> withdraw(in, acc);
case "3" -> deposit(in, acc);
case "4" -> {
System.out.println("Last transactions (newest first):");
if (acc.history.isEmpty()) System.out.println(" none yet");
acc.history.forEach(h -> System.out.println(" " + h));
}
case "5" -> changePin(in, acc);
case "0" -> {
System.out.println("Thank you for banking with us. Please take your card.");
return;
}
default -> System.out.println("Invalid choice '" + choice + "'.");
}
}
}
static Long readAmount(Scanner in, String prompt) {
System.out.print(prompt);
if (!in.hasNextLine()) return null;
String text = in.nextLine().trim();
System.out.println();
try {
long v = Long.parseLong(text);
if (v <= 0) {
System.out.println("Amount must be positive.");
return null;
}
return v;
} catch (NumberFormatException e) {
System.out.println("'" + text + "' is not a valid amount.");
return null;
}
}
static void withdraw(Scanner in, Account acc) {
Long amount = readAmount(in, "Amount to withdraw: ");
if (amount == null) return;
if (amount % 100 != 0) {
System.out.println("Please enter a multiple of 100.");
} else if (amount > acc.balance) {
System.out.printf("Insufficient funds. Balance is Rs %,d.%n", acc.balance);
} else if (acc.withdrawnToday + amount > DAILY_LIMIT) {
System.out.printf("Daily limit exceeded. You can withdraw Rs %,d more today.%n", DAILY_LIMIT - acc.withdrawnToday);
} else {
acc.balance -= amount;
acc.withdrawnToday += amount;
System.out.println("Please collect your cash: " + notesFor(amount));
acc.record("Withdrawal Rs " + amount);
}
}
// Greedy note selection: the largest notes first.
static String notesFor(long amount) {
StringBuilder sb = new StringBuilder();
for (int note : NOTES) {
long count = amount / note;
amount %= note;
if (count > 0) sb.append(sb.isEmpty() ? "" : ", ").append(count).append(" x ").append(note);
}
return sb.toString();
}
static void deposit(Scanner in, Account acc) {
Long amount = readAmount(in, "Amount to deposit: ");
if (amount == null) return;
acc.balance += amount;
System.out.printf("Deposited Rs %,d. New balance Rs %,d.%n", amount, acc.balance);
acc.record("Deposit Rs " + amount);
}
static void changePin(Scanner in, Account acc) {
System.out.print("Current PIN: ");
String current = in.hasNextLine() ? in.nextLine().trim() : "";
System.out.print("\nNew PIN: ");
String next = in.hasNextLine() ? in.nextLine().trim() : "";
System.out.println();
if (!acc.checkPin(current)) System.out.println("Current PIN is incorrect.");
else if (!next.matches("\\d{4}")) System.out.println("A PIN must be exactly 4 digits.");
else if (next.equals(current)) System.out.println("The new PIN must be different.");
else {
acc.pin = next;
System.out.println("PIN changed successfully.");
acc.record("PIN changed");
}
}
}

What each file does

src/Main.java
The Account class, PIN check, menu loop, withdrawal/deposit/PIN-change logic and the note breakdown.

How to run it

Compile

Terminal
javac -d out $(find src -name "*.java")
# Windows PowerShell: javac -d out (Get-ChildItem -Recurse src -Filter *.java).FullName

Run

Terminal
java -cp out Main

Test against the expected output

Terminal
java -cp out Main < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt
# Windows PowerShell:
# Get-Content tests\sample-input.txt | java -cp out Main > actual.txt

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== KENHOLT Bank ATM ===
Enter PIN:
Wrong PIN. 2 attempt(s) left.
Enter PIN:
Welcome, Asha Rao!
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Available balance: Rs 40,000
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Amount to withdraw:
Please enter a multiple of 100.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Amount to withdraw:
Please collect your cash: 9 x 500, 1 x 200
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Amount to deposit:
'abc' is not a valid amount.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Amount to deposit:
Deposited Rs 1,500. New balance Rs 36,800.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Amount to withdraw:
Daily limit exceeded. You can withdraw Rs 20,300 more today.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Amount to withdraw:
Please collect your cash: 18 x 500
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Current PIN:
New PIN:
A PIN must be exactly 4 digits.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Current PIN:
New PIN:
PIN changed successfully.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Last transactions (newest first):
#004 PIN changed balance 27,800
#003 Withdrawal Rs 9000 balance 27,800
#002 Deposit Rs 1500 balance 36,800
#001 Withdrawal Rs 4700 balance 35,300
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Invalid choice '7'.
1) Balance 2) Withdraw 3) Deposit 4) Mini statement 5) Change PIN 0) Exit
Choice:
Thank you for banking with us. Please take your card.
Input
1111
4321
1
2
2550
2
4700
3
abc
3
1500
2
30000
2
9000
5
4321
98
5
4321
2468
4
7
0

Testing

  • Run with tests/sample-input.txt and compare with tests/expected-output.txt.
  • The first PIN is wrong and the second is right, so two attempts are reported.
  • Withdrawing 2550 is rejected (not a multiple of 100); 4700 is paid as 9 × 500 and 1 × 200.
  • After 4700, a request for 30,000 exceeds the ₹25,000 daily limit, and 9000 succeeds.
  • The mini statement lists the four transactions, newest first.

Common errors

The menu reads an empty choice right after a number

Read every input with nextLine() and parse it, so no leftover newline remains in the buffer.

Mini statement keeps growing

Remove the oldest entry (removeLast) whenever the deque holds more than five.

Possible improvements

  • Support several accounts selected by card number
  • Reset the daily limit at midnight using java.time
  • Keep a fixed stock of notes in the machine and refuse amounts it can't pay

Additional challenges

  1. 1Pay withdrawals with at most 10 notes, preferring 200s when 500s run out.
  2. 2Add a fund-transfer option between two accounts.
  3. 3Lock the card for 24 hours instead of permanently.

Built and tested it yourself? Mark it to update your dashboard.