/* VirtualTeller.java */ import sortedlist.*; /** An implementation of a virtual teller machine */ public class VirtualTeller { private static int nextAccountID = 100; /** Constructs a new virtual teller. */ public VirtualTeller() { accounts = new SortedList(); } /* Creates a new account for the customer `name'. * @return the new account's ID number. */ public int openAccount( String name ) { AccountData newData = new AccountData( name, nextAccountID ); accounts.insert( newData ); nextAccountID++; return newData.getNumber(); } /** Withdraws `amount' dollars from the account whose number is `acct'. * Assumes amount >= 0. * If `acct' is invalid, no action is taken. */ public void withdraw( int acct, int amount ) { AccountData account = findAccount( acct ); if( account == null ) { // didn't find System.out.println( "Error: Couldn't find account number `" + acct + "'" ); } else { account.withdraw( amount ); } } /** Deposits `amount' dollars into the bank account whose * id number is `acct'; if `acct' is invalid, no action is taken. * Assumes amount >= 0. * @param acct is an account number. * @param amount an amount of money. */ public void deposit( int acct, int amount ) { AccountData account = findAccount( acct ); if( account == null ) { System.out.println( "Error: Couldn't find account number `" + acct + "'" ); } else { account.deposit( amount ); } } /** Finds the balance on the account whose number is `acct'. * If `acct' is an invalid number, returns -1. * @param acct is an account number. * @return the balance. */ public int balanceInquiry( int acct ) { AccountData account = findAccount( acct ); if( account == null ) { System.out.println( "Error: Couldn't find account number `" + acct + "'" ); return -1; } else { return account.getBalance(); } } /** Get the AccountData object associated with the account number * `acct'. If `acct' does not refer to a valid account, returns null. * @param acct is an account number. */ private AccountData findAccount( int acct ) { AccountData account = (AccountData)accounts.find( acct ); return account; } private SortedList accounts; }