Java ATM project

Some points to remember before you start the program:
1. Enter all the numerical values without commas.
2. Use 1234567890123456 when asked for card number.
3. Default PIN for your card is 1234 which can be changed later.

Click here to download the java source code file.
Following is the java source code of the program that mimics some of the functionality of a ATM:

import java.util.Scanner; // for taking user input

/*
 Note: all the methods of class HomePage are static which means all them can be
       accessed from anywhere without object by just using name of the class
*/
class HomePage
{
static int choice = 0;
private static Scanner sc=new Scanner(System.in);

static void showWelcomeNote() // displys the welcome note at the start
{
System.out.println("\n\n\t\t\tWelcome to AMN BANK ATM ");
System.out.println("\t\t________________________________________\n\n");
}

static void showMenu() // displays the list of operations that can be performed
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("Main Menu:");
System.out.println("1. Deposit in Account");
System.out.println("2. Withdraw from Account");
System.out.println("3. Balance Enquiry");
System.out.println("4. Change PIN");
System.out.println("5. Exit");
}

static int getChoice() // filters the inputs for choices and returns the choosen option
{
String s;
System.out.print("\nEnter your choice: ");
try
{
s=sc.nextLine();
choice = Integer.parseInt(s);
}
catch(Exception e)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("ERROR: invalid choice !!!\n");
System.out.println("...Press the Enter key...");
sc.nextLine();
HomePage.showMenu();
HomePage.getChoice();
}
finally
{
if(choice < 1 || choice > 5)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("ERROR: entered choice out of range !!!\n");
System.out.println("...Press the Enter key.....");
sc.nextLine();
HomePage.showMenu();
HomePage.getChoice();
}
}
return choice;
}

static void showExitAndThankYouNote() // executes when program terminates after successful transaction(s)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("\n\nTHANKS FOR USING OUR SERVICE....\n\t...Have a good day !!");
java.lang.System.exit(0);
}

static void showExitThreeTimesIncorrectPIN() // executes when program terminates due to incorrect PIN
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("\n\n You have entered the incorrect PIN three times !!!\n");
System.out.println("ERROR: further transaction is blocked !!!");
java.lang.System.exit(0);
}

}// End of class HomePage

// class Verifier has all the methods to verify the credentials of the user
class Verifier
{
static String cardNo;
static String pin="1234";
private static Scanner sc = new Scanner(System.in);

static void verifyCardNo()
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("NOTE: Card insertion facility is not available in this ATM !!!\n");
System.out.print("Enter your 16-digit card no: ");
cardNo = sc.nextLine();
if(!(cardNo.equals("1234567890123456")))
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("ERROR: invalid card number !!!\n");
System.out.println("...Press the Enter key to enter the card number again...");
System.out.println("... and ...");
System.out.println("...Press CTRL-C to exit...");
sc.nextLine();
Verifier.verifyCardNo();
}
}

static boolean verifyPIN() // returns true if PIN is verified else false
{
System.out.print("\nPlease enter your PIN to continue : ");
String enteredPIN=sc.nextLine();
if(enteredPIN.equals(pin))
return true;
System.out.println("______________________________________________________________________\n\n");
System.out.println("\n\nWARNING: incorrect PIN !!!");
System.out.println("...Press the ENTER key.....");
sc.nextLine();
return false;
}

static void changePIN(String changedPIN) // pin can be changed directly from here as it is static
{
pin = changedPIN;
}

}// End of class Verifier

// class Transaction has all the methods used for transaction
class Transaction
{
static double balance = 200000d;
private static Scanner sc = new Scanner(System.in);

boolean deposit() // returns true if deposition is successful else false
{
int amount=0;
boolean loop = true;
String enteredAmount;
int rs2000=0,rs500=0,rs200=0,rs100=0,calculatedAmount;
String rs2000s,rs500s,rs200s,rs100s;

if(!Verifier.verifyPIN())
return false;
while(loop) // control exits this loop only when a valid amount is entered
{
System.out.print("\nEnter the amount(without commas) to be deposited: ");
try
{
loop = false;
enteredAmount=sc.nextLine();
amount = Integer.parseInt(enteredAmount);
if(amount % 100 != 0)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("ERROR: invalid amount !!!\n");
System.out.println("Amount accepted only in the multiples of Rs. 100, 200, 500 and 2,000\n");
System.out.println("...Press the Enter key...");
sc.nextLine();
loop = true;
}
else
{
System.out.println("Enter the denominations:");
System.out.print("Rs.2000 X : ");
rs2000s = sc.nextLine();
rs2000 = Integer.parseInt(rs2000s);
System.out.print(" Rs.500 X : ");
rs500s = sc.nextLine();
rs500 = Integer.parseInt(rs500s);
System.out.print(" Rs.200 X : ");
rs200s = sc.nextLine();
rs200 = Integer.parseInt(rs200s);
System.out.print(" Rs.100 X : ");
rs100s = sc.nextLine();
rs100 = Integer.parseInt(rs100s);
calculatedAmount = (rs2000 * 2000) + (rs500 * 500) + (rs200 * 200) + (rs100 * 100);
if(calculatedAmount != amount)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("ERROR: amount mismatch !!!\n");
System.out.println("...entered amount and denominated amount does not matches !!!\n");
System.out.println("...Press the Enter key...");
sc.nextLine();
loop = true;
}
}
}
catch(Exception e)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("ERROR: invalid amount encountered !!!\n");
System.out.println("...Press the Enter key...");
sc.nextLine();
loop = true;
}
}

System.out.println("______________________________________________________________________\n\n");
System.out.println("\nRs. "  + amount + " deposited successfully !!!");
                System.out.println(" ( It is assumed here that the valid currency notes are inserted by the user in proper way )\n");
System.out.println("-> Previous balance: " + balance);
balance += amount;
System.out.println("-> Updated balance: " + balance);
System.out.println("\n...Press the ENTER key...");

sc.nextLine();
return true;
}

boolean withdraw() // returns true if withdrawal is successfull else false
{
int amount = 0;
boolean loop = true;
String enteredAmount;
String confirm_print;
if(!Verifier.verifyPIN())
return false;
while(loop) // control exits this loop only when a valid amount is entered
{
System.out.print("\nEnter the amount to withdraw: ");
try
{
enteredAmount = sc.nextLine();
amount = Integer.parseInt(enteredAmount);
loop = false;
}
catch(Exception e)
{
System.out.println("______________________________________________________________________\n\n");
System.out.println("\nERROR: invalid amount !!!");
System.out.println("...Press the ENTER key...");
sc.nextLine();
}
}
                if(amount % 100 != 0)
{
                System.out.println("______________________________________________________________________\n\n");
                System.out.println("ERROR: invalid amount !!!");
System.out.println("....Amount must be in multiples of Rs. 100, 200, 500, or 2,000\n");
System.out.println("...Press the ENTER key...");
sc.nextLine();
return true;
}
else if(balance < amount-500)
{
    System.out.println("______________________________________________________________________\n\n");
    System.out.println("ERROR: insufficient balance !!!");
    System.out.println("....your account balance is low, Rs.500 is minimum balance that you cannot withdraw\n");
System.out.println("...Press the ENTER key...");
sc.nextLine();
return true;
}
else if(amount > 15000)
{
System.out.println("______________________________________________________________________\n\n");
    System.out.println("WARNING: withdrawal limit exceeded !!!\n");
System.out.println("...maximum withdrawable amount in a single transaction is Rs. 15000 for your card\n");
System.out.println("...Press the ENTER key...");
sc.nextLine();
return true;
}
while(true)
        {
System.out.print("Do you want to print the receipt ? (yes or no): ");
confirm_print = sc.nextLine();
if(confirm_print.equalsIgnoreCase("yes") || confirm_print.equalsIgnoreCase("no")
   ||confirm_print.equalsIgnoreCase("y") || confirm_print.equalsIgnoreCase("n"))
break;
    else
{
    System.out.println("______________________________________________________________________\n\n");
     System.out.println("ERROR: invalid input !!!\n");
System.out.println("...Press the ENTER key...");
sc.nextLine();
}
        }

if(confirm_print.equalsIgnoreCase("yes") || confirm_print.equalsIgnoreCase("y"))
Transaction.printReceipt(amount);
else
balance -= amount;
System.out.println("______________________________________________________________________\n\n");
System.out.println("Amount withdrawn successfully !!!\n");
System.out.println("...Press the ENTER key.....");
sc.nextLine();
return true;
}

boolean getBalance()
{
if(!Verifier.verifyPIN())
return false;
System.out.println("______________________________________________________________________\n\n");
System.out.println("\n\nYour current balance is: Rs." + balance + " /-\n");
System.out.println("...Press the ENTER key...");
sc.nextLine();
return true;
}

boolean changePIN()
{
int pin = 0;
String newPIN="1234";
boolean loop = true;

if(!Verifier.verifyPIN())
return false;

while(loop)
{
System.out.println("______________________________________________________________________\n\n");
System.out.print("Enter the new PIN: ");
try
{
newPIN=sc.nextLine();
System.out.println("______________________________________________________________________\n\n");
pin = Integer.parseInt(newPIN);
if(pin < 1000 || pin > 9999)
System.out.println("PIN is strictly an integer of exactly four digits, where first digit is non-zero.\n");
else
loop = false;
}
catch(Exception e)
{
System.out.println("PIN cannot have any alphabet or special character.!!\n");
}
finally
{
if(loop)
{
System.out.println("...Press the Enter key...");
sc.nextLine();
}
}
}
Verifier.changePIN(newPIN);
System.out.println("PIN changed successfully !!!\n");
System.out.println("...Press the ENTER key.....");
sc.nextLine();
return true;
}

static void printReceipt(int amount)
{
int amountToDispense = amount;
int rs2000=0,rs500=0,rs200=0,rs100=0;
while(amountToDispense != 0)
{
if(amountToDispense >= 2000)
{
rs2000++;
amountToDispense -= 2000;
}
else if(amountToDispense >= 500)
{
rs500++;
amountToDispense -= 500;
}
else if(amountToDispense >= 200)
{
rs200++;
amountToDispense -= 200;
}
else
{
rs100++;
amountToDispense -= 100;
}
}
System.out.println("_______________________________________________");
System.out.println("\n\tAMN BANK ATM SERVICE\n");
System.out.println("Account No: 74561010XXXXXX");
System.out.println("   Card No: 85XX-XXXX-XXXX-3456");
System.out.println(" ATM ID No: 1809010015");
System.out.println("      Date: dd-mm-yyyy");
System.out.println("      Time: hh:mm:ss");
System.out.println("\nCurrency Notes:");
if(rs2000 != 0)
System.out.println("->Rs. 2000 X " + rs2000 + " = " + 2000 * rs2000);
if(rs500 != 0)
System.out.println("->Rs. 500 X " + rs500 + " = " + 500 * rs500);
if(rs200 != 0)
System.out.println("->Rs. 200 X " + rs200 + " = " + 200 * rs200);
if(rs100 != 0)
System.out.println("->Rs. 100 X " + rs100 + " = " + 100 * rs100);
System.out.println("=>Total Amount: " + amount);
System.out.println("=>Previous balance: " + balance);
balance -= amount;
System.out.println("=>Updated balance: " + balance);
System.out.println("_______________________________________________");
}

}// End of class Transaction

// class ATM is public and contains main() method
public class ATM
{
public static void main(String[] args)
{
int checkIncorrectPIN = 0;
Transaction t = new Transaction();
HomePage.showWelcomeNote();
Verifier.verifyCardNo();

// do unless incorrect pin is entered three times
do{
HomePage.showMenu();
switch(HomePage.getChoice())
{
case 1:
checkIncorrectPIN = (t.deposit()) ? 0 : checkIncorrectPIN+1 ;// returns true if method executed successfully else false
break;

case 2:
checkIncorrectPIN = (t.withdraw()) ? 0 : checkIncorrectPIN+1 ;// returns true if method executed successfully else false
break;

case 3:
checkIncorrectPIN = (t.getBalance()) ? 0 : checkIncorrectPIN+1 ;// returns true if method executed successfully else false
break;

case 4:
checkIncorrectPIN = (t.changePIN()) ? 0 : checkIncorrectPIN+1 ;// returns true if method executed successfully else false
break;

case 5:
HomePage.showExitAndThankYouNote();
}
}while(checkIncorrectPIN<3);

// checkIncorrectPIN = 3 means incorrect pin has been entered three times
HomePage.showExitThreeTimesIncorrectPIN();
}
}// End of public class ATM

Comments

Popular posts from this blog

Working of a Computer Program