Answer to Question #249479 in Java | JSP | JSF for Locos

Question #249479

Write a menu driven program in Java to automate the ATM operations by

demonstrating the concepts of interfaces. And create custom exceptions to

deal with the following situations

a. Invalid PIN number is entered more than 3 times

b. Withdraw operation when balance amount < withdraw amount


1
Expert's answer
2021-10-12T00:36:25-0400
public class InvalidPINException extends RuntimeException{
    public InvalidPINException(){
        super("The number of attempts has been exceeded.");
    }
}


public class WithdrawOperationException extends RuntimeException{
    public WithdrawOperationException(){
        super("The amount is greater than balance. ");
    }
}


public interface Operation {
    boolean withdraw(double amount);

    boolean deposit(double amount);

    boolean enterPIN(int pin);

    double getBalance();
}


public class ATM implements Operation {

    private static final int PIN = 0000;
    private double balance;
    private int pinAttempts;
    private boolean signIn;

    public ATM(double balance) {
        this.balance = balance;
        pinAttempts = 3;
        signIn = false;
    }

    @Override
    public boolean withdraw(double amount) {
        if (amount > 0 && signIn) {
            if (amount > balance) {
                throw new WithdrawOperationException();
            }
            balance -= amount;
            return true;
        }
        return false;
    }

    @Override
    public boolean deposit(double amount) {
        if (amount > 0 & signIn) {
            balance += amount;
            return true;
        }
        return false;
    }

    @Override
    public boolean enterPIN(int pin) {
        if (pinAttempts == 0) {
            throw new InvalidPINException();
        }
        if (pin == PIN) {
            signIn = true;
            return true;
        } else {
            pinAttempts--;
            return false;
        }
    }

    @Override
    public double getBalance() {
        if (signIn) {
            return balance;
        } else {
            return -1;
        }
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ATM atm = new ATM(100);
        String input;
        while (true) {
            System.out.println("1. Enter PIN\n" + "2. Withdraw\n" +
                    "3. Deposit\n" + "4. Balance\n" + "0. Exit");
            input = in.nextLine();
            switch (input) {
                case "1":
                    System.out.print("Enter 4 digit PIN: ");
                    input = in.nextLine();
                    if (atm.enterPIN(Integer.parseInt(input))) {
                        System.out.println("PIN is valid.");
                    } else {
                        System.out.println("PIN is invalid.");
                    }
                    break;
                case "2":
                    System.out.print("Enter the amount to withdraw: ");
                    input = in.nextLine();
                    if (atm.withdraw(Double.parseDouble(input))) {
                        System.out.println("Success.");
                    }else{
                        System.out.println("Fail.");
                    }
                    break;
                case "3":
                    System.out.print("Enter the amount to deposit: ");
                    input = in.nextLine();
                    if (atm.deposit(Double.parseDouble(input))) {
                        System.out.println("Success.");
                    }else{
                        System.out.println("Fail.");
                    }
                    break;
                case "4":
                    System.out.println("Balance: " + atm.getBalance());
                    break;
                case "5":
                    System.exit(0);
                    break;
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS