Create Java program using GUI Javafx that simulates a bank ATM. Must compile and
ID: 3850350 • Letter: C
Question
Create Java program using GUI Javafx that simulates a bank ATM. Must compile and run using eclipse. ***You MUST put the required comment information at the beginning of every class you write and use appropriate comments throughout the code to explain general functionality. -Bank ATM should open in a single window that is exactly ¾ of the screen width and height. --It should contain a menu bar at the top of the window that allows access to various ATM system functions. --The program should only ever exit using the menu bar. -The ATM must ask for an ‘ATM Card’ (a number as input) and an ‘ATM Pin’ (a number as input) that corresponds to that ‘ATM Card’ --The ATM must assure proper access (login) before proceeding to the corresponding account information. --If a customer is not already in the ATM system they should be able to create an account with a new ‘ATM Card’ and ‘ATM Pin’ -The program must display at least 3 basic functions (withdrawal, deposit, get balance) once a customer is logged-in. --A function can be selected by either clicking on the function specific button or accessing the function from the menu items. --When a function is selected, the program should look-up the account information from an account history file and update the file based on the function performed. -Overall, there is no limit to the number of transactions that can take place during one session of use. -There are two withdrawal limits for any account --No more than $1000.00 --No more than 70% of the account balance -At any time the customer can exit the system and be assured of a successful log-out of the system. -JavaFX Interface Requirements --A single JavaFX Stage window with menu bar functionality --The main stage should only close by choosing ‘file->close’ --The Menu bar should include at least: ---File, Options ,Help ---Buttons that allow functionality for: ----Login/Logout ----Withdrawal (‘Quick’, or specified amount) Deposit ----Get Balance ----Create New Account ----Change Pin ----Transfer Funds ------Requires two ‘ATM Cards’ and ‘ATM Pin ------Print Receipt (Print to File not a printer) -------The contents of the receipt must include but not limited to: ---------The date ---------The ATM card(s) # that preform the action ---------Only the last 3 digits are visible (XXXXXXXX123) ---------The action (withdrawdeposit ransfer) ---------The current balance -------View Transaction History --All of the above functions should be available from user interface buttons as well (not all on the screen at once) --A status bar to show the current transactional history for the current session --A label box above the status bar to show the current account number and current balance that updates as transactions occur --The main GUI should not be re-sizeable --All items should contain a ‘text-tooltip’ that describes the item. -Operational Requirements --There must be an entire Bank history file to track all accounts and number of transactions (Bank.txt) --The entire bank file should be formatted in the following way: ---ATM Card#<<, >>ATM Pin<<,>>number of all transactions --There must be an account history file for each created account (acct###.txt) --The individual account files should be formatted in the following way: ---ATM Card#<>ATM Pin#<> Balance<>Transaction --All account data should be ‘looked-up’ from the corresponding account file ---This should be an output file named ‘receipt-MM-DD-YY.txt’ ---It should be saved to the folder where the program is running. --No ‘JAVA threads’ are necessary --Bank ATM must contain at least one exception handler. -Make appropriate use of classes and methods to simplify the design and support the abstraction of the various data objects and operations. Do not create many classes in one file. Try to have a separate file for each class. -You MUST put the required comment information at the beginning of every class you write and use appropriate comments throughout the code to explain general functionality.
Explanation / Answer
The code is given below :
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
A simulation of an automatic teller machine
*/
public class BankATM
{ public static void main(String[] args)
{ ATM frame = new ATM();
frame.setTitle("First National Bank of Java");
frame.show();
}
}
class ATM extends JFrame
{ /**
Constructs the user interface of the ATM application.
*/
public ATM()
{ final int FRAME_WIDTH = 500;
final int FRAME_HEIGHT = 200;
setSize(FRAME_WIDTH, FRAME_HEIGHT);
addWindowListener(new WindowCloser());
// initialize bank and customers
theBank = new Bank();
try
{ theBank.readCustomers("customers.txt");
}
catch(IOException e)
{ JOptionPane.showMessageDialog
(null, "Error opening accounts file.");
}
// construct components
pad = new KeyPad();
display = new JTextArea(4, 20);
aButton = new JButton(" A ");
aButton.addActionListener(new AButtonListener());
bButton = new JButton(" B ");
bButton.addActionListener(new BButtonListener());
cButton = new JButton(" C ");
cButton.addActionListener(new CButtonListener());
// add components to content pane
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
buttonPanel.add(aButton);
buttonPanel.add(bButton);
buttonPanel.add(cButton);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(pad);
contentPane.add(display);
contentPane.add(buttonPanel);
setState(START_STATE);
}
/**
Sets the current customer number to the keypad value
and sets state to PIN.
*/
public void setCustomerNumber()
{ customerNumber = (int)pad.getValue();
setState(PIN_STATE);
}
/**
Gets PIN from keypad, finds customer in bank.
If found sets state to ACCOUNT, else to START.
*/
public void selectCustomer()
{ int pin = (int)pad.getValue();
currentCustomer
= theBank.findCustomer(customerNumber, pin);
if (currentCustomer == null)
setState(START_STATE);
else
setState(ACCOUNT_STATE);
}
/**
Sets current account to checking or savings. Sets
state to TRANSACT
@param account one of Customer.CHECKING_ACCOUNT
or Customer.SAVINGS_ACCOUNT
*/
public void selectAccount(int account)
{ currentAccount = currentCustomer.getAccount(account);
setState(TRANSACT_STATE);
}
/**
Withdraws amount typed in keypad from current account.
Sets state to ACCOUNT.
*/
public void withdraw()
{ currentAccount.withdraw(pad.getValue());
setState(ACCOUNT_STATE);
}
/**
Deposits amount typed in keypad to current account.
Sets state to ACCOUNT.
*/
public void deposit()
{ currentAccount.deposit(pad.getValue());
setState(ACCOUNT_STATE);
}
/**
Sets state and updates display message.
@param state the next state
*/
public void setState(int newState)
{ state = newState;
pad.clear();
if (state == START_STATE)
display.setText("Enter customer number A = OK");
else if (state == PIN_STATE)
display.setText("Enter PIN A = OK");
else if (state == ACCOUNT_STATE)
display.setText("Select Account "
+ "A = Checking B = Savings C = Exit");
else if (state == TRANSACT_STATE)
display.setText("Balance = "
+ currentAccount.getBalance()
+ " Enter amount and select transaction "
+ "A = Withdraw B = Deposit C = Cancel");
}
private int state;
private int customerNumber;
private Customer currentCustomer;
private BankAccount currentAccount;
private Bank theBank;
private JButton aButton;
private JButton bButton;
private JButton cButton;
private KeyPad pad;
private JTextArea display;
private static final int START_STATE = 1;
private static final int PIN_STATE = 2;
private static final int ACCOUNT_STATE = 3;
private static final int TRANSACT_STATE = 4;
private class AButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ if (state == START_STATE)
setCustomerNumber();
else if (state == PIN_STATE)
selectCustomer();
else if (state == ACCOUNT_STATE)
selectAccount(Customer.CHECKING_ACCOUNT);
else if (state == TRANSACT_STATE)
withdraw();
}
}
private class BButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ if (state == ACCOUNT_STATE)
selectAccount(Customer.SAVINGS_ACCOUNT);
else if (state == TRANSACT_STATE)
deposit();
}
}
private class CButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ if (state == ACCOUNT_STATE)
setState(START_STATE);
else if (state == TRANSACT_STATE)
setState(ACCOUNT_STATE);
}
}
private class WindowCloser extends WindowAdapter
{ public void windowClosing(WindowEvent event)
{ System.exit(0);
}
}
}