1 import java.io.Serializable;
2 import java.util.ArrayList;
3
4 /**
5 This bank contains a collection of bank accounts.
6 */
7 public class Bank implements Serializable
8 {
9 private ArrayList<BankAccount> accounts;
10
11 /**
12 Constructs a bank with no bank accounts.
13 */
14 public Bank()
15 {
16 accounts = new ArrayList<BankAccount>();
17 }
18
19 /**
20 Adds an account to this bank.
21 @param a the account to add
22 */
23 public void addAccount(BankAccount a)
24 {
25 accounts.add(a);
26 }
27
28 /**
29 Gets the sum of the balances of all accounts in this bank.
30 @return the sum of the balances
31 */
32 public double getTotalBalance()
33 {
34 double total = 0;
35 for (BankAccount a : accounts)
36 {
37 total = total + a.getBalance();
38 }
39 return total;
40 }
41
42 /**
43 Counts the number of bank accounts whose balance is at
44 least a given value.
45 @param atLeast the balance required to count an account
46 @return the number of accounts having least the given balance
47 */
48 public int count(double atLeast)
49 {
50 int matches = 0;
51 for (BankAccount a : accounts)
52 {
53 if (a.getBalance() >= atLeast) matches++; // Found a match
54 }
55 return matches;
56 }
57
58 /**
59 Finds a bank account with a given number.
60 @param accountNumber the number to find
61 @return the account with the given number, or null if there
62 is no such account
63 */
64 public BankAccount find(int accountNumber)
65 {
66 for (BankAccount a : accounts)
67 {
68 if (a.getAccountNumber() == accountNumber) // Found a match
69 return a;
70 }
71 return null; // No match in the entire array list
72 }
73
74 /**
75 Gets the bank account with the largest balance
76 @return the account with the largest balance, or null if the
77 bank has no accounts
78 */
79 public BankAccount maximum()
80 {
81 if (accounts.size() == 0) return null;
82 BankAccount largestYet = accounts.get(0);
83 for (int i = 1; i < accounts.size(); i++)
84 {
85 BankAccount a = accounts.get(i);
86 if (a.getBalance() > largestYet.getBalance())
87 largestYet = a;
88 }
89 return largestYet;
90 }
91 }