Source of BankDatabase.java


  1: // BankDatabase.java
  2: // Represents the bank account information database 

  4: public class BankDatabase
  5: {
  6:    private Account accounts[]; // array of Accounts
  7:    
  8:    // no-argument BankDatabase constructor initializes accounts
  9:    public BankDatabase()
 10:    {
 11:       accounts = new Account[ 2 ]; // just 2 accounts for testing
 12:       accounts[ 0 ] = new Account( 12345, 54321, 1000.0, 1200.0 );
 13:       accounts[ 1 ] = new Account( 98765, 56789, 200.0, 200.0 );  
 14:    } // end no-argument BankDatabase constructor
 15:    
 16:    // retrieve Account object containing specified account number
 17:    private Account getAccount( int accountNumber )
 18:    {
 19:       // loop through accounts searching for matching account number
 20:       for ( Account currentAccount : accounts )
 21:       {
 22:          // return current account if match found
 23:          if ( currentAccount.getAccountNumber() == accountNumber )
 24:             return currentAccount;
 25:       } // end for

 27:       return null; // if no matching account was found, return null
 28:    } // end method getAccount

 30:    // determine whether user-specified account number and PIN match
 31:    // those of an account in the database
 32:    public boolean authenticateUser( int userAccountNumber, int userPIN )
 33:    {
 34:       // attempt to retrieve the account with the account number
 35:       Account userAccount = getAccount( userAccountNumber );

 37:       // if account exists, return result of Account method validatePIN
 38:       if ( userAccount != null )
 39:          return userAccount.validatePIN( userPIN );
 40:       else
 41:          return false; // account number not found, so return false
 42:    } // end method authenticateUser

 44:    // return available balance of Account with specified account number
 45:    public double getAvailableBalance( int userAccountNumber )
 46:    {
 47:       return getAccount( userAccountNumber ).getAvailableBalance();
 48:    } // end method getAvailableBalance

 50:    // return total balance of Account with specified account number
 51:    public double getTotalBalance( int userAccountNumber )
 52:    {
 53:       return getAccount( userAccountNumber ).getTotalBalance();
 54:    } // end method getTotalBalance

 56:    // credit an amount to Account with specified account number
 57:    public void credit( int userAccountNumber, double amount )
 58:    {
 59:       getAccount( userAccountNumber ).credit( amount );
 60:    } // end method credit

 62:    // debit an amount from of Account with specified account number
 63:    public void debit( int userAccountNumber, double amount )
 64:    {
 65:       getAccount( userAccountNumber ).debit( amount );
 66:    } // end method debit
 67: } // end class BankDatabase



 71: /**************************************************************************
 72:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 73:  * Pearson Education, Inc. All Rights Reserved.                           *
 74:  *                                                                        *
 75:  * DISCLAIMER: The authors and publisher of this book have used their     *
 76:  * best efforts in preparing the book. These efforts include the          *
 77:  * development, research, and testing of the theories and programs        *
 78:  * to determine their effectiveness. The authors and publisher make       *
 79:  * no warranty of any kind, expressed or implied, with regard to these    *
 80:  * programs or to the documentation contained in these books. The authors *
 81:  * and publisher shall not be liable in any event for incidental or       *
 82:  * consequential damages in connection with, or arising out of, the       *
 83:  * furnishing, performance, or use of these programs.                     *
 84:  *************************************************************************/