ATM CASE STUDY


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Screen
  2. {
  3.     public void displayMessage(String message)
  4.     {
  5.         System.out.print(message);
  6.     }
  7.     public void displayMessageLine(String message)
  8.     {
  9.         System.out.println(message);
  10.     }
  11.     public void displayDollarAmount(double amount)
  12.     {
  13.         System.out.printf("$%,.2f",amount);
  14.     }
  15. }

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Scanner;
  2. public class Keypad
  3. {
  4.     private Scanner input;
  5.    
  6.     public Keypad()
  7.     {
  8.         input=new Scanner(System.in);
  9.     }
  10.    
  11.     public int getInput()
  12.     {
  13.         return input.nextInt();
  14.     }
  15. }

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class CashDispenser
  2. {
  3.     private final static int INITIAL_COUNT=500;
  4.     private int count;
  5.    
  6.     public CashDispenser()
  7.     {
  8.         count=INITIAL_COUNT;
  9.     }
  10.    
  11.     public void dispenseCash(int amount)
  12.     {
  13.         int billsRequired=amount/20;
  14.         count -=billsRequired;
  15.     }
  16.    
  17.     public boolean isSufficientCashAvailable(int amount)
  18.     {
  19.         int billsRequired=amount/20;
  20.         if(count>=billsRequired)
  21.             return true;
  22.         else
  23.             return false;
  24.     }
  25. }

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class DepositSlot
  2. {
  3.     public boolean isEnvelopeReceived()
  4.     {
  5.         return true;
  6.     }
  7. }

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public abstract class Transaction
  2. {
  3.     private int accountNumber;      // indicates account involved
  4.     private Screen screen;          // ATM's screen
  5.     private BankDatabase bankDatabase;  // account info database
  6.    
  7.     // Transaction constructor invoked by subclasses using super()
  8.     public Transaction(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase){
  9.         accountNumber = userAccountNumber;
  10.         screen = atmScreen;
  11.         bankDatabase = atmBankDatabase;
  12.     }   // end Transaction constructor
  13.    
  14.     // return account number
  15.     public int getAccountNumber(){
  16.         return accountNumber;
  17.     }   // end method
  18.    
  19.     // return reference to screen
  20.     public Screen getScreen(){
  21.         return screen;
  22.     }   // end method
  23.    
  24.     // return reference to bank database
  25.     public BankDatabase getBankDatabase(){
  26.         return bankDatabase;
  27.     }   // end method
  28.    
  29.     // perform the transaction (overridden by each subclass)
  30.     abstract public void execute();
  31. }   // end class Transaction

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class BalanceInquiry extends Transaction
  2. {
  3.     // BalanceInquiry constructor
  4.     public BalanceInquiry(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase){
  5.         super(userAccountNumber, atmScreen, atmBankDatabase);
  6.     }   // end BalanceInquiry constructor
  7.    
  8.     // performs the transaction
  9.     @Override
  10.     public void execute(){
  11.         // get references to bank database and screen
  12.         BankDatabase bankDatabase = getBankDatabase();
  13.         Screen screen = getScreen();
  14.        
  15.         // get the available balance for the account involved
  16.         double availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
  17.        
  18.         // get the total balance for the account involved
  19.         double totalBalance = bankDatabase.getTotalBalance(getAccountNumber());
  20.        
  21.         // display the balance information on the screen
  22.         screen.displayMessageLine("\nBalance Information : ");
  23.         screen.displayMessage(" - Available Balance : ");
  24.         screen.displayDollarAmount(availableBalance);
  25.         screen.displayMessage("\n - Total Balance : ");
  26.         screen.displayDollarAmount(totalBalance);
  27.         screen.displayMessageLine("");
  28.     }   // end method execute
  29. }   // end class BalanceInquiry

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Withdrawal extends Transaction
  2. {
  3.     private int amount;     // amount to withdraw
  4.     private Keypad keypad;  // references to keypad
  5.     private CashDispenser cashDispenser;    // references to cash dispenser
  6.    
  7.     // constant corresponding to menu option to cancel
  8.     private final static int CANCELED = 6;
  9.    
  10.     // Withdrawal constructor
  11.     public Withdrawal(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, CashDispenser atmCashDispenser){
  12.         // initializes superclass variables
  13.         super(userAccountNumber, atmScreen, atmBankDatabase);
  14.        
  15.         // initializes references to keypad and cash dispenser
  16.         keypad = atmKeypad;
  17.         cashDispenser = atmCashDispenser;
  18.     }   // end Withdrawal constructor
  19.    
  20.     // perform transaction
  21.     @Override
  22.     public void execute(){
  23.         boolean cashDispensed = false;  // cash was not dispensed yet
  24.         double availableBalance;        // amount available for withdrawal
  25.        
  26.         // get references to bank database and screen
  27.         BankDatabase bankDatabase = getBankDatabase();
  28.         Screen screen = getScreen();
  29.        
  30.         // loop until cash is dispensed or the user cancels
  31.         do{
  32.             // obtain a chosen withdrawal amount from the user
  33.             amount = displayMenuOfAmounts();
  34.            
  35.             // check whether user choose a withdrawal amount or canceled
  36.             if(amount != CANCELED){
  37.                 // get available balance of account involved
  38.                 availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
  39.                
  40.                 // check whether the user has enough money in the account
  41.                 if(amount <= availableBalance){
  42.                    
  43.                     // check whether the cash dispenser has enough money
  44.                     if(cashDispenser.isSufficientCashAvailable(amount)){
  45.                        
  46.                         // update the account involved to reflect the withdrawal
  47.                         bankDatabase.debit(getAccountNumber(), amount);
  48.                        
  49.                         cashDispenser.dispenseCash(amount);     // dispense cash
  50.                         cashDispensed = true;   // cash was dispensed
  51.                        
  52.                         // instructs user to take cash
  53.                         screen.displayMessageLine("\nYour cash has been dispensed. Please take your cash now.");
  54.                     }   // end if
  55.                     else{
  56.                         // cash dispenser does not have enough cash
  57.                         screen.displayMessageLine("\nInsufficient cash available in the ATM.");
  58.                         screen.displayMessageLine("\nPlease choose a smaller amount.");
  59.                     }   // end if
  60.                 }   // end if
  61.                 else{
  62.                     // not enough money available in user's account
  63.                     screen.displayMessageLine("\nInsufficient funds in your account.");
  64.                     screen.displayMessageLine("\nPlease choose a smaller amount.");
  65.                 }   // end if
  66.             }   // end if
  67.             else{
  68.                 // user choose cancel menu option
  69.                 screen.displayMessageLine("\nCancelling transactions...");
  70.                 return;     // return to main menu because user canceled
  71.             }   // end if
  72.         }   while(!cashDispensed);
  73.     }   // end method execute
  74.    
  75.     // display a menu of withdrawal amounts and the options to cancel
  76.     // return the chosen amount or 0 if the user chooses to cancel
  77.     private int displayMenuOfAmounts(){
  78.         int userChoice = 0;     // local variable to store return value
  79.        
  80.         Screen screen = getScreen();    // get screen references
  81.        
  82.         // array of amounts to correspond to menu numbers
  83.         int[] amounts = {0, 20, 40, 60, 100, 200};
  84.        
  85.         // loop while no valid choice has been made
  86.         while(userChoice == 0){
  87.             // display the withdrawal menu
  88.             screen.displayMessageLine("\nWithdrawal Menu : ");
  89.             screen.displayMessageLine("1 - $20");
  90.             screen.displayMessageLine("2 - $40");
  91.             screen.displayMessageLine("3 - $60");
  92.             screen.displayMessageLine("4 - $100");
  93.             screen.displayMessageLine("5 - $200");
  94.             screen.displayMessageLine("6 - Cancel Transaction");
  95.             screen.displayMessage("\nChoose a withdrawal amount : ");
  96.            
  97.             int input = keypad.getInput();      // get user input through keypad
  98.            
  99.             // determine how to proceed based on the input value
  100.             switch(input){
  101.                 // if the user choose a withdrawal amount
  102.                 // i.e choose option 1, 2, 3, 4 or 5
  103.                 // return the corresponding amount from amounts's array
  104.                 case 1 :
  105.                 case 2 :
  106.                 case 3 :
  107.                 case 4 :
  108.                 case 5 :
  109.                     userChoice = amounts[input];        // save user's choice
  110.                     break;
  111.                
  112.                 case CANCELED :
  113.                     // the user choose to cancel
  114.                     userChoice = CANCELED;      // save user's choice
  115.                     break;
  116.                
  117.                 default :
  118.                     // the user did not enter value between 1-6
  119.                     screen.displayMessageLine("\nInvalid selection.");
  120.                     screen.displayMessageLine("Try again.");
  121.             }   // end switch
  122.         }   // end while
  123.        
  124.         return userChoice;      // return withdrawal amount or CANCELED
  125.     }   // end method displayMenuOfAmounts
  126. }   // end class Withdrawal

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Deposit extends Transaction
  2. {
  3.     private double amount;      // amount to deposit
  4.     private Keypad keypad;      // references to keypad
  5.     private DepositSlot depositSlot;    // references to deposit slot
  6.     private final static int CANCELED = 0;  // constant for cancel option
  7.    
  8.     // Deposit constructor
  9.     public Deposit(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, DepositSlot atmDepositSlot){
  10.         // initializes superclass variables
  11.         super(userAccountNumber, atmScreen, atmBankDatabase);
  12.        
  13.         // initialize references to keypad and deposit slot
  14.         keypad = atmKeypad;
  15.         depositSlot = atmDepositSlot;
  16.     }   // end Deposit constructor
  17.    
  18.     // perform transaction
  19.     @Override
  20.     public void execute(){
  21.         BankDatabase bankDatabase = getBankDatabase();      // get reference
  22.         Screen screen = getScreen();        // get reference
  23.        
  24.         amount = promptForDepositAmount();  // get deposit amount from user
  25.        
  26.         // check whether the user entered a deposit amount or canceled
  27.         if(amount != CANCELED){
  28.             // request deposit envelope containing specified amount
  29.             screen.displayMessage("\nPlease insert a deposit envelope containing ");
  30.             screen.displayDollarAmount(amount);
  31.             screen.displayMessage(".");
  32.            
  33.             // receive deposit envelope
  34.             boolean envelopeReceived = depositSlot.isEnvelopeReceived();
  35.            
  36.             // check whether deposit envelope was received
  37.             if(envelopeReceived){
  38.                 screen.displayMessageLine("\nYour envelope has been received.");
  39.                 screen.displayMessage("NOTE: The money just deposited will not be available until we verify the amount");
  40.                 screen.displayMessage("of any enclosed cash and your checks clear.");
  41.                
  42.                 // credit account to reflect the deposit
  43.                 bankDatabase.credit(getAccountNumber(), amount);
  44.             }   // end if
  45.             else{
  46.                 // deposit envelope not received
  47.                 screen.displayMessageLine("\nYou did not insert an envelope");
  48.                 screen.displayMessageLine("So, the ATM has canceled your transaction.");
  49.             }   // end else
  50.         }   // end if
  51.         else{
  52.             // user canceled instead of entering amount
  53.             screen.displayMessageLine("\nCanceling transaction...");
  54.         }   // end else
  55.     }   // end method execute
  56.    
  57.     // prompt user to enter a deposit amount in cents
  58.     private double promptForDepositAmount(){
  59.         Screen screen = getScreen();    // get references to screen
  60.        
  61.         // display the prompt
  62.         screen.displayMessage("\nPlease enter a deposit amount in CENTS (or 0 to cancel)");
  63.         int input = keypad.getInput();  // receive input of deposit amount
  64.        
  65.         // check whether the user canceled or entered a valid amount
  66.         if(input == CANCELED) return CANCELED;
  67.         else{
  68.             return (double) input / 100;        // return dollar amount
  69.         }   // end else
  70.     }   // end method
  71. }   // end class Deposit

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Account
  2. {
  3.     private int accountNumber;
  4.     private int pin;
  5.     private double availableBalance;
  6.     private double totalBalance;
  7.    
  8.     public Account(int theAccountNumber,int thePin,double theAvailableBalance,double theTotalBalance)
  9.     {
  10.         accountNumber=theAccountNumber;
  11.         pin=thePin;
  12.         availableBalance=theAvailableBalance;
  13.     }
  14.    
  15.     public boolean validatePIN(int userPIN)
  16.     {
  17.         if(userPIN==pin)
  18.             return true;
  19.         else
  20.             return false;
  21.     }
  22.    
  23.     public double getAvailableBalance()
  24.     {
  25.         return availableBalance;
  26.     }
  27.    
  28.     public double getTotalBalance()
  29.     {
  30.         return totalBalance;
  31.     }
  32.    
  33.     public void credit(double amount)
  34.     {
  35.         totalBalance +=amount;
  36.     }
  37.    
  38.     public void debit(double amount)
  39.     {
  40.         availableBalance -=amount;
  41.         totalBalance -=amount;
  42.     }
  43.    
  44.     public int getAccountNumber()
  45.     {
  46.         return accountNumber;
  47.     }
  48. }

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class ATM
  2. {
  3.     private boolean userAuthenticated;  // whether user is authenticated
  4.     private int currentAccountNumber;   // current user's account number
  5.     private Screen screen;  // ATM's screen
  6.     private Keypad keypad;  // ATM's keypad
  7.     private CashDispenser cashDispenser;    // ATM's cash dispenser
  8.     private DepositSlot depositSlot;        // ATM's deposit slot
  9.     private BankDatabase bankDatabase;      // account information database
  10.    
  11.     // constants corresponding to main menu options
  12.     private static final int BALANCE_INQUIRY = 1;
  13.     private static final int WITHDRAWAL = 2;
  14.     private static final int DEPOSIT = 3;
  15.     private static final int EXIT = 4;
  16.    
  17.     // no-argument ATM constructor initializes instance variables
  18.     public ATM(){
  19.         userAuthenticated = false;  // user is not authenticated to start
  20.         currentAccountNumber = 0;   // no current account number to start
  21.         screen = new Screen();      // create screen
  22.         keypad = new Keypad();      // create keypad
  23.         cashDispenser = new CashDispenser();    // create cash dispenser
  24.         depositSlot = new DepositSlot();        // create deposit slot
  25.         bankDatabase = new BankDatabase();      // create account info database
  26.     }   // end no argument ATM constructor
  27.    
  28.     // start ATM
  29.     public void run(){
  30.         // welcome and authenticate user; perform transactions
  31.         while(true){
  32.             // loop while user is not yet authenticated
  33.             while(!userAuthenticated){
  34.                 screen.displayMessageLine("\nWelcome!");
  35.                 authenticateUser();     // authenticate user
  36.             }   // end while
  37.            
  38.             performTransactions();      // user is now authenticated
  39.             userAuthenticated = false;  // reset before next ATM session
  40.             currentAccountNumber = 0;   // reset before next ATM session
  41.             screen.displayMessageLine("\nThank You!\nGoodbye!");
  42.         }   // end while
  43.     }   // end method run
  44.    
  45.     // attempts to authenticate user against database
  46.     private void authenticateUser(){
  47.         screen.displayMessage("\nPlease enter your account number : ");
  48.         int accountNumber = keypad.getInput();  // input account number
  49.         screen.displayMessage("\nEnter your PIN : ");   // prompt for PIN
  50.         int pin = keypad.getInput();    // input PIN
  51.        
  52.         // set userAuthenticated to boolean value returned by database
  53.         userAuthenticated = bankDatabase.authenticateUser(accountNumber, pin);
  54.        
  55.         // check whether authentication succeeded
  56.         if(userAuthenticated){
  57.             currentAccountNumber = accountNumber;   // save user's account
  58.         }   // end if
  59.         else{
  60.             screen.displayMessageLine("Invalid Account Number or PIN.");
  61.             screen.displayMessageLine("Please Try Again.");
  62.         }
  63.     }   // end method authenticateUser
  64.    
  65.     // display the main menu and perform transactions
  66.     private void performTransactions(){
  67.         // local variable to store transaction currently being processed
  68.         Transaction currentTransaction = null;
  69.         boolean userExited = false;     // user has not chosen to exit
  70.        
  71.         // loop while user has not chosen option to exit system
  72.         while(!userExited){
  73.             // show main menu and get user selection
  74.             int mainMenuSelection = displayMainMenu();
  75.            
  76.             // decide how to proceed based on user's menu selection
  77.             switch(mainMenuSelection){
  78.                 // user choose to perform one of three transaction types
  79.                 case BALANCE_INQUIRY :
  80.                 case WITHDRAWAL :
  81.                 case DEPOSIT :
  82.                
  83.                     // initialize as new object choosen type
  84.                     currentTransaction = createTransaction(mainMenuSelection);
  85.                    
  86.                     currentTransaction.execute();   // execute transaction
  87.                     break;
  88.                
  89.                 case EXIT :
  90.                 // user choose to terminate session
  91.                     screen.displayMessageLine("\nExiting the system...");
  92.                     userExited = true;      // this ATM session should end
  93.                     break;
  94.                    
  95.                 default :
  96.                 // user did not enter an integer between 1-4
  97.                     screen.displayMessageLine("\nYou did not enter a valid selection.");
  98.                     screen.displayMessageLine("Please try again.");
  99.                     break;
  100.             }   // end switch
  101.         }   // end while
  102.     }   // end method performTransactions
  103.    
  104.     // display the main menu and return an input selection
  105.     private int displayMainMenu(){
  106.         screen.displayMessageLine("\nMain Menu :");
  107.         screen.displayMessageLine("1 - View my balance");
  108.         screen.displayMessageLine("2 - Withdraw cash");
  109.         screen.displayMessageLine("3 - Deposit funds");
  110.         screen.displayMessageLine("4 - Exit\n");
  111.         screen.displayMessage("Enter a choice : ");
  112.         return keypad.getInput();   // return user's selection
  113.     }   // end method of displayMainMenu
  114.    
  115.     // return object of specified Transaction subclass
  116.     private Transaction createTransaction(int type){
  117.         Transaction temp = null;        // temporary Transaction variable
  118.        
  119.         // determine which type of Transaction to create
  120.         switch(type){
  121.             case BALANCE_INQUIRY :
  122.             // create new BalanceInquiry transaction
  123.                 temp = new BalanceInquiry(currentAccountNumber, screen, bankDatabase);
  124.                 break;
  125.                
  126.             case WITHDRAWAL :
  127.             // create new Withdrawal transaction
  128.                 temp = new Withdrawal(currentAccountNumber, screen, bankDatabase, keypad, cashDispenser);
  129.                 break;
  130.                
  131.             case DEPOSIT :
  132.             // create new Deposit transaction
  133.                 temp = new Deposit(currentAccountNumber, screen, bankDatabase, keypad, depositSlot);
  134.                 break;
  135.         }   // end switch
  136.        
  137.         return temp;    // return newly created object
  138.     }   // end method createTransaction
  139. }   // end class ATM

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class ATMCaseStudy
  2. {
  3.     // main method creates and runs the ATM
  4.     public static void main(String[] args){
  5.         ATM theATM = new ATM();
  6.         theATM.run();
  7.     }   // end main
  8. }
output

Komentar

Postingan Populer