Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ATM.java
- //represents an ATM
- public class ATM
- {
- private boolean userAuthenticated;
- private int currentAccountNumber;
- private Screen screen;
- private Keypad keypad;
- private CashDispenser cashDispenser;
- private DepositSlot depositSlot;
- private BankDatabase bankDatabase;
- //constant coresponding to main menu options
- private static final int BALANCE_INQUIRY=1;
- private static final int WITHDRAWAL=2;
- private static final int DEPOSIT=3;
- private static final int EXIT=4;
- //no argument ATM constructor intitializes instancevariables
- public ATM()
- {
- userAuthenticated = false;
- currentAccountNumber = 0;
- screen = new Screen();
- keypad = new Keypad();
- cashDispenser = new CashDispenser();
- depositSlot = new DepositSlot();
- bankDatabase = new BankDatabase();
- }// end of no argument construct
- //start ATM
- public void run()
- {
- // welcome authenticated user
- while(true)
- {
- //loop while user is not yet authenthicated
- while(!userAuthenticated)
- {
- screen.displayMessageLine("\nwelcome!!");
- authenticateUser();
- }//end while
- performTransactions();
- userAuthenticated = false;
- currentAccountNumber = 0;
- screen.displayMessageLine("\nThank you Goodbye!");
- }//endwhile
- }//endmethodrun
- //attemp to authentiate against database
- private void authenticateUser()
- {
- screen.displayMessage("\n please enter your account number: ");
- int accountNumber = keypad.getInput();
- screen.displayMessage( "\nEnter your PIN: " );
- int pin = keypad.getInput();
- // set userAuthenticated to boolean value returned by database
- userAuthenticated =
- bankDatabase.authenticateUser(accountNumber,pin);
- // check wether authentication succeeded
- if( userAuthenticated )
- {
- currentAccountNumber = accountNumber;
- }//end if
- else
- screen.displayMessageLine(
- "Invalid account number, or PIN,Please try again");
- }
- //display Main menu and perform transaction;
- private void performTransactions()
- {
- //local variable to store transaction;
- Transaction currentTransaction = null;
- boolean userExited = false;
- //loop wile user still not exit
- while(!userExited)
- {
- //show main menu anfd get user selection
- int mainMenuSelection = displayMainMenu();
- //decidevhow to proceed based on user selection
- switch ( mainMenuSelection )
- {
- //user choose one transaction type
- case BALANCE_INQUIRY:
- case WITHDRAWAL:
- case DEPOSIT:
- //initialize as new object of chosen type;
- currentTransaction =
- createTransaction( mainMenuSelection );
- currentTransaction.execute(); //execute transaction
- break;
- case EXIT:// user choose exit
- screen.displayMessageLine("\nExiting the system....");
- userExited = true;
- break;
- default: // user input integer other than 1-4
- screen.displayMessageLine(
- "\nYou did not enter a valid selection, try again");
- break;
- }//end switch
- }//end while
- }//end method performTransaction
- //dislay main menu and return input selection
- private int displayMainMenu()
- {
- screen.displayMessageLine( "\nMain menu:" );
- screen.displayMessageLine( "1 - View my balance" );
- screen.displayMessageLine( "2 - Withdraw cash" );
- screen.displayMessageLine( "3 - Deposit funds" );
- screen.displayMessageLine( "4 - Exit\n" );
- screen.displayMessage( "Enter a choice: " );
- return keypad.getInput(); // return user's selection
- }//end method displayMainMenu
- //return object of specified transaction subcalss
- private Transaction createTransaction( int type )
- {
- Transaction temp = null;
- //determine wich type to create
- switch ( type )
- {
- case BALANCE_INQUIRY:
- temp = new BalanceInquiry(
- currentAccountNumber, screen, bankDatabase);
- break;
- case WITHDRAWAL: // create new Withdrawal transaction
- temp = new Withdrawal( currentAccountNumber, screen,
- bankDatabase, keypad, cashDispenser );
- break;
- case DEPOSIT: // create new Deposit transaction
- temp = new Deposit( currentAccountNumber, screen,
- bankDatabase, keypad, depositSlot );
- break;
- } // end switch
- return temp;// return newly created object
- }//end method createTransaction
- }//end ATM
Add Comment
Please, Sign In to add comment