-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavingsAccount.java
More file actions
32 lines (27 loc) · 1.04 KB
/
Copy pathSavingsAccount.java
File metadata and controls
32 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package Phase3_ObjectOrientation.BankingApp;
/**
* SavingsAccount - "is-a" Account with an interest rate.
* <p>
*
* Demonstrates:
* - INHERITANCE - extends Account, reuses its encapsulated balance API.
* - POLYMORPHISM - overrides calculateInterest() so applyInterest() (a
* template method on Account) does the right thing per
* subclass at runtime.
*/
public class SavingsAccount extends Account {
private final double annualRate; // e.g. 0.04 for 4% per year
public SavingsAccount(String holderName, double openingBalance, double annualRate) {
super(holderName, openingBalance);
if (annualRate < 0 || annualRate > 0.50) {
throw new IllegalArgumentException("rate out of sensible range");
}
this.annualRate = annualRate;
}
public double annualRate() { return annualRate; }
/** Simple monthly interest: balance * rate / 12. */
@Override
public double calculateInterest() {
return balance() * annualRate / 12.0;
}
}