In Java, we can create an ATM program to simulate ATM transactions. The program presents a menu of options to the user, including withdrawing money, depositing money, checking the balance, and exiting.
To withdraw money, the program prompts the user to enter the withdrawal amount. It subtracts the amount from the total balance and displays a success message.
To deposit money, the program asks the user to enter the deposit amount. It adds the amount to the total balance and displays a success message.
To check the balance, the program simply displays the current total balance.
To exit the program, the program uses the exit(0) method, which terminates the current transaction mode and returns the user to the home page or initial screen.
ATMExample.java
//import required classes and packages import java.util.Scanner; //create ATMExample class to implement the ATM functionality public class ATMExample { //main method starts public static void main(String args[] ) { //declare and initialize balance, withdraw, and deposit int balance = 100000, withdraw, deposit; //create scanner class object to get choice of user Scanner sc = new Scanner(System.in); while(true) { System.out.println("Automated Teller Machine"); System.out.println("Choose 1 for Withdraw"); System.out.println("Choose 2 for Deposit"); System.out.println("Choose 3 for Check Balance"); System.out.println("Choose 4 for EXIT"); System.out.print("Choose the operation you want to perform:"); //get choice from user int choice = sc.nextInt(); switch(choice) { case 1: System.out.print("Enter money to be withdrawn:"); //get the withdrawl money from user withdraw = sc.nextInt(); //check whether the balance is greater than or equal to the withdrawal amount if(balance >= withdraw) { //remove the withdrawl amount from the total balance balance = balance - withdraw; System.out.println("Please collect your money"); } else { //show custom error message System.out.println("Insufficient Balance"); } System.out.println(""); break; case 2: System.out.print("Enter money to be deposited:"); //get deposite amount from te user deposit = sc.nextInt(); //add the deposit amount to the total balanace balance = balance + deposit; System.out.println("Your Money has been successfully depsited"); System.out.println(""); break; case 3: //displaying the total balance of the user System.out.println("Balance : "+balance); System.out.println(""); break; case 4: //exit from the menu System.exit(0); } } } }
Output:
Automated Teller Machine Choose 1 for Withdraw Choose 2 for Deposit Choose 3 for Check Balance Choose 4 for EXIT Choose the operation you want to perform:1 Enter money to be withdrawn:2500 Please collect your money Automated Teller Machine Choose 1 for Withdraw Choose 2 for Deposit Choose 3 for Check Balance Choose 4 for EXIT Choose the operation you want to perform:2 Enter money to be deposited:25 Your Money has been successfully depsited Automated Teller Machine Choose 1 for Withdraw Choose 2 for Deposit Choose 3 for Check Balance Choose 4 for EXIT Choose the operation you want to perform: 3 Balance : 97525 Automated Teller Machine Choose 1 for Withdraw Choose 2 for Deposit Choose 3 for Check Balance Choose 4 for EXIT Choose the operation you want to perform:4
For further information on Java ATM Program and similar topics, please visit tutorials.freshersnow.com.