Google News
logo
Basic ATM Program in Java
The following example of a basic implementation of an ATM program in Java :
Program :
import java.util.Scanner;

public class ATM {
    private static Scanner scanner = new Scanner(System.in);
    private static int balance = 10000;
    
    public static void main(String[] args) {
        while (true) {
            System.out.println("1. Check Balance");
            System.out.println("2. Withdraw");
            System.out.println("3. Deposit");
            System.out.println("4. Exit");
            
            int choice = scanner.nextInt();
            
            switch (choice) {
                case 1:
                    checkBalance();
                    break;
                case 2:
                    withdraw();
                    break;
                case 3:
                    deposit();
                    break;
                case 4:
                    System.exit(0);
                default:
                    System.out.println("Invalid Choice!");
            }
        }
    }
    
    private static void checkBalance() {
        System.out.println("Current Balance: " + balance);
    }
    
    private static void withdraw() {
        System.out.println("Enter Amount to Withdraw:");
        int amount = scanner.nextInt();
        
        if (amount > balance) {
            System.out.println("Insufficient Balance!");
            return;
        }
        
        balance -= amount;
        System.out.println("Withdrawn Successfully! Current Balance: " + balance);
    }
    
    private static void deposit() {
        System.out.println("Enter Amount to Deposit:");
        int amount = scanner.nextInt();
        
        balance += amount;
        System.out.println("Deposited Successfully! Current Balance: " + balance);
    }
}
Output :
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
1
Current Balance: 10000

1. Check Balance
2. Withdraw
3. Deposit
4. Exit
2
Enter Amount to Withdraw:
1500
Withdrawn Successfully! Current Balance: 8500

1. Check Balance
2. Withdraw
3. Deposit
4. Exit
3
Enter Amount to Deposit:
5700
Deposited Successfully! Current Balance: 14200

1. Check Balance
2. Withdraw
3. Deposit
4. Exit
This ATM program provides four options to the user : check balance, withdraw, deposit, and exit.

The program keeps running until the user chooses to exit. When the user selects an option, the corresponding method is called, which performs the required operation.

The balance is stored in a static variable, which is updated whenever a deposit or withdrawal is made.