// Exercise 1: try/catch/finally, multi-catch, and a custom checked exception
//
// Implement:
//   - InsufficientFundsException: a CHECKED exception (extends Exception)
//     with a constructor(String message)
//   - withdraw(balance, amount): returns balance - amount if amount <= balance
//     and amount > 0; otherwise throws InsufficientFundsException (if
//     amount > balance) or IllegalArgumentException (if amount <= 0)
//   - safeDivide(a, b): returns a / b as a double; if b == 0, catch the
//     resulting ArithmeticException and return Double.NaN instead of
//     letting it propagate

public class java_exception_handling_exercise_1 {

    static class InsufficientFundsException extends Exception {
        InsufficientFundsException(String message) {
            // TODO: call super(message)
            super(message);
        }
    }

    static double withdraw(double balance, double amount) throws InsufficientFundsException {
        // TODO: implement validation and return balance - amount
        return 0;
    }

    static double safeDivide(int a, int b) {
        // TODO: implement using try/catch(ArithmeticException)
        return 0;
    }

    // --- self-check: do not modify below this line ---
    public static void main(String[] args) {
        boolean ok = true;

        try {
            double result = withdraw(100.0, 40.0);
            ok &= result == 60.0;
        } catch (InsufficientFundsException e) {
            ok = false;
        }

        boolean threwInsufficient = false;
        try {
            withdraw(100.0, 200.0);
        } catch (InsufficientFundsException e) {
            threwInsufficient = true;
        }
        ok &= threwInsufficient;

        boolean threwIllegalArg = false;
        try {
            withdraw(100.0, -5.0);
        } catch (IllegalArgumentException e) {
            threwIllegalArg = true;
        } catch (InsufficientFundsException e) {
            ok = false; // wrong exception type
        }
        ok &= threwIllegalArg;

        ok &= safeDivide(10, 2) == 5.0;
        ok &= Double.isNaN(safeDivide(10, 0));

        System.out.println(ok ? "PASS" : "FAIL");
    }
}
