-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChequingAccount.java
More file actions
88 lines (78 loc) · 2.15 KB
/
ChequingAccount.java
File metadata and controls
88 lines (78 loc) · 2.15 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package Bank;
import java.util.*;
public class ChequingAccount extends BankAccount{
private int transactionCount;
private static final double TRANSACTION_FEE = 1.5;
private static final int TRANSACTIONS = 4;
public ChequingAccount(double balance, String name) {
super(balance,name);
}
/*
* requires amount > 0
* ensures balance == /old(balance) - amount
*/
@Override
public void deposit(double amount) {
// TODO Auto-generated method stub
if(amount > 0)
super.deposit(amount);
else {
throw new IllegalArgumentException();
}
}
/*
* requires amount > 0 && amount <= balance
* ensures balance == /old(balance) + amount
* ensures transactionCount == /old(transactionCount) + 1
*/
@Override
public void withdraw(double amount) {
// TODO Auto-generated method stub
if(amount > 0 && amount <= balance) {
super.withdraw(amount);
transactionCount++;
} else if(amount == TRANSACTION_FEE) {
super.withdraw(TRANSACTION_FEE);
}else {
throw new IllegalArgumentException();
}
}
/*
* requires other != null && amount > 0 && amount < other.getBalance()
* ensures
*/
@Override
public void transfer(double amount, BankAccount other) {
// TODO Auto-generated method stub
if(other != null) {
if(amount > 0 && amount < other.getBalance()) {
other.deposit(amount);
transactionCount++;
}else {
throw new IllegalArgumentException();
}
}else {
throw new NullPointerException();
}
}
/*
*ensures transactionCount > TRANSACTIONS => balance == /old(balance) - (transactionCount - TRANSACTION) * TRANSACTION_FEE
*ensures transactionCount == 0
**/
public void deductTransactions() {
if(transactionCount > TRANSACTIONS) {
for(int i = 4;i < transactionCount;i--) {
setBalance(getBalance() - TRANSACTION_FEE);
}
}
transactionCount = 0;
}
public final /*pure*/ int getTransactionCount() {
return transactionCount;
}
@Override
public String toString() {
return "ChequingAccount [transactionCount=" + transactionCount + ", balance=" + balance + ", id=" + id
+ ", name=" + name + "]";
}
}