-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankTest.java
More file actions
139 lines (112 loc) · 3.47 KB
/
BankTest.java
File metadata and controls
139 lines (112 loc) · 3.47 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.ArrayList;
// Account class remains unchanged
class Account {
private double bal; // The current balance
private int accnum; // The account number
public Account(int a) {
bal = 0.0;
accnum = a;
}
public void deposit(double sum) {
if (sum > 0)
bal += sum;
else
System.err.println("Account.deposit(...): "
+ "cannot deposit negative amount.");
}
public void withdraw(double sum) {
if (sum > 0)
bal -= sum;
else
System.err.println("Account.withdraw(...): "
+ "cannot withdraw negative amount.");
}
public double getBalance() {
return bal;
}
public double getAccountNumber() {
return accnum;
}
public String toString() {
return "Acc " + accnum + ": " + "balance = " + bal;
}
public final void print() {
// Don't override this,
// override the toString method
System.out.println(toString());
}
}
// Derived SavingsAccount class
class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(int a, double rate) {
super(a);
interestRate = rate;
}
public void addInterest() {
super.deposit(super.getBalance() * interestRate);
}
}
// Derived CurrentAccount class
class CurrentAccount extends Account {
private double overdraftLimit;
public CurrentAccount(int a, double limit) {
super(a);
overdraftLimit = limit;
}
@Override
public void withdraw(double sum) {
if (super.getBalance() - sum >= -overdraftLimit)
super.withdraw(sum);
else
System.err.println("CurrentAccount.withdraw(...): "
+ "exceeds overdraft limit.");
}
}
// Bank class
class Bank {
private ArrayList<Account> accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void openAccount(Account acc) {
accounts.add(acc);
}
public void closeAccount(Account acc) {
accounts.remove(acc);
}
public void payDividend(double dividend) {
for (Account acc : accounts) {
acc.deposit(dividend);
}
}
public void update() {
for (Account acc : accounts) {
if (acc instanceof SavingsAccount) {
((SavingsAccount) acc).addInterest();
} else if (acc instanceof CurrentAccount && acc.getBalance() < 0) {
System.out.println("Sending letter to account number " + acc.getAccountNumber() + " for overdraft.");
}
}
}
}
public class BankTest {
public static void main(String[] args) {
// Testing classes
SavingsAccount savings = new SavingsAccount(1001, 0.05);
CurrentAccount current = new CurrentAccount(2001, 500);
Bank bank = new Bank();
bank.openAccount(savings);
bank.openAccount(current);
savings.deposit(1000);
current.deposit(2000);
bank.update();
System.out.println("Savings Account Balance: " + savings.getBalance());
System.out.println("Current Account Balance: " + current.getBalance());
bank.payDividend(100);
bank.update();
System.out.println("After paying dividend...");
System.out.println("Savings Account Balance: " + savings.getBalance());
System.out.println("Current Account Balance: " + current.getBalance());
}
}