-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
69 lines (59 loc) · 1.98 KB
/
Bank.java
File metadata and controls
69 lines (59 loc) · 1.98 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
/**
* A Bank can hold up to a fixed number of BankAccounts.
*/
public class Bank {
/** The array of BankAccount objects. */
private BankAccount[] accounts;
/** The first available account index. */
private int firstAvailableAcc;
/**
* Creates a bank that can have up to numAccounts accounts.
*/
public Bank(int numAccounts) {
this.accounts = new BankAccount[numAccounts];
this.firstAvailableAcc = 0;
}
/**
* Adds the given BankAccount to the bank. If the bank is full
* an error message is printed and the bank is unchanged.
* @param account The account to add
*/
public void add(BankAccount account) {
if (firstAvailableAcc == accounts.length) {
System.out.println("Bank is full. No account added.");
return;
}
this.accounts[firstAvailableAcc] = account;
firstAvailableAcc++;
}
/**
* Returns the bank account with the given account number.
* If no such account exists, null is returned.
* @param acctNumber The account number
* @return The account
*/
public BankAccount find(int acctNumber) {
for (int i = 0; i < firstAvailableAcc; i++) {
if (accounts[i].getAccountNumber() == acctNumber) {
return accounts[i];
}
}
return null;
}
/**
* Returns a string representation of the bank.
* The format is one account per line.
*/
public String toString() {
if (firstAvailableAcc == 0) return "NONE";
String result = "";
for (int i = 0; i < firstAvailableAcc; i++) {
result += accounts[i].getAccountNumber() + " ";
result += accounts[i].getBalance() + "\n";
// Note that we don't make use of BankAccount's toString because
// we don't want to have dollar signs ($) as part of the String
// representation for the Bank.
}
return result;
}
}