Number Programs
Java Program to Display ATM Menu
A Java program to simulate an ATM machine.
Problem Description
Write a Java program to simulate an ATM machine with options to Withdraw, Deposit, Check Balance, and Exit.
Code
import java.util.Scanner;
public class ATMExample {
public static void main(String[] args) {
int balance = 100000, withdraw, deposit;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("ATM Machine\n");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
System.out.print("Choose the operation: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter money to be withdrawn: ");
withdraw = sc.nextInt();
if (balance >= withdraw) {
balance = balance - withdraw;
System.out.println("Please collect your money");
} else {
System.out.println("Insufficient Balance");
}
System.out.println("");
break;
case 2:
System.out.print("Enter money to be deposited: ");
deposit = sc.nextInt();
balance = balance + deposit;
System.out.println("Your Money has been successfully deposited");
System.out.println("");
break;
case 3:
System.out.println("Balance : " + balance);
System.out.println("");
break;
case 4:
System.exit(0);
}
}
}
}Output
ATM Machine
1. Withdraw
2. Deposit
3. Check Balance
4. Exit
Choose the operation: 3
Balance : 100000Explanation
- Loop: Infinite loop to keep the menu active.
- Switch: Handle user choice.
