// BankAccount.java — classes, objects, constructors, encapsulation, static members.
public class BankAccount {

    private static int accountsOpened = 0;   // shared by all accounts

    private final String owner;              // set once, never changes
    private double balance;                  // hidden: change it only through methods

    public BankAccount(String owner, double openingBalance) {
        if (openingBalance < 0) {
            throw new IllegalArgumentException("Opening balance cannot be negative");
        }
        this.owner = owner;
        this.balance = openingBalance;
        accountsOpened++;
    }

    public void deposit(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("Deposit must be positive");
        }
        balance += amount;
    }

    public boolean withdraw(double amount) {
        if (amount > balance) {
            return false;   // not enough money
        }
        balance -= amount;
        return true;
    }

    public double getBalance() { return balance; }
    public String getOwner() { return owner; }
    public static int getAccountsOpened() { return accountsOpened; }

    @Override
    public String toString() {
        return owner + ": " + String.format("%.2f", balance);
    }

    public static void main(String[] args) {
        BankAccount alice = new BankAccount("Alice", 100);
        BankAccount bob = new BankAccount("Bob", 50);

        alice.deposit(25);
        boolean ok = bob.withdraw(80);

        System.out.println(alice);                          // Alice: 125.00
        System.out.println(bob + " (withdraw ok? " + ok + ")");
        System.out.println("Accounts: " + BankAccount.getAccountsOpened());
    }
}
