-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranch.java
More file actions
60 lines (53 loc) 路 1.83 KB
/
Copy pathBranch.java
File metadata and controls
60 lines (53 loc) 路 1.83 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
public class Branch {
private int id;
private String name;
private Account [] accounts;
private int accountCount;
private static Branch[] branches = new Branch[20];
private static int branchCount = 0;
// you are not allowed to write any other constructor
public Branch(int id, String name) {
this.id = id;
this.name = name;
this.accounts = new Account[10];
if (branchCount < 20) {
branches[branchCount++] = this;
}
}
public void addAccount(Account a) {
accounts[accountCount++] = a;
}
public double getBranchBalance() {
double total = 0;
for (int i = 0; i < accountCount; i++) {
total += accounts[i].getBalance();
}
return total;
}
public Account getMinBalanceAccount() {
if (accountCount == 0) return null;
Account minAcc = accounts[0];
for (int i = 1; i < accountCount; i++) {
if (accounts[i].getBalance() < minAcc.getBalance()) {
minAcc = accounts[i];
}
}
return minAcc;
}
public static void transferBalance(Account from, Account to, double amount) {
if (from.getBalance() >= amount) {
from.setBalance(from.getBalance() - amount);
to.setBalance(to.getBalance() + amount);
}
}
public static void printAllBranchesInfo() {
for (int i = 0; i < branchCount; i++) {
Branch b = branches[i];
System.out.println("Branch Id: " + b.id + ", Branch Name: " + b.name);
for (int j = 0; j < b.accountCount; j++) {
Account acc = b.accounts[j];
System.out.println("Account Number: " + acc.getNumber() + ", Customer Name: " + acc.getCustomer() + ", Balance: " + acc.getBalance());
}
}
}
}