Question: If I have a class like this in Java, how would I finish the public boolean add(Account account) and the public boolean hasAccountNumber (int accountNumber)
If I have a class like this in Java, how would I finish the public boolean add(Account account) and the public boolean hasAccountNumber (int accountNumber) method? Suppose Account is a different class(not shown here) if a primitive type object is required from that class to finish the code just type ".primitiveInt" for int for me to understand( and i will know what i need to add instead) The main class of the is shown in bold and so is the output. public class Bank { private String name; /** * An array of Accounts managed by * this bank. */ private Account [] accounts; private int numAccounts;//number of active accounts public Bank(String name, int maxNumberAccounts) { this.name = name; accounts = new Account[maxNumberAccounts]; numAccounts = 0; } /** * @return the name */ public String getName() { return name; //Fix this } /** * @return the numAccounts */ public int getNumAccounts() { return numAccounts; //Fix this } public Account[] getAccounts() { return accounts; //Fix this } /** * Return true if the Bank already has an account * with this number; otherwise false. * @param accountNumber * @return */ public boolean hasAccountNumber(int accountNumber) { return false; //Fix this } /** * Adds the specified account to the Bank if possible. If the account number * already exists or the Bank has reached its maximum * number of accounts, return false and do not add it; otherwise, * add it and return true. * @param account * @return true if successful */ public boolean add(Account account) { return true; //Fix this } @Override public String toString() { //DO NOT MODIFY THIS CODE String s = getName() + ": " + getNumAccounts() + " of " + getAccounts().length + " accounts open"; for(Account account : getAccounts()) { if (account == null) break; s += " " + account; } return s; } }
public class MainBank { /** * @param args the command line arguments */ public static void main(String[] args) { Bank [] banks = {new Bank("Toronto Dominion", 3), new Bank("Bank of Montreal", 5)}; Bank td = banks[0]; Bank bmo = banks[1]; System.out.println(td); Account charlie = new Account("Charles", 234, 200.00); td.add(charlie); System.out.println(td); Account dora = new Account("Dora", 456, 300.00); td.add(dora); System.out.println("td has account # 456: " + td.hasAccountNumber(456)); Account ed = new Account("Edward", 456, 400.00); for(Bank bank : banks) { if (bank.add(ed)) break; } for(Bank bank : banks) { System.out.println(bank); } }
}
output:
Toronto Dominion: 0 of 3 accounts open
Toronto Dominion: 1 of 3 accounts open
(Charles, 234, $200.00)
td has account # 456: true
Toronto Dominion: 2 of 3 accounts open
(Charles, 234, $200.00)
(Dora, 456, $300.00)
Bank of Montreal: 1 of 5 accounts open
(Edward, 456, $400.00)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
