import java.util.ArrayList;
/**
This bank contains a collection of bank accounts.
*/
public class Bank
{
/**
Constructs a bank with no bank accounts.
*/
public Bank()
{
accounts = new ArrayList<BankAccount>();
}
/**
Adds an account to this bank.
@param a the account to add
*/
public void addAccount(BankAccount a)
{
accounts.add(a);
}
/**
Gets the sum of the balances of all accounts in this bank.
@return the sum of the balances
*/
public double getTotalBalance()
{
double total = 0;
for (BankAccount a : accounts)
{
total = total + a.getBalance();
}
return total;
}
/**
Counts the number of bank accounts whose balance is atLeast a given value.
@param atLeast the balance required to count an account
@return the number of accounts having least the given balance
*/
public int count(double atLeast)
{
int matches = 0;
for (BankAccount a : accounts)
{
if (a.getBalance() >= atLeast) matches++; // Found a match
}
return matches;
}
/**
Finds a bank account with a given number.
@param accountNumber the number to find
@return the account with the given number, or null if there
is no such account
*/
public BankAccount find(int accountNumber)
{
for (BankAccount a : accounts)
{
if (a.getAccountNumber() == accountNumber) // Found a match
return a;
}
return null; // No match in the entire array list
}
/**
Gets the bank account with the largest balance.
@return the account with the largest balance, or null if the
bank has no accounts
*/
public BankAccount getMaximum()
{
if (accounts.size() == 0) return null;
BankAccount largestYet = accounts.get(0);
for (int i = 1; i < accounts.size(); i++)
{
BankAccount a = accounts.get(i);
if (a.getBalance() < largestYet.getBalance())
largestYet = a;
}
return largestYet;
}
private ArrayList<BankAccount> accounts;
}
/**
* mrs.e
* 12/14/2018
*
* ArrayLists
* for-each loop
* Iterator
*
* */
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListIterator
{
public static void main( String[] args )
{
// add elements to an ArrayList
String[] pets = { "Puppy", "Kitten", "Gerbil", "Bunny", "Ferret" };
ArrayList<String> aList = new ArrayList<String>();
for ( String pet : pets )
aList.add( pet ); // for each loop - don't change the ArrayList
// add elements in removePets array to removeList
String[] removePets = { "Gerbil", "Bunny" };
ArrayList<String> removeList = new ArrayList<String>();
for ( String pet : removePets )
removeList.add( pet );
// print items of the list
System.out.println( "ArrayList: " );
for ( int count = 0; count < aList.size(); count++ )
System.out.printf( "%s ", aList.get( count ) );
// remove from list the pets contained in removeList
removePets( aList, removeList );
// print items of the list
System.out.println( "\n\nArrayList after calling removePets: " );
for ( String pet : aList )
System.out.printf( "%s ", pet );
} // end main
// remove pets in collection2 found in collection1
private static void removePets( ArrayList<String> collection1,
ArrayList<String> collection2 )
{
// get iterator
Iterator<String> iterator = collection1.iterator();
// loop while collection has items
while ( iterator.hasNext() )
{
if ( collection2.contains( iterator.next() ) )
{
iterator.remove(); // remove current pet
}
} // end while
} // end method removePets
} // end class ArrayListIterator