Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following files are used to check your work: BankAccount.java BankTester.jav

ID: 3593111 • Letter: T

Question

The following files are used to check your work:

BankAccount.java

BankTester.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 BankAccount objects. You will write the Bank class The BankAccount class is provided for you. A BankAcount has a balance and a accountld. A Bank has a constructor that takes no parameters. It needs to initialize the Arrayl ist 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 determins if a BankAccount with a give aountId is in the Bank. Return true is so, otherwise false list gets an ArrayList of the BankAccount accountIds in the Bank tat have balauces over the specified amount list gets an ArrayList of the BankAccount accountlds in the Bank Provide Javadoc.

Explanation / Answer

public class BankTester {

public static void main(String[] args) {

Bank accounts = new Bank();

// test empty bank

accounts.largestFirst();

System.out.println(accounts.list());

System.out.println("Expected: []");

accounts.largestFirst();

System.out.println(accounts.list());

System.out.println("Expected: []");

System.out.println(accounts.contains("abc123"));

System.out.println("Expected: false");

System.out.println(accounts.list(10000));

System.out.println("Expected: []");

accounts.add(new BankAccount(10000.0, "def333"));

accounts.add(new BankAccount(20000.0, "abc123"));

accounts.add(new BankAccount(15000.0, "pqr456"));

accounts.add(new BankAccount(20000.0, "xyz789"));

accounts.add(new BankAccount(9500.0, "abc111"));

accounts.largestFirst();

System.out.println(accounts.list());

System.out

.println("Expected: [abc123, def333, pqr456, xyz789, abc111]");

System.out.println(accounts.contains("abc123"));

System.out.println("Expected: true");

System.out.println(accounts.contains("aaa999"));

System.out.println("Expected: false");

System.out.println(accounts.list(10000));

System.out.println("Expected: [abc123, pqr456, xyz789]");

System.out.println(accounts.list(100000));

System.out.println("Expected: []");

// accounts.add();

}

}

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Bank {

List<BankAccount> bankAccounts;

/**

*

*/

public Bank() {

// TODO Auto-generated constructor stub

bankAccounts = new ArrayList<BankAccount>();

}

/**

*

*/

public void largestFirst() {

// TODO Auto-generated method stub

Collections.sort(bankAccounts);

}

/**

* @return

*/

public ArrayList<String> list() {

// TODO Auto-generated method stub

ArrayList<String> accountIDs = new ArrayList<>();

for (int i = 0; i < bankAccounts.size(); i++) {

accountIDs.add(bankAccounts.get(i).getAccountId());

}

return accountIDs;

}

/**

* @param balance

* @return

*/

public ArrayList<String> list(double balance) {

// TODO Auto-generated method stub

ArrayList<String> accountIDs = new ArrayList<>();

for (int i = 0; i < bankAccounts.size(); i++) {

if (balance < bankAccounts.get(i).getBalance())

accountIDs.add(bankAccounts.get(i).getAccountId());

}

return accountIDs;

}

public boolean contains(String accountID) {

// TODO Auto-generated method stub

boolean flag = false;

for (int i = 0; i < bankAccounts.size(); i++) {

if (accountID.equals(bankAccounts.get(i).getAccountId()))

flag = true;

}

return flag;

}

/**

* @param bankAccount

*/

public void add(BankAccount bankAccount) {

// TODO Auto-generated method stub

bankAccounts.add(bankAccount);

}

}

public class BankAccount implements Comparable<BankAccount> {

private double balance;

private String accountId;

/**

* Constructs a bank account with a given balance.

*

* @param initialBalance

* the initial balance

* @param id

* the id for this account

*/

public BankAccount(double initialBalance, String id) {

balance = initialBalance;

accountId = id;

}

/**

* Gets the id for this account

*

* @returns the id for this account

*/

public String getAccountId() {

return accountId;

}

/**

* Deposits money into the bank account.

*

* @param amount

* the amount to deposit

*/

public void deposit(double amount) {

balance = balance + amount;

}

/**

* Withdraws money from the bank account.

*

* @param amount

* the amount to withdraw

*/

public void withdraw(double amount) {

balance = balance - amount;

}

/**

* Gets the current balance of the bank account.

*

* @return the current balance

*/

public double getBalance() {

return balance;

}

@Override

public int compareTo(BankAccount account) {

// TODO Auto-generated method stub

if (this.getBalance() < account.getBalance())

return 0;

else

return 1;

}

}

OUTPUT:

[]
Expected: []
[]
Expected: []
false
Expected: false
[]
Expected: []
[def333, abc123, pqr456, xyz789, abc111]
Expected: [abc123, def333, pqr456, xyz789, abc111]
true
Expected: true
false
Expected: false
[abc123, pqr456, xyz789]
Expected: [abc123, pqr456, xyz789]
[]
Expected: []

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote