-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank.java
83 lines (83 loc) · 2.97 KB
/
Bank.java
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
public class Bank extends Object {
private int numberOfAccounts;
private Account[] accounts;
public Bank() { // constructor to init. instance variables
numberOfAccounts = 0;
accounts = new Account[3];
}
public Account[] getAccounts() { // accounts getter
return accounts;
}
public void displayAccounts(Account[] accounts) { // accounts printer
for (int i = 0; i < numberOfAccounts; i++) {
System.out.println(accounts[i].accountToString());
}
}
public int getNumberOfAccounts() { // number of accounts getter
return numberOfAccounts;
}
public void addAccount(String name, double amount) { // account adder
if (numberOfAccounts == accounts.length) {
Account[] tmp = new Account[accounts.length*2];
for (int i = 0; i < numberOfAccounts; i++) {
tmp[i] = accounts[i];
}
accounts = tmp;
}
accounts[numberOfAccounts++] = new Account(name, amount);
}
public void deleteAccount(String name, double amount) { // account remover
int check = 0;
for (int i = 0; i < numberOfAccounts; i++) {
if (accounts[i].getName().equals(name) && (accounts[i].getBalance() == (amount))) {
check = i;
}
}
int j;
for (j = check+1; j < numberOfAccounts; j++) {
accounts[j-1] = accounts[j];
}
accounts[--j] = new Account("Deleted", 0.0);
}
public Account findAccount(String name) { // account finder
int left = 0, right = numberOfAccounts-1, compared = 0;
while (left <= right) {
int middle = left + (right - left) / 2;
for (int i = 0; i < numberOfAccounts; i++) {
compared = name.compareTo(accounts[i].getName());
if (compared == 0) {
return accounts[i];
}
else {
if (compared > 0) {
left = middle + 1;
}
else {
right = middle - 1;
}
}
}
}
System.err.println("No such account");
return null;
}
public void sortAccounts(Account[] accounts) { // accounts sorter
for (int i = 0; i < numberOfAccounts; i++) {
for (int j = 0; j < numberOfAccounts; j++) {
int compared = accounts[i].getName().compareTo(accounts[j].getName());
if (compared < 0) {
Account account = accounts[i];
accounts[i] = accounts[j];
accounts[j] = account;
}
}
}
}
public double getTotalAccountsBalance(Account[] accounts) { // total accounts balance getter
double sum = 0;
for (int i = 0; i < numberOfAccounts; i++) {
sum += accounts[i].getBalance();
}
return sum;
}
}