/**
 * Una cuenta de ahorros: es una BankAccount que además paga un interés
 * mensual y permite a lo más tres retiros por mes.
 */
public class SavingsAccount extends BankAccount {
    public static final int MAX_WITHDRAWALS = 3;

    private final int ratePercent;      // interés mensual, en %
    private int withdrawals;            // retiros hechos este mes

    public SavingsAccount(String id, int ratePercent) {
        super(id);                      // primero se construye la parte BankAccount
        if (ratePercent < 0) {
            throw new IllegalArgumentException("tasa negativa: " + ratePercent);
        }
        this.ratePercent = ratePercent;
        this.withdrawals = 0;
    }

    /** Agrega el interés del mes y reinicia el contador de retiros. */
    public void applyInterest() {
        long interest = balance() * ratePercent / 100;
        if (interest > 0) {
            deposit(interest);
        }
        withdrawals = 0;
    }

    /** Igual que en BankAccount, pero solo MAX_WITHDRAWALS veces por mes. */
    @Override
    public boolean withdraw(long cents) {
        if (withdrawals == MAX_WITHDRAWALS) {
            return false;
        }
        boolean ok = super.withdraw(cents);
        if (ok) {
            withdrawals++;
        }
        return ok;
    }

    public int ratePercent() {
        return ratePercent;
    }

    @Override
    public String toString() {
        return super.toString() + " (ahorros, " + ratePercent + " % mensual)";
    }
}
