farizmpr

Untitled

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