ATM Program in Java
In this article, we will learn how to build a simple ATM machine program in Java. This program lets users choose from the options displayed on the screen. Before choosing the options, the user has to enter their ATM PIN for verification. The default PIN will be '1234', which can be changed if needed. If the PIN is wrong, you will be exited from the program. If the PIN is correct, then the user can choose from the following ?
Java ATM Program Operations
The following are the operations that you can perform with the program ?
- Withdraw Cash ? The user has to enter the amount to be withdrawn. If the withdrawal amount is less than or equal to the balance, then the transaction will be performed; otherwise, it will display an insufficient funds message.
- Deposit Cash ? The user has to enter the amount to be deposited in the account. A message will be displayed if the cash was deposited successfully.
- CheckBalance ? This will display the current balance of the account.
- ViewLast Transaction ? This will display the last transaction.
- Exit ? This will let the user exit from the program.
ATM Program Using 'Switch Case'
This approach uses a simple command line interface to interact with users and implement ATM functionalities. The program uses basic loops and conditional statements for execution.
Implementation steps
The following are the steps to implement ATM program in java using switch case.
Step 1: Initialize the variables such as balance, last transaction, and PIN.
Step 2: Ask the user to input the PIN and verify it with the actual PIN. If the PIN is correct, proceed with the next step; otherwise, exit the program.
Step 3: Ask the user to choose one from the options ?
- withdraw
- deposit
- Check balance.
- View last transaction.
- exit
Step 4: If the user chooses to withdraw, check if the amount is not less than the balance, subtract the amount from the balance, and display a message if the withdrawal is successful.
Step 5: If the user chooses deposit, add the amount to the balance and display a message showing the amount deposited.
Step 6: If the user asks to display the balance, print the current balance on the screen.
Step 7: If the user selects the last transaction, display the last transaction.
Step 8: If the user chooses to exit, close the program.
Step 9: If the user selects any other option, print 'invalid choice'.
Implementation code
The code for implementing ATM program using switch case in java is given below
import java.util.Scanner; public class ATM { public static void main(String[] args) { int balance = 50000; String lasttransaction = "No transactions made"; int pin = 1234; Scanner sc = new Scanner(System.in); System.out.print("Enter your PIN: "); int enteredpin = sc.nextInt(); if (enteredpin != pin) { System.out.println("Access denied. Incorrect Pin"); return; } System.out.println("Welcome!"); while (true) { System.out.println("1. Withdraw Cash"); System.out.println("2. Deposit Cash"); System.out.println("3. Check Balance"); System.out.println("4. View Last Transaction"); System.out.println("5. Exit"); System.out.print("Enter your choice: "); int choice = sc.nextInt(); switch (choice) { case 1: // withdrawal System.out.print("Enter the amount to withdraw: "); int withdraw = sc.nextInt(); if (withdraw > 0 && withdraw <= balance) { balance -= withdraw; lasttransaction = "You withdrew: Rs." + withdraw; System.out.println("Withdrawal successful! Please take your cash.\n"); } else if (withdraw > balance) { System.out.println("Insufficient funds! Please try a smaller amount.\n"); } else { System.out.println("Invalid amount. Please try again.\n"); } break; case 2: // deposit System.out.print("Enter the amount to deposit: "); int deposit = sc.nextInt(); if (deposit > 0) { balance += deposit; lasttransaction = "You deposited: Rs." + deposit; System.out.println("Deposited successfully! Thank you.\n"); } else { System.out.println("Invalid amount.Please try again.\n"); } break; case 3: // Displaying balance System.out.println("Your current balance is: Rs." + balance+"\n"); break; case 4: // Displaying last transaction System.out.println("Last transaction: " + lasttransaction+"\n"); break; case 5: // Exit the program System.out.println("Thank you for using the ATM. Have a great day!\n"); System.exit(0); break; default: System.out.println("Invalid choice. Please select a valid option.\n"); } } } }
Output
ATM Program using File-Handling
This approach uses the same logic above but also uses file handling for saving and retrieving the data from a file. This ensures that the data remains consistent when executing the program for multiple times. In this program we will use a txt file to store the balance and the last transaction details.
File Handling classes
This code uses the following classes to handle file operations.
- File class ? Used to check if the file exists or not.
- FileReader class ? Used to read the contents of the file.
- BufferedReader class ? Used with the FileReader object to read files more efficiently.
- FileWriter class ? Used to write contents into the file.
- BufferedWriter class ? Used with the FileWriter object to write files more efficiently.
Implementation code
The code for implementing ATM program using file handling in java is given below.
import java.io.*; import java.util.Scanner; public class ATM { public static void main(String[] args) { int balance = 50000; String lasttransaction = "No transactions made"; int pin = 1234; // Reading balance and last transaction from the file try { File file = new File("datafile.txt"); if (file.exists()) { BufferedReader in = new BufferedReader(new FileReader(file)); balance = Integer.parseInt(in.readLine());//reading first line lasttransaction = in.readLine();//reading second line in.close(); } } catch (IOException e) { System.out.println("Error reading file: " + e.getMessage()); } Scanner sc = new Scanner(System.in); System.out.print("Enter your PIN: "); int enteredpin = sc.nextInt(); if (enteredpin !=pin) { System.out.println("Access denied. Incorrect Pin"); return; } System.out.println("Welcome!"); while (true) { System.out.println("1. Withdraw Cash"); System.out.println("2. Deposit Cash"); System.out.println("3. Check Balance"); System.out.println("4. View Last Transaction"); System.out.println("5. Exit"); System.out.print("Enter your choice: "); int choice = sc.nextInt(); switch (choice) { case 1: // withdrawal System.out.print("Enter the amount to withdraw: "); int withdraw = sc.nextInt(); if (withdraw > 0 && withdraw <= balance) { balance -= withdraw; lasttransaction = "You withdrew: Rs." + withdraw; System.out.println("Withdrawal successful! Please take your cash.\n"); } else if (withdraw > balance) { System.out.println("Insufficient funds! Please try a smaller amount.\n"); } else { System.out.println("Invalid amount. Please try again.\n"); } break; case 2: // deposit System.out.print("Enter the amount to deposit: "); int deposit = sc.nextInt(); if (deposit > 0) { balance += deposit; lasttransaction = "You deposited: Rs." + deposit; System.out.println("Deposited successfully! Thank you.\n"); } else { System.out.println("Invalid amount.Please try again.\n"); } break; case 3: // Displaying balance System.out.println("Your current balance is: Rs." + balance+"\n"); break; case 4: // Displaying last transaction System.out.println("Last transaction: " + lasttransaction+"\n"); break; case 5: // Exit the program System.out.println("Thank you for using the ATM. Have a great day!\n"); sc.close(); // Save balance and last transaction to file try { BufferedWriter wr = new BufferedWriter(new FileWriter("datafile.txt")); wr.write(balance + "\n"); wr.write(lasttransaction + "\n"); wr.close(); } catch (IOException e) { System.out.println("Error writing the file: " + e.getMessage()); } System.exit(0); break; default: System.out.println("Invalid choice. Please try again."); } } } }
Output
A new file, 'datafile.txt', is created, which stores the current balance in the first line and the last transaction in the second line. When running the code for the next time, the data in this file is retrieved to continue from the state where it was left.