[Java] In this problem you will implement a solution using the design pattern fo
ID: 3863735 • Letter: #
Question
[Java]
In this problem you will implement a solution using the design pattern for collecting objects. We are going to model a bank. A Bank uses an ArrayList to keep track of BankAccountobjects. You will write the Bank class
The BankAccount class is provided for you. A BankAcount has a balance and a accountId.
A Bank has a constructor that takes no parameters. It needs to initialize the ArrayList used to collect bankAccounts
It has methods
add adds the specified BankAccount to the Bank
largestFirst puts the BankAccount with the largest balance first in the list. If two have the same large balance, use the first one encountered in the list
contains determines if a BankAccount with a given accountId is in the Bank. Return true is so, otherwise false
list gets an ArrayList of the BankAccount accountIds in the Bank that have balances over the specified amount.
list gets an ArrayList of the BankAccount accountIds in the Bank
Use the following files:
BankAccount.java
BankTester.java
Explanation / Answer
import java.util.*;
/*
Make sure you compile BankAccount.java first followed by Bank.java !
*/
public class Bank{
private ArrayList<BankAccount> bankAccounts;
// Constructor
Bank(){
bankAccounts = new ArrayList<BankAccount>();
}
public void add(BankAccount account){
bankAccounts.add(account);
}
public void largestFirst(){
if(bankAccounts.size() > 1){
int largestIndex = -1;
double largestBalance = -1.0;
for(int i=0;i<bankAccounts.size();i+=1){
BankAccount temp = bankAccounts.get(i);
if(temp.getBalance() > largestBalance){
largestBalance = temp.getBalance();
largestIndex = i;
}
}
if(largestIndex != 0){
BankAccount temp = bankAccounts.get(largestIndex);
bankAccounts.remove(largestIndex);
bankAccounts.add(0,temp);
}
}
}
public boolean contains(String accountId){
for(int i=0;i<bankAccounts.size();i+=1){
String currentAccountId = (bankAccounts.get(i)).getAccountId();
if(currentAccountId.equals(accountId)){
return true;
}
}
return false;
}
public ArrayList list(double thresholdAmount){
ArrayList accountList = new ArrayList();
for(int i=0;i<bankAccounts.size();i+=1){
if(((bankAccounts.get(i)).getBalance()) > thresholdAmount){
accountList.add((bankAccounts.get(i)).getAccountId());
}
}
return accountList;
}
public ArrayList list(){
ArrayList accountList = new ArrayList();
for(int i=0;i<bankAccounts.size();i+=1){
accountList.add((bankAccounts.get(i)).getAccountId());
}
return accountList;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.