For questions 13 to 17, use the skeletal class definitions below: public class B
ID: 3591021 • Letter: F
Question
For questions 13 to 17, use the skeletal class definitions below: public class BankAccount { private double balance; private String accountNum; public double getBalance(); public String getAccountNum(); … } public class AccountManager { private ArrayList accountList; …} 13. Write the constructor for the AccountManager class that initializes accountList to an empty ArrayList. 14. Write a method for the accountManager class called addAccount that will add a new BankAccount object to accountList. 15. Write a method for the accountManager class called removeAccount that will remove a BankAccount object that is specified by its index from accountList. 16. Write a method for the accountManager class called listAccounts that will print all the BankAccount objects (balance and account number for each account, one account per line) in accountList. 17. Write a method for the accountManager class called findMax that will print the account number and balance for the BankAccount object with the highest balance in accountList.
Explanation / Answer
Please find my implementation.
import java.util.ArrayList;
public class AccountManager {
private ArrayList<BankAccount> accountList;
// 13 constructor
public AccountManager() {
accountList = new ArrayList<>();
}
//14
void addAccount(BankAccount account) {
accountList.add(account);
}
//15
void removeAccount(int index) {
accountList.remove(index);
}
//16
void listAccounts() {
for(BankAccount accnt : accountList) {
System.out.println(accnt.getBalance()+", "+accnt.getAccountNum());
}
}
void findMax() {
if(accountList.size() == 0) {
System.out.println("List is empty");
return;
}
BankAccount max = accountList.get(0);
for(int i=1; i<accountList.size(); i++) {
if(max.getBalance() < accountList.get(i).getBalance())
max = accountList.get(i);
}
System.out.println(max.getBalance()+", "+max.getAccountNum());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.