import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; import javax.swing.JOptionPane; /* * BankAccountExerciseSolution.java * * Created on October 5, 2005, 4:02 PM * * Author: Bill Kraynek */ /** * * @author Bill Kraynek */ public class BankAccountExerciseSolution { /** Creates a new instance of BankAccountExerciseSolution */ public BankAccountExerciseSolution() throws Exception { ArrayList accounts = new ArrayList(); Scanner fileScanner = new Scanner(new File("bankData.txt")); while( fileScanner.hasNext() ) { String name = fileScanner.next(); BankAccount anAccount = new BankAccount(name); if( accounts.contains(anAccount) ) { // if anAccount is in accounts find it and assign anAccount to it int accountIndex = accounts.indexOf(anAccount); anAccount = accounts.get(accountIndex); } else { // if anAccount is not there add it accounts.add(anAccount); } // end if String transaction = fileScanner.next(); double amount = fileScanner.nextDouble(); if( transaction.equals("d") ) { // make a deposit anAccount.deposit(amount); }// end if if( transaction.equals("w") ) { // make a withdrawal anAccount.withdraw(amount); }// end if }// end while JOptionPane.showMessageDialog(null,accounts); Collections.sort(accounts); JOptionPane.showMessageDialog(null,accounts); Collections.sort(accounts, new CompareBalances()); JOptionPane.showMessageDialog(null,accounts); } /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { new BankAccountExerciseSolution(); } class CompareBalances implements Comparator { public int compare(BankAccount left, BankAccount right) { return (int)(right.getBalance() - left.getBalance()); } } }