/** * A little class to simulate some of the actions of a retail bank * with a passbooks savings account. * (Using a lot of imagination) * * @author gtowell * Created: Nov 2023 */ public class Bank { BankAccount[] accounts = null; int accountNumber = 2; int activeAccounts; public Bank(int size) { accounts = new BankAccount[size]; activeAccounts = 0; } private String nextAccountNumber() { accountNumber *= 1.5; String nextNum = "" + accountNumber; while (nextNum.length() < 9) { nextNum = " " + nextNum; } return nextNum; } public BankAccount makeAccount(String name, double initialDeposit) { BankAccount ba = new BankAccount(name, nextAccountNumber(), initialDeposit); accounts[activeAccounts] = ba; activeAccounts++; return ba; } public void listAccounts() { for (int i = 0; i < activeAccounts; i++) { System.out.println(accounts[i]); } } public BankAccount getAccountFromNumber(String accNum) { for (int i = 0; i < activeAccounts; i++) { if (accNum.equals(accounts[i].getAccountNumber())) { return accounts[i]; } } return null; } public void listAccountsByName() { BankAccount[] temp = new BankAccount[activeAccounts]; for (int i = 0; i < activeAccounts; i++) { temp[i] = accounts[i]; } for (int i = 0; i < temp.length; i++) { int bestloc = -1; for (int j = 0; j < temp.length; j++) { if (temp[j] != null) { if (bestloc < 0) { bestloc = j; } else { if (temp[bestloc].getName().compareTo(temp[j].getName()) < 0) { bestloc = j; } } } } System.out.println(temp[bestloc]); temp[bestloc] = null; } } public void listAccountsByBalance() { BankAccount[] temp = new BankAccount[activeAccounts]; for (int i = 0; i < activeAccounts; i++) { temp[i] = accounts[i]; } for (int i = 0; i < temp.length; i++) { int bestloc = -1; for (int j = 0; j < temp.length; j++) { if (temp[j] != null) { if (bestloc < 0) { bestloc = j; } else { if (temp[bestloc].getBalance() > temp[j].getBalance()) { bestloc = j; } } } } System.out.println(temp[bestloc]); temp[bestloc] = null; } } }