#### Extending our interest calculator No. 105

##### Q:

Our current Account class does not handle negative balances accordingly. Typically banks will charge a different interest rate whenever an account is in debt i.e. having a negative balance. In this case a second so called default interest rate (being significantly higher) will be applied.

Extend the current project by adding a new instance variable debtInterestRate along with getter and setter methods. Then change the implementation of applyInterest() and applyInterest(int years) by using the correct interest value according to the account's balance being positive or negative.

### Caution

A:

We introduce a new variable debtInterestRate to cover negative balance values:

private static double
assetInterestRate = 1.5,   // applied to positive balances / assets
debtInterestRate = 15.;    // applied to negative balances / debts

We need the appropriate getter and setter methods in Account:

/**
* @return The debt interest rate value.
*/
public static double getDebtInterestRate() {
return debtInterestRate;
}

/**
* This interest rate will be applied to negative balances. In contrast
* {{@link #setInterestRate(double)} will handle positive balance values.
*
* @param defaultInterestRate
*                         the desired default interest rate value.
*/
public static void setDebtInterestRate(double debtInterestRate) {
Account.debtInterestRate = debtInterestRate;
}

The computed interest depends on positive or negative balance values:

public void applyInterest(int years) {
final double annualInterestFactor;
if (0 < balance) {                                      // Asset type balance
annualInterestFactor = 1 + assetInterestRate / 100;
} else {                                                // Debt type balance
annualInterestFactor = 1 + debtInterestRate / 100;
}
for (int i = 0; i < years; i++) {
balance *= annualInterestFactor;
}
}

Using Math.pow() for calculating e.g. $( 1 + interest 100 ) years$ rather than using a loop is actually a bad idea: Math.pow() internally using a power series expansion is expensive with respect to execution performance. Due to the integer exponential value a simple loop involving only multiplications offers a better choice.