// Exercise 2: encapsulation with invariants
//
// Implement the BankAccount class below so that:
//   - balance starts at the constructor's initial value (must be >= 0,
//     otherwise throw IllegalArgumentException)
//   - deposit(amount): amount must be > 0 (else IllegalArgumentException),
//     increases balance
//   - withdraw(amount): amount must be > 0 and <= current balance
//     (else IllegalArgumentException), decreases balance
//   - getBalance(): returns current balance
//   - the `balance` field itself must be private (already declared that way)

public class java_oop_fundamentals_exercise_2 {

    static class BankAccount {
        private double balance;

        BankAccount(double initialBalance) {
            // TODO: validate initialBalance >= 0, else throw IllegalArgumentException
            // TODO: assign to balance
        }

        void deposit(double amount) {
            // TODO: implement with validation
        }

        void withdraw(double amount) {
            // TODO: implement with validation
        }

        double getBalance() {
            return balance;
        }
    }

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

        BankAccount acc = new BankAccount(100.0);
        ok &= acc.getBalance() == 100.0;

        acc.deposit(50.0);
        ok &= acc.getBalance() == 150.0;

        acc.withdraw(30.0);
        ok &= acc.getBalance() == 120.0;

        boolean threwOnBadWithdraw = false;
        try {
            acc.withdraw(1000.0);
        } catch (IllegalArgumentException e) {
            threwOnBadWithdraw = true;
        }
        ok &= threwOnBadWithdraw;

        boolean threwOnNegativeInit = false;
        try {
            new BankAccount(-5.0);
        } catch (IllegalArgumentException e) {
            threwOnNegativeInit = true;
        }
        ok &= threwOnNegativeInit;

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