'Need to show "Insufficient Funds" for negative amounts

I need to throw an exception for "insufficient Funds" when a user withdrawals more than the amount in initialAccountBalance (which equals 500.00). However, I am unsure where to put the exception.

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    double initialAccountBalance = 500.00;

    System.out.print("Enter a transaction type (Balance, Deposit, or Withdrawal): ");
    String transactionType = in.nextLine();

    if (transactionType.equalsIgnoreCase("Balance")){
        System.out.println("Balance " +initialAccountBalance);
        System.out.println();

    } else if (transactionType.equalsIgnoreCase("Deposit")){
        System.out.println("Enter deposit: ");
        int deposit = in.nextInt();
        double balance = initialAccountBalance + deposit;
        System.out.printf("Account Balance: %8.2f", balance);

    } else if(transactionType.equalsIgnoreCase("Withdrawal")){
        System.out.println("Enter withdrawal: ");
        int withdrawal = in.nextInt();
        double balance = initialAccountBalance - withdrawal;
        System.out.printf("Account Balance: %8.2f", balance);

    } else {
        System.out.println("Invalid transaction type");
    }
}


Solution 1:[1]

You could place a if right after the user enters the amount to withdrawal like this:

//...
System.out.println("Enter withdrawal: ");
int withdrawal = in.nextInt();
if (withdrawal > initialAccountBalance)
    throw new RuntimeException("Insufficient Funds");
double balance = initialAccountBalance - withdrawal;
System.out.printf("Account Balance: %8.2f", balance);
//...

If you want to throw your own exception you have to create a new class that extends Exception like @chenchuk mentioned in the comments.

public class InsufficientFundsException extends Exception{
    //if you need to you can Overwrite any method of Exception here
}

So you can throw the InsufficientFundsException in your main method:

//...
System.out.println("Enter withdrawal: ");
int withdrawal = in.nextInt();
if (withdrawal > initialAccountBalance)
    throw new InsufficientFundsException();
double balance = initialAccountBalance - withdrawal;
System.out.printf("Account Balance: %8.2f", balance);
//...

but you would have to provide some way of handling the exception by either using a try-catch block or adding a throws declaration to the method head. But you should look that up, it's to much to explain in depth here...

Hope this helps you a bit (:

Solution 2:[2]

...
} else if (transactionType.equalsIgnoreCase("Withdrawal")){
    System.out.println("Enter withdrawal: ");
    int withdrawal = in.nextInt();

    if (withdrawal > initialAccountBalance) {
        throw new InsufficientFundsException("Insufficient Funds");
    }

    double balance = initialAccountBalance - withdrawal;
    System.out.printf("Account Balance: %8.2f", balance);

}
...

And your InsufficientFundsException should be like this:

public class InsufficientFundsException extends Exception {

    private static final long serialVersionUID = -1L;

    public InsufficientFundsException() {
    }

    public InsufficientFundsException(String message) {
        super(message);
    }
}

Solution 3:[3]

 public void withdrawal() {  
        long amt;  
        System.out.println("Enter the amount you want to withdraw: ");  
        amt = sc.nextLong();  
        if (balance >= amt) {  
            balance = balance - amt;  
            System.out.println("Balance after withdrawal: " + balance);  
        } else {  
            System.out.println("Your balance is less than " + amt + "\tTransaction failed...!!" );  
        }  
    }  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 António Ribeiro
Solution 3 champion-runner