package ATM;
// ATM.java
import javax.swing.*;
public class ATM
{
private boolean userAuthenticated; // user authentication
private int currentAccountNumber; // current account number
private Screen screen; // JOptionPane (pop-up(s))
private CashDispenser cashDispenser; // virtual cash dispenser
private DepositSlot depositSlot; // virtual deposit slot
private BankDatabase bankDatabase; // account database
private LoginScreen loginScreen; // JFrame for login
private MenuScreen menuScreen; // JFrame for transaction selection
private WithdrawalScreen withdrawalScreen;
// constants corresponding 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;
public int mainMenuSelection = 0;
Transaction currentTransaction = null;
// no-argument ATM constructor initializes instance variables
public ATM()
{
userAuthenticated = false; // user is not authenticated to start
currentAccountNumber = 0; // no current account number to start
screen = new Screen(); // create screen
cashDispenser = new CashDispenser(); // create cash dispenser
depositSlot = new DepositSlot(); // create deposit slot
bankDatabase = new BankDatabase(); // create acct info database
loginScreen = new LoginScreen();
menuScreen = new MenuScreen();
withdrawalScreen = new WithdrawalScreen();
} // end constructor
public void run()
{
while ( true )
{
while ( !userAuthenticated ) // loop while user is not yet authenticated
{
login(); //start login screen
currentAccountNumber = loginScreen.getCurrentAccountNumber();
userAuthenticated = loginScreen.getAuthentication();
} // end while
currentAccountNumber = loginScreen.getCurrentAccountNumber();
performTransactions(); // user authenticated
userAuthenticated = false; // reset ATM
currentAccountNumber = 0; // reset ATM
screen.displayMessageLine( "\nThank you! Goodbye!" );
} // end while
} // end method run
private void login()//login screen constructor
{
loginScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
loginScreen.setSize( 275, 130 ); // set frame size
loginScreen.setVisible( true ); // display frame
}//end login()
// display the main menu and perform transactions
private void performTransactions()
{
boolean userExited = false;
while ( !userExited )
{
mainMenuSelection = displayMainMenu();
// decide how to proceed based on user's menu selection
switch ( mainMenuSelection )
{
// user chose to perform one of three transaction types
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 chose to terminate session
screen.displayMessageLine( "\nExiting the system..." );
userExited = true; // this ATM session should end
break;
} // end switch
} // end while
} // end performTransactions
// display the main menu and return an input selection
private int displayMainMenu() //constructs menu screen and return user's choice
{
menuScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
menuScreen.setSize( 275, 130 ); // set frame size
menuScreen.setVisible( true ); // display frame
return menuScreen.displayMainMenuValue; // return user's choice
} // end method displayMainMenu
private Transaction createTransaction( int type )
{
Transaction temp = null; // temporary Transaction variable
// determine which type of Transaction
switch ( type )
{
case BALANCE_INQUIRY: // create BalanceInquiry transaction
temp = new BalanceInquiry(
currentAccountNumber, screen, bankDatabase );
currentTransaction = null;
mainMenuSelection = 0;
break;
case WITHDRAWAL: // create Withdrawal transaction
temp = new Withdrawal( currentAccountNumber, screen,
bankDatabase, cashDispenser );
currentTransaction = null;
mainMenuSelection = 0;
break;
case DEPOSIT: // create Deposit transaction
temp = new Deposit( currentAccountNumber, screen,
bankDatabase, depositSlot );
currentTransaction = null;
mainMenuSelection = 0;
break;
} // end switch
return temp; // return the newly created object
} // end createTransaction
} // end class ATM
The code above is the code for the main.
The problem that I encounter is that once I start a transaction, it doesn't end.
For example, when I balance inquiry, it'll keep repeating showing me the balance report no matter how many times I click ok.
screen.displayMessageLine( "\nBalance Information: " + "\n\nAvailable balance: $ " + availableBalance + "\nTotal Balance: $"+ totalBalance );
will pop up non-stop.
Can anyone tell me what's wrong with the code? I suspect that currentTransaction doesn't reset itself after each transaction and that is why there is a continuous loop.