ATM Machine Program | Java

ATM Machine Program | Java

ATM Machine

This is a Java Program to Display the ATM Machine Transaction.

The user will choose from any one of the available options as input. Different cases using switch case have been provided for different operations like withdraw, deposit and check balance.

This Program has 3 source files named Main.java,  BankAccount.java,  AtmMachine.java.

  • BankAccount class used to create Bank Account for Customers, so that we easily check the debit card number and pin.
  • AtmMachine class used to create ATM Machine Instance to Simulate ATM Transactions.

Here is the source code of the Java Program to Display the ATM Transaction. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

PROGRAM

BankAccount.java
public class BankAccount {
    private String customerName;
    private long customerDebitCardNumber;
    private int pin;
    private String customerAccountNumber;
    private String customerEmailAddress;
    private double balance;

    public BankAccount(String customerName, long customerDebitCardNumber, int pin, String customerAccountNumber,
                       String customerEmailAddress, double balance) {
        this.customerName = customerName;
        this.customerDebitCardNumber = customerDebitCardNumber;
        this.pin = pin;
        this.customerAccountNumber = customerAccountNumber;
        this.customerEmailAddress = customerEmailAddress;
        this.balance = balance;
    }

    public String getCustomerName() {
        return customerName;
    }

    public long getCustomerDebitCardNumber() {
        return customerDebitCardNumber;
    }

    public int getPin(long customerDebitCardNumber) {
        if(this.customerDebitCardNumber == customerDebitCardNumber) {
            return pin;
        }
        return -1;
    }

    public String getCustomerAccountNumber() {
        return customerAccountNumber;
    }

    public String getCustomerEmailAddress() {
        return customerEmailAddress;
    }

    public double getBalance() {
        return balance;
    }
    public void updateBalance(double newBalance) {
        balance = newBalance;
    }
}
AtmMachine.java
public class AtmMachine {
    private long customerDebitCardNumber;
    private int pin;
    private BankAccount bankAccount;

    public AtmMachine(long customerDebitCardNumber, int pin) {
        this.customerDebitCardNumber = customerDebitCardNumber;
        this.pin = pin;
        bankAccount = null;
    }
    public boolean isAccountFound(BankAccount[] bankAccounts) {
        this.bankAccount = findBankAccount(bankAccounts);
        if(bankAccount != null) {
            return true;
        }
        return false;
    }
    private BankAccount findBankAccount(BankAccount[] bankAccounts) {
        for(int i = 0; i < bankAccounts.length; i++) {
            if(bankAccounts[i].getCustomerDebitCardNumber() == customerDebitCardNumber &&
                bankAccounts[i].getPin(customerDebitCardNumber) == pin) {
                return bankAccounts[i];
            }
        }
        return null;
    }
    public String getCustomerName() {
        return this.bankAccount.getCustomerName().toUpperCase();
    }
    public void checkBalance() {
        if(bankAccount != null) {
            System.out.println("Balance Amount : Rs." + bankAccount.getBalance());
        } else {
            System.out.println("Invalid Account");
        }
    }
    public int deposit(int amountToDeposit) {
        if(amountToDeposit > 0 && amountToDeposit % 100 == 0) {
            double balance = bankAccount.getBalance();
            balance += amountToDeposit;
            bankAccount.updateBalance(balance);
            System.out.println("Rs." + amountToDeposit + " has been Successfully Deposited.");
            System.out.println("Your Updated Balance : " + balance);
            return 0;
        } else {
            System.out.println("Deposited Amount Should be Multiples of Rs.100, Rs.200, Rs.500, Rs2000");
            return -1;
        }
    }

    public int withdraw(int amountToWithdraw) {
        if(amountToWithdraw % 100 == 0) {
            if(amountToWithdraw > 0 && amountToWithdraw <= bankAccount.getBalance()){
                double balance = bankAccount.getBalance();
                balance -= amountToWithdraw;
                bankAccount.updateBalance(balance);
                System.out.println("Please Collect Your Cash");
                System.out.println("Rs." + amountToWithdraw + " has been Debited Successfully.");
                return 0;
            } else {
                System.out.println("Insufficient Balance.Only Rs." + bankAccount.getBalance() + " is there.");
                return -1;
            }
        } else {
            System.out.println("Withdrawal Amount should be Multiples of Rs.100, Rs.200, Rs.500, Rs.2000");
            return -1;
        }
    }
}
Main.java
import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        BankAccount[] customers = new BankAccount[3];
        int debitCardPin;
        long debitCardNumber;

        customers[0] = new BankAccount("Hari Prasath", 123456, 9087, "ACC123987",
                "hari@gmail.com", 9500);
        customers[1] = new BankAccount("John Wick", 654321, 1543, "ACC56783",
                "wick@gmail.com", 4300);
        customers[2] = new BankAccount("Bruce Wayne", 959713, 7099, "ACC703987",
                "wayne@gmail.com", 8790);

        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("Enter Debit Card Number : ");
            if(scanner.hasNextLong()) {
                debitCardNumber = scanner.nextLong();
                break;
            }
            scanner.nextLine();
        }
        while(true) {
            System.out.println("Enter Pin : ");
            if(scanner.hasNextInt()) {
                debitCardPin = scanner.nextInt();
                break;
            }
            scanner.nextLine();
        }
        AtmMachine atmMachine = new AtmMachine(debitCardNumber, debitCardPin);
        if(atmMachine.isAccountFound(customers)) {
            while(true) {
                System.out.println("Hello! " + atmMachine.getCustomerName() + "\n");
                System.out.println("1. WithDraw");
                System.out.println("2. Deposit");
                System.out.println("3. Check Balance");
                System.out.println("4. Exit\n");
                System.out.println("Enter Your Choice : ");
                if(scanner.hasNextInt()) {
                    int choice = scanner.nextInt();
                    switch (choice) {
                        case 1 :
                            while(true) {
                                 System.out.println("\nEnter Amount to Withdraw : ");
                                 if(scanner.hasNextInt()) {
                                     int amountToWithDraw = scanner.nextInt();
                                     if(atmMachine.withdraw(amountToWithDraw) == 0) {
                                         atmMachine.checkBalance();
                                         break;
                                     } else {
                                         continue;
                                     }
                                 } else {
                                     System.out.println("Please Enter Valid Integer Value.");
                                 }
                                 scanner.nextLine();
                            }
                            break;
                        case 2 :
                            while(true) {
                                System.out.println("\nEnter Amount to Deposit : ");
                                if(scanner.hasNextInt()) {
                                    int amountToDeposit = scanner.nextInt();
                                    if(atmMachine.deposit(amountToDeposit) == 0) {
                                        atmMachine.checkBalance();
                                        break;
                                    } else {
                                        continue;
                                    }
                                } else {
                                    System.out.println("Please Enter Valid Integer Value.");
                                }
                                scanner.nextLine();
                            }
                            break;
                        case 3 :
                            atmMachine.checkBalance();
                            break;
                        case 4 :
                            System.exit(-1);
                            break;
                        default:
                            System.out.println("Please Enter Valid Option");
                            break;
                    }
                    break;
                } else {
                    System.out.println("Please Enter Valid Integer Number ( 1 or 2 or 3 or 4)");
                }
                scanner.nextLine();
            }
        } else {
            System.out.println("INCORRECT PIN");
        }


    }
}

OUTPUT

While Enter INCORRECT PIN :

atm machine

While Enter CORRECT PIN :

atm machine

 

WITHDRAW:

atm machine

 

DEPOSIT  :

atm machine

 

CHECK BALANCE :

check balance

 

You May Also like…

Simple Minesweeper Game | C++

Square Diagonal Star Pattern | Java

C Program To Play Flames Game | CodeChef | HackerRank

My Zoho Interview Experience | August 2019

Hari Prasath R

You know who I am :)