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