/** * file : VirtualTeller.java * desc : An implementation of a virtual teller machine */ import HashTable.*; public class VirtualTeller { private static int nextAccountID = 100; /** * post : creates a new virtual teller */ public VirtualTeller() { accounts = new HashTable(); } /** * post : creates a new account for the customer `name'. * returns the new account's ID number */ public int openAccount( String name ) { AccountNumber newAcctNumber = new AccountNumber( nextAccountID ); AccountData newData = new AccountData( name ); accounts.insert( newAcctNumber, newData ); nextAccountID++; return newAcctNumber.getNumber(); } /** * pre : amount >= 0 * * post : withdraws `amount' dollars from the account whose * number is `acct'. 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 ); } /** * post : deposits `amount' dollars into the bank account whose * id number is `acct'; if `acct' is invalid, no action * is taken. */ public void deposit( 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.deposit( amount ); } /** * post : returns the balance on the account whose number is * `acct'. * If `acct' is an invalid number, returns -1. */ 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(); } /** * post : returns the AccountData object associated with * the account number `acct'. If `acct' does not * refer to a valid account, returns null. */ private AccountData findAccount( int acct ) { AccountNumber acctNum = new AccountNumber( acct ); AccountData account = (AccountData)accounts.find( acctNum ); return account; } // fields private HashTable accounts; } // eof