GSculerlor

ATM.java

Oct 26th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.19 KB | None | 0 0
  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.      {
  20.          userAuthenticated = false; // user is not authenticated to start
  21.          currentAccountNumber = 0; // no current account number to start
  22.          screen = new Screen(); // create screen
  23.          keypad = new Keypad(); // create keypad
  24.          cashDispenser = new CashDispenser(); // create cash dispenser
  25.          depositSlot = new DepositSlot(); // create deposit slot
  26.          bankDatabase = new BankDatabase(); // create acct info database
  27.      } // end no-argument ATM constructor
  28.    
  29.      // start ATM
  30.      public void run()
  31.      {
  32.          // welcome and authenticate user; perform transactions
  33.          while ( true )
  34.          {
  35.              // loop while user is not yet authenticated
  36.              while ( !userAuthenticated )
  37.              {
  38.                 screen.displayMessageLine( "\nWelcome!" );
  39.                 authenticateUser(); // authenticate user
  40.              } // end while        
  41.              performTransactions(); // user is now authenticated
  42.              userAuthenticated = false; // reset before next ATM session
  43.              currentAccountNumber = 0; // reset before next ATM session
  44.              screen.displayMessageLine( "\nThank you! Goodbye!" );
  45.          } // end while
  46.      } // end method run
  47.    
  48.      // attempts to authenticate user against database
  49.      private void authenticateUser()
  50.      {
  51.          screen.displayMessage( "\nPlease enter your account number: " );
  52.          int accountNumber = keypad.getInput(); // input account number
  53.          screen.displayMessage( "\nEnter your PIN: " ); // prompt for PIN
  54.          int pin = keypad.getInput(); // input PIN
  55.        
  56.          // set userAuthenticated to boolean value returned by database
  57.          userAuthenticated =
  58.          bankDatabase.authenticateUser( accountNumber, pin );
  59.        
  60.          // check whether authentication succeeded
  61.          if ( userAuthenticated )
  62.          {
  63.              currentAccountNumber = accountNumber; // save user's account #
  64.          } // end if
  65.          else
  66.          screen.displayMessageLine("Invalid account number or PIN. Please try again.");
  67.      } // end method authenticateUser
  68.    
  69.      // display the main menu and perform transactions
  70.      private void performTransactions()
  71.      {
  72.          // local variable to store transaction currently being processed
  73.          Transaction currentTransaction = null;
  74.          boolean userExited = false; // user has not chosen to exit
  75.        
  76.          // loop while user has not chosen option to exit system
  77.          while ( !userExited )
  78.          {
  79.              // show main menu and get user selection
  80.              int mainMenuSelection = displayMainMenu();
  81.            
  82.              // decide how to proceed based on user's menu selection
  83.              switch ( mainMenuSelection )
  84.              {
  85.                  // user chose to perform one of three transaction types
  86.                  case BALANCE_INQUIRY:
  87.                  case WITHDRAWAL:
  88.                  case DEPOSIT:
  89.                
  90.                  // initialize as new object of chosen type
  91.                  currentTransaction =
  92.                  createTransaction( mainMenuSelection );
  93.                
  94.                  currentTransaction.execute(); // execute transaction
  95.                  break;
  96.                  
  97.                  case EXIT: // user chose to terminate session
  98.                  screen.displayMessageLine( "\nExiting the system..." );
  99.                  userExited = true; // this ATM session should end
  100.                  break;
  101.                  
  102.                  default: // user did not enter an integer from 1-4
  103.                  screen.displayMessageLine("\nYou did not enter a valid selection. Try again.");
  104.                  break;
  105.              } // end switch
  106.          } // end while
  107.      } // end method performTransactions
  108.    
  109.      // display the main menu and return an input selection
  110.      private int displayMainMenu()
  111.      {
  112.          screen.displayMessageLine( "\nMain menu:" );
  113.          screen.displayMessageLine( "1 - View my balance" );
  114.          screen.displayMessageLine( "2 - Withdraw cash" );
  115.          screen.displayMessageLine( "3 - Deposit funds" );
  116.          screen.displayMessageLine( "4 - Exit\n" );
  117.          screen.displayMessage( "Enter a choice: " );
  118.          return keypad.getInput(); // return user's selection
  119.      } // end method displayMainMenu
  120.    
  121.      // return object of specified Transaction subclass
  122.      private Transaction createTransaction( int type )
  123.      {
  124.          Transaction temp = null; // temporary Transaction variable
  125.        
  126.          // determine which type of Transaction to create
  127.          switch ( type )
  128.          {
  129.              case BALANCE_INQUIRY: // create new BalanceInquiry transaction
  130.              temp = new BalanceInquiry(
  131.              currentAccountNumber, screen, bankDatabase );
  132.              break;
  133.              
  134.              case WITHDRAWAL: // create new Withdrawal transaction
  135.              temp = new Withdrawal( currentAccountNumber, screen,
  136.              bankDatabase, keypad, cashDispenser );
  137.              break;
  138.              
  139.              case DEPOSIT: // create new Deposit transaction
  140.              temp = new Deposit( currentAccountNumber, screen,
  141.              bankDatabase, keypad, depositSlot );
  142.              break;
  143.          } // end switch
  144.        
  145.          return temp; // return the newly created object
  146.      } // end method createTransaction
  147. } // end class ATM
Add Comment
Please, Sign In to add comment