Coders Packet

ATM Machine using Java

By Praveen Kumar. S

  • atmMachine.java
  • We are here to create a ATM Machine using Java. You should have some idea in Java basics like looping statements, class, objects, etc...

    In this tutorial, we are here to create an ATM Machine using Java basics. I assume that, you know how the ATM Machine works. Initially, it asks user's userid and Password. Then, if the user entered userid and password matches to the userid and password in the database. It asks us to choose the type of account to be logged in.

    Then, it asks for the type of operation to be performed like View Balance, Withdraw Funds, Deposite Funds, Exit.

    View Balance, displays our account Balance in the Machine.
    Withdraw Funds, takes us to the Withdrawing page. There, we enter the required amount that should be withdrawn. Once we had withdrawn our amount, automatically our account balance gets updated.
    Deposite Funds, takes us to the Depositing page. There, we enter the depositing amount. Once we deposit the amount, automatically our account balance gets updated.
    Exit, takes us out of the server.

    Let us discuss in terms of Programs.
    In this Program, Initially we will create four variables. Two variable for two userid's and other Two variable for two Password's.
    Using Scanner class we will get user inputs, userid and password. 

    Using If-Else loop, we will check for correct userid and Password. If the userid and Password are correct, If block will be Executed. If not, The Else block will be Executed.

    Inside the If block, Initially the user should choose the type of account in which he/she have to do some operations. This can be Achieved using Switch case. For Example, case 1 will have the code for Savings account and case 2 will have the code for Current account.He/She can do some Operations like, View Balance, WithDraw Funds, Deposite Funds, Exit. This can be Achieved using If-ElseIf -Else loop.

    I Think, you understood the working principle of our project. Let's see the code, so that you will understand it better.

    import java.util.Scanner;
    public class atmMachine
    {
    public static void main(String[] args)
    {
    int account_no1= 12345678;
    int account_no2= 87654321;
    int password1= 1234;
    int password2= 4321;
    int userid;
    int pwd;
    System.out.println("Enter The Account Number : ");
    Scanner sc = new Scanner(System.in);
    userid= sc.nextInt();
    System.out.println("Enter The PIN Number : ");
    pwd = sc.nextInt();
    if((account_no1 == userid && password1 == pwd) || (account_no2 == userid && password2 == pwd))
    {
    additionalClass ac = new additionalClass();
    ac.accountType();
    }
    else
    {
    System.out.println("Invalid UserName (or) Password");
    }
    }
    }
    class additionalClass
    {
    int account_type;
    int choice;
    int sbalance = 10000;
    int sdeposite;
    int swithdraw;
    int cbalance = 50000;
    int cdeposite;
    int cwithdraw;
    public void accountType()
    {
    System.out.println("Select the type of account you want to use : \n1)Savings\n2)Current\n3)Exit");
    Scanner sc = new Scanner(System.in);
    account_type = sc.nextInt();
    while(account_type !=3)
    {
    switch(account_type)
    {
    case 1:
    System.out.println("You have selected Savings account");
    System.out.println("\n1)View Balance\n2)WithDraw Funds\n3)Deposite Funds\n4)Exit");
    choice = sc.nextInt();
    if(choice == 1)
    {
    System.out.println("Savings Account Balance : "+sbalance);
    }
    else if(choice ==2)
    {
    System.out.println("Enter The amount to be withdrawn : ");
    swithdraw = sc.nextInt();
    if(sbalance>=swithdraw)
    {
    sbalance = sbalance - swithdraw;
    System.out.println("Savings Account Balance : "+sbalance);
    }
    else
    {
    System.out.println("Insufficient Balance ");
    }
    }
    else if(choice ==3)
    {
    System.out.println("Enter the amount to be Deposited : ");
    sdeposite = sc.nextInt();
    sbalance = sbalance + sdeposite;
    System.out.println("Savings Account Balance : "+sbalance);
    }
    else
    {
    break;
    }
    break;
    case 2:
    System.out.println("You have selected Savings account");
    System.out.println("\n1)View Balance\n2)WithDraw Funds\n3)Deposite Funds\n4)Exit");
    choice = sc.nextInt();
    if(choice == 1)
    {
    System.out.println("Savings Account Balance : "+cbalance);
    }
    else if(choice ==2)
    {
    System.out.println("Enter The amount to be withdrawn : ");
    cwithdraw = sc.nextInt();
    if(cbalance>=cwithdraw)
    {
    cbalance = cbalance - cwithdraw;
    System.out.println("Savings Account Balance : "+cbalance);
    }
    else
    {
    System.out.println("Insufficient Balance ");
    }
    }
    else if(choice ==3)
    {
    System.out.println("Enter the amount to be Deposited : ");
    cdeposite = sc.nextInt();
    cbalance = cbalance + cdeposite;
    System.out.println("Savings Account Balance : "+cbalance);
    }
    else
    {
    break;
    }
    break;
    }
    System.out.println("Select the type of account you want to use : ");
    account_type = sc.nextInt();
    }
    }
    }


    Output,

    Enter the Account number: 12345678
    Enter the PIN Number:1234
    Select the type of account you want to use :
    1)Savings
    2)Current
    3)Exit
    You have selected Savings Account
    1)View Balance
    2)WithDraw Funds
    3)Deposit Funds
    4)Exit
    3
    Enter the amount to be deposited: 10000
    Savings Account Balance: 20000
    Select the type of account you want to use : 
    1)Savings
    2)Current
    3)Exit
    You have selected a savings account
    1)View balance
    2)WithDraw Funds
    3)Deposit Funds
    4)Exit
    2
    Enter The Amount to be Withdrawn: 5000
    Savings account Balance: 15000
    Select the type of account you want to use : 
    1)savings
    2)Current
    3)Exit
    3

     

    Kindly, go through line by line. Definitely, you will understand the Program.
    If you can't understand it, you should go through some basic concepts of java.

    Download Complete Code

    Comments

    Comments for the packet "ATM Machine using Java";
    No comments yet