GSculerlor

BankDatabase.java

Oct 26th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. /**
  2.  * Represents the bank account information database
  3.  */
  4.  
  5. public class BankDatabase {
  6.     private Account[] accounts; // array of Accounts
  7.  
  8.     // no-argument BankDatabase constructor initializes accounts
  9.     public BankDatabase() {
  10.         accounts = new Account[2]; // just 2 accounts for testing
  11.         accounts[0] = new Account(12345, 54321, 1000.0, 1200.0);
  12.         accounts[1] = new Account(98765, 56789, 200.0, 200.0);
  13.     } // end no-argument BankDatabase constructor
  14.  
  15.     // retrieve Account object containing specified account number
  16.     private Account getAccount(int accountNumber) {
  17.         // loop through accounts searching for matching account number
  18.         for (Account currentAccount : accounts) {
  19.             // return current account if match found
  20.             if (currentAccount.getAccountNumber() == accountNumber)
  21.                 return currentAccount;
  22.         } // end for
  23.  
  24.         return null; // if no matching account was found, return null
  25.     } // end method getAccount
  26.  
  27.     // determine whether user-specified account number and PIN match
  28.     // those of an account in the database
  29.     public boolean authenticateUser(int userAccountNumber, int userPIN) {
  30.         // attempt to retrieve the account with the account number
  31.         Account userAccount = getAccount(userAccountNumber);
  32.  
  33.         // if account exists, return result of Account method validatePIN
  34.         if (userAccount != null)
  35.             return userAccount.validatePIN(userPIN);
  36.         else
  37.             return false; // account number not found, so return false
  38.     } // end method authenticateUser
  39.  
  40.     // return available balance of Account with specified account number
  41.     public double getAvailableBalance(int userAccountNumber) {
  42.         return getAccount(userAccountNumber).getAvailableBalance();
  43.     } // end method getAvailableBalance
  44.  
  45.     // return total balance of Account with specified account number
  46.     public double getTotalBalance(int userAccountNumber) {
  47.         return getAccount(userAccountNumber).getTotalBalance();
  48.     } // end method getTotalBalance
  49.  
  50.     // credit an amount to Account with specified account number
  51.     public void credit(int userAccountNumber, double amount) {
  52.         getAccount(userAccountNumber).credit(amount);
  53.     } // end method credit
  54.  
  55.     // debit an amount from Account with specified account number
  56.     public void debit(int userAccountNumber, double amount) {
  57.         getAccount(userAccountNumber).debit(amount);
  58.     } // end method debit
  59. } // end class BankDatabase
Add Comment
Please, Sign In to add comment