-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavingsAccount.java
More file actions
37 lines (30 loc) · 1.19 KB
/
Copy pathSavingsAccount.java
File metadata and controls
37 lines (30 loc) · 1.19 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
33
34
35
36
37
package model.entity;
import exceptions.InvalidIFSCException;
import exceptions.MinimumBalanceException;
import model.enums.AccountType;
public class SavingsAccount extends Account {
// 1. Fields (private, final)
private final double interestRate;
// 2. Constructor with validation
public SavingsAccount(int accNo, String ifscCode, double balance, AccountType accType, double interestRate) throws InvalidIFSCException,MinimumBalanceException{
super(accNo, ifscCode, balance, accType);
if (interestRate < 0 || interestRate > 20) {
throw new IllegalArgumentException("Interest rate must be 0-20%: " + interestRate);
}
this.interestRate = interestRate;
}
// 3. Business Getters
public double getInterestRate() {
return interestRate;
}
// 4. Interest Calculation (Monthly simple interest)
@Override
public double calculateInterest() {
return getBalance() * (interestRate / 100) * (1.0 / 12); // Monthly interest
}
// 5. Professional toString()
@Override
public String toString() {
return super.toString() + ", Rate=" + String.format("%.2f%%", interestRate);
}
}