In this tutorial you will learn about the ATM program In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program of ATM program In Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Here we Build a Java Program, to show how ATM Transaction takes place,here a User has to choose options which are displayed on the Screen.
Following operations available in the ATM.
- Withdraw
- Deposit
- Check Balance
- Exit.
Create a ATM program In Java programming.
In this program , we will create a ATM Mechanism In Java, we can create an ATM program for representing ATM transaction we will use options related to withdraw money, deposit, check balance, and exit from transaction.Let look at the code of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
//java program to create a ATM Program using swicth case.. import java.util.Scanner; public class ATM { //declare variables and take a value from user at run time public static void main(String args[] ) { int bal = 100000, with_draw, deposit; Scanner sc = new Scanner(System.in); while(true) { System.out.println("ATM"); 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 any operation you want to perform:"); //take a choice from user int ch = sc.nextInt(); switch(ch) { case 1: System.out.print("Enter money to be withdrawn from a/c:"); //get the withdrawl amount from user.. with_draw = sc.nextInt(); //check conditon for balance is greater than or equal to the withdrawal amount . if(bal >= with_draw) { //Deduct the withdrawl amount from total balance bal = bal - with_draw; System.out.println("Please collect your money"); } else { //show erroe if amount is greater than balanace 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 amount to the total balanace . bal = bal + deposit; System.out.println("Your Money has been successfully depsited"); System.out.println(""); break; case 3: //displaying balance of the user.. System.out.println("Balance : "+bal); System.out.println(""); break; case 4: //exit from the menu System.exit(0); } } } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- bal = it will hold balance amount.
- with_draw= it wil hold value of prime number.
- deposit= it will hold deposit.
After declaring variables we take a nth value of from user.
Now user will choose any one available options and input the number with.here we use different cases using switch case that have been provided to user like withdraw, deposit and check balance.As shown in the image suppose user choose the option 3 then the output of program is show balance.
and again ask user to input different value for perform different operations.
following are the switch case condition according to option for different case are as follow
case 1: for withdraw.
case 2: for Deposit
Case 3: for Check Balance.
Case 4 : for exit
Approach to Check Balance
show the balance amount in the screen.