Home  >  Article  >  Java  >  ATM Program in Java

ATM Program in Java

WBOY
WBOYOriginal
2024-08-30 15:14:38702browse

We can construct an ATM program in Java to display ATM transactions. An automated teller machine (ATM) or cash machine (In British English) is an electronic telecommunications system that allows customers of banking firms to conduct financial transactions. The user must choose a choice from the options shown on the screen in the ATM application. For example, withdraw money, deposit money, check your balance, and exit the available options.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Working of ATM Program in Java

To withdraw your funds, deposit your funds and check your account balance before exiting, the following operations are needed to perform in the ATM program:

  • Withdraw: For withdrawing the funds, gets the withdrawal amount from the user, deduct it from the total balance, and display the message.
  • Deposit: For depositing the funds, gets the deposit amount from the user to add, add it to the total balance, and display the message.
  • Check the balance: For checking the balance, display the user’s total balance.
  • Exit: Return the user to the home page or initial screen by exiting the current Transaction mode.

Example of ATM Program in Java

Given below is the example of the ATM Program in Java:

Example for ATM program in Java to withdraw amount, deposit amount and check the balance.

Code:

package jex;
import java.util.*;
class ATM {
public static void main( String args[] ) {
//declare and initialize balance, withdraw, and deposit
int balance = 50000;
int withdraw, deposit;
//create scanner class object to get choice of user
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println( "Welcome to ATM ... " );
System.out.println( "Select 1 for Withdraw" );
System.out.println( "Select 2 for Deposit" );
System.out.println( "Select 3 for Check Balance" );
System.out.println( "Select 4 for EXIT" );
System.out.print( "Select the appropriate options you want to perform:" );
//get the user selected option
int op = sc.nextInt( );
switch( op )
{
case 1: System.out.print( "Enter the amount to be withdrawn :" );
// accept the withdraw amount from the user
withdraw = sc.nextInt();
//check whether the balance is greater than or equal to the withdrawal amount
withdraw( balance, withdraw);
break;
case 2: System.out.print( "Enter the amount to be deposited :" );
//accept the deposit amount from the user
deposit = sc.nextInt();
// call the function and add the deposit amount to the total balance
deposit( balance, deposit );
break;
case 3:
// printing the total balance of the user
printBalance( balance );
System.out.println(" ");
break;
case 4:
// exit from the menu
System.exit( 0 );
}
}
}
// function to print the current balance in an account
public static void printBalance(int balance)
{
System.out.println(" The Current Balance : " + balance);
System.out.println();
}
// The function to Withdraw an amount and update the balance
public static int withdraw(int balance, int withdrawAmount)
{
System.out.println( "Withdrawn Operation :" );
System.out.println("The withdrawing Amount is : " + withdrawAmount);
if (balance >= withdrawAmount) {
balance = balance - withdrawAmount;
System.out.println( "Please collect your money and remove the card" );
printBalance( balance );
}
else {
System.out.println( "Sorry! the balanace is insufficient." );
System.out.println( );
}
return balance;
}
// The function to deposit an amount and update the balance
public static int deposit(int balance, int depositAmount)
{
System.out.println( "Deposit Operation :" );
System.out.println(" The depositing amount is : " + depositAmount);
balance = balance + depositAmount;
System.out.println( "Your Money has been successfully deposited" );
printBalance(balance);
return balance;
}
}

Output:

An output of the above code for the withdrawal operation is:

ATM Program in Java

An output of the above code for the deposit operation is:

ATM Program in Java

Finally, an output of the above code for the deposit operation is:

ATM Program in Java

As in the above program, the ATM class is created which contains withdraw(), deposit() and printbalance() functions. The withdraw() function is used to perform the withdraw operation; this function accepts the balance and the withdrawn amount. Inside the withdraw() function, first check whether the balance is greater than the withdraw amount or not; when it is true, then update the balance by subtracting the withdraw amount from the balance. Next, the function deposit() is used to performs the deposit operation; this function accepts the balance and the deposit amount.

Inside the deposit() function, it updates the balance by adding the deposit amount to the balance. Next, the printbalance() function is used to print the balance; it accepts the balance. Then, in the main function, a balance variable of an integer is created. Next, printing the selecting pitons for withdrawing, deposit, balance, and exit operations, depending on the specific option selection the case gets to execute, as we can see in the above output.

Conclusion

An automated teller machine (ATM) is an electronic telecommunications system that allows customers of banking firms to conduct financial transactions. We can create an ATM program in Java to display ATM transactions, and the user can withdraw money, deposit money, check the balance, and exit from the ATM.

The above is the detailed content of ATM Program in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Linear search in JavaNext article:Linear search in Java