-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavingsAccount.java
More file actions
63 lines (52 loc) · 1.45 KB
/
SavingsAccount.java
File metadata and controls
63 lines (52 loc) · 1.45 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package Bank;
public class SavingsAccount extends BankAccount {
private final double interestRate = 0.02;
public SavingsAccount(double amount, String name) {
super(amount, name);
}
/*
* requires amount > 0 && amount < other.getBalance() && other != null
* ensures other.getBalance = /old(ohter.getBalance) + amount && balance == /old(balance) - amount
* */
@Override
public void transfer(double amount, BankAccount other) {
// TODO Auto-generated method stub
if(other != null) {
if(amount > 0 && amount < other.getBalance()) {
withdraw(amount);
other.deposit(amount);
}else {
throw new IllegalArgumentException();
}
}else {
throw new NullPointerException();
}
}
/*
* ensures amount >= 1000 -> balance == /old(balance) + amount
* */
@Override
public void deposit(double amount) {
if(amount >= 1000) {
super.deposit(amount);
}
}
//ensures balance == /old(balance) - amount
@Override
public void withdraw(double amount) {
// TODO Auto-generated method stub
super.withdraw(amount);
}
private double interest() {
return /*pure*/ balance * interestRate;
}
//ensures balance == balance + balance * interestRate
public void addInterest() {
balance += interest();
}
@Override
public String toString() {
return "SavingsAccount [interestRate=" + interestRate + ", balance=" + balance + ", id=" + id + ", name=" + name
+ "]";
}
}