Question: 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
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. There is no starter code.
The BankAccount class is provided for you. A BankAccount has a balance and a customerName.
A Bank has a constructor that takes no parameters. It needs to initialize the ArrayList used to collect BankAccounts
It has methods
public void add(BankAccount a) adds the specified BankAccount to the Bank
public BankAccount smallest() gets the BankAccount with the smallest balance. If two have the same small balance, get the first one encountered in the list. If the ArrayList is empty, return null
public ArrayList
BankAccount.java
/** * A bank account has a balance that can be changed by * deposits and withdrawals * */ public class BankAccount { private double balance; private String customerName; /** * Constructs a bank account with a given balance. * @param initialBalance the initial balance * @param name the customerName for this account */ public BankAccount(double initialBalance, String name) { balance = initialBalance; customerName = name; } /** * Gets the customer name for this account * @returns the customer name for this account */ public String getCustomerName() { return customerName; } /** 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; } } BankTester.java
public class BankTester { public static void main(String[] args) { Bank bank = new Bank(); bank.add(new BankAccount(150.0, "Tim")); bank.add(new BankAccount(500.0, "Perkeet")); bank.add(new BankAccount(499.0, "Song")); bank.add(new BankAccount(699.0, "Fransisco")); bank.add(new BankAccount(1059.0, "Thien")); System.out.println(bank.smallest().getBalance()); System.out.println("Expected: 150.0"); System.out.println(bank.tList()); System.out.println("Expected: [150.0, 1059.0]"); bank.add(new BankAccount(2000.0, "Tara")); System.out.println(bank.tList()); System.out.println("Expected: [150.0, 1059.0, 2000.0]"); bank = new Bank(); System.out.println(bank.tList()); System.out.println("Expected: []"); BankAccount smallest = bank.smallest(); if (smallest == null) { System.out.println("empty Bank"); System.out.println("Expected: empty Bank"); } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
