import java.util.*; //needed for Scanner and Random class /* This program illustrates using objects as various TYPES because they * implement different interfaces */ public class Tester{ public static void main(String[] args) { //create service provider objects PrintingService printingService = new PrintingService(); BankingService bankingService = new BankingService(); SortingService sortingService = new SortingService(); //create some bank account objects ArrayList accounts = new ArrayList(); //make some bank accounts accounts.add(new BankAccount("Lou", 0.0) ); accounts.add(new BankAccount("Susan", 0.0) ); accounts.add(new BankAccount("Ann", 0.0) ); accounts.add(new BankAccount("Jason", 0.0) ); //do some transactions using the BankingService (which expects MoneyAccount objects) for(MoneyAccount aMoneyAccount : accounts) bankingService.doSomeTransactions(aMoneyAccount); //print each account using the PrintService (which expect Printable objects) for(Printable aPrintable : accounts){ printingService.print(aPrintable); } //sort the accounts by balance using the SortingService (which expects Comparable objects) //creat an actual list of Sortable objects which contains the accounts ArrayList accountsAsSortables = new ArrayList(); accountsAsSortables.addAll(accounts); sortingService.sort( accountsAsSortables); //sort the list of sortables System.out.println(""); System.out.println("The Accounts Sorted By Balance"); System.out.println("=============================="); for(Sortable s : accountsAsSortables){ printingService.print((Printable) s); //notice we need to cast here } } //end main } //end class Tester /*Sample output BankAccount #10001 ================== #10001 OWNER: Lou $280.0 BankAccount #10002 ================== #10002 OWNER: Susan $3441.0 BankAccount #10003 ================== #10003 OWNER: Ann $2616.0 BankAccount #10004 ================== #10004 OWNER: Jason $-662.0 The Accounts Sorted By Balance ============================== BankAccount #10004 ================== #10004 OWNER: Jason $-662.0 BankAccount #10001 ================== #10001 OWNER: Lou $280.0 BankAccount #10003 ================== #10003 OWNER: Ann $2616.0 BankAccount #10002 ================== #10002 OWNER: Susan $3441.0 */