Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package atm; /* File: ATM.java * Author: Andress Quinton * Date: Feb 2, 2017 * Purpose: CMIS 242 Project 2 * - This will create

image text in transcribed
package atm; /* File: ATM.java * Author: Andress Quinton * Date: Feb 2, 2017 * Purpose: CMIS 242 Project 2 * - This will create a GUI that will have 4 buttons,2 radio buttons, * and a text box * - The user will be able to enter an amount in the text box * and select ether checking or saving account * -- If the user enters a non-numerical input a message will display * Informing the user to enter a valid amount * - After this the user, can select withdraw, transfer, or deposit * -- If Deposit, the amount will be added to the selected account * -- If Withdraw, the amount will be subtracted from the account * --- Withdraws can only be an interval of 20 * ---- If you try to withdraw anything other number that is not in an * interval of 20 a message will appear Informing the user * --- If you do not have enough funds a message will appear * informing the user, they do not have enough funds * --- The program will count the number of times withdrawal and will display * a message when it reaches 4 informing the user they used their last free * withdraw * ---- The program will display a message at 5 withdraws and deduct $1.50 * from the account and will continue for any more withdraws * ----- If withdraw and charge makes account balance below 0 an * insufficient funds message appears and stops the withdraw * (ex: 5th withdraw is for $20 and you have $20 left the charge is added * to the withdraw causing you to have -$1.50 the program sees this and denies * the withdraw) * -- If Transferred, the selected account will receive * the amount from the other account * - In the output log, it will keep track of everything the program * does for bank records, this is to make sure the program is working properly */ import import import import import javax.swing.*; java.awt.*; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.text.DecimalFormat; public class ATM extends JFrame { // Data fields for frame design static final int WINDOWWIDTH = 350, WINDOWHEIGHT = 200, TEXTWIDTH = 225, TEXTHEIGHT = 25; // Data private private private private private private private private private fields for the construction of the JFrame elements JButton withdrawButton = new JButton("Withdraw"); JButton depositButton = new JButton("Deposit"); JButton transferToButton = new JButton("Transfer To"); JButton balanceButton = new JButton("Balance"); JRadioButton checkingRadio = new JRadioButton("Checking"); JRadioButton savingsRadio = new JRadioButton("Savings"); JTextField entry = new JTextField(""); ButtonGroup radios = new ButtonGroup(); JOptionPane frame = new JOptionPane(); // Two objects for a checking and savings account private static Account checking = new Account().new Checking(); private static Account savings = new Account().new Savings(); // Decimal format to show dollars private static DecimalFormat df = new DecimalFormat("$0.00"); // Method that will create a checking and savings account // based on the starting values public static void makeAccounts(double checkingStartingBalance, double savingsStartingBalance) { } checking.setBalance(checkingStartingBalance); savings.setBalance(savingsStartingBalance); // Error checking handles invalid input // such as blanks, letters, and negative numbers public void errorValidNumber() { JOptionPane.showMessageDialog(frame, "Please enter a valid amount." + "\ You can only withdrawal in $20 increments."); } // Action listener for the Withdraw button class WithdrawButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { try { // check for negative number and increment of 20 if (getEntryValue() > 0 && getEntryValue() % 20 == 0) { // Check radio button selection if (Account.count 0) { // Then checks for radio selection if (checkingRadio.isSelected()) { checking.deposit(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + " has been Deposited into Checking."); System.out.println(df.format(getEntryValue()) +" Deposited to Checking"); } else if (savingsRadio.isSelected()) { savings.deposit(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + " has been Deposited into Savings."); System.out.println(df.format(getEntryValue()) +" Deposited to Savings"); } clearEntryValue(); } else errorValidNumber(); clearEntryValue(); } } // Action listener for the Transfer To button class TransferToButtonListener implements ActionListener { } @Override public void actionPerformed(ActionEvent e) { try { // First checks for positive number if (getEntryValue() > 0) { // Then checks for radio selection if (checkingRadio.isSelected()) { // Separate methods for transferFrom and transferTo savings.transferFrom(getEntryValue()); checking.transferTo(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + " has been Transferred from Savings" + " to Checking."); System.out.println(df.format(getEntryValue()) +" Transfer from Savings to Checking"); } else if (savingsRadio.isSelected()) { checking.transferFrom(getEntryValue()); savings.transferTo(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + " has been Transferred from Checking" + " to Savings."); System.out.println(df.format(getEntryValue()) +" Transfer from Checking to Savings"); } clearEntryValue(); } else errorValidNumber(); clearEntryValue(); } catch (InsufficientFunds insufficientFunds ) { System.out.println("Insufficient funds in Account"); } } // Action listener for the Transfer To button class BalanceButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // Only checks for radio selection if (checkingRadio.isSelected()) { } JOptionPane.showMessageDialog(frame, "Your checking account balance is: \ " + df.format(checking.getBalance())); System.out.println("Displayed checking accound balance " + df.format(checking.getBalance())); } else if (savingsRadio.isSelected()) { JOptionPane.showMessageDialog(frame, "Your savings account balance is: \ " + df.format(savings.getBalance())); System.out.println("Displayed saving accound balance " + df.format(savings.getBalance())); } else errorValidNumber(); clearEntryValue(); } /** * ATM object constructor. This constructor creates the panels, * and set the layout boundaries, and then add them to the panel. * Finally it will create the checking and savings accounts and * apply action listeners to each of the buttons so that the user is able * to make their selection. * * @param checkingStartingBalance * @param savingsStartingBalance */ public ATM(double checkingStartingBalance, double savingsStartingBalance) { super("ATM Machine"); setLayout(new GridBagLayout()); GridBagConstraints layout = new GridBagConstraints(); setFrame(WINDOWWIDTH, WINDOWHEIGHT); JPanel buttonPanel = new JPanel(); JPanel textEntry = new JPanel(); setResizable(false); layout.gridy = 2; add(buttonPanel); add(textEntry, layout); buttonPanel.setLayout(new GridLayout(3, 2, 10, 10)); textEntry.setLayout(new GridLayout(1, 1)); buttonPanel.add(withdrawButton); buttonPanel.add(depositButton); buttonPanel.add(transferToButton); buttonPanel.add(balanceButton); radios.add(checkingRadio); radios.add(savingsRadio); buttonPanel.add(checkingRadio); buttonPanel.add(savingsRadio); entry.setPreferredSize(new Dimension(TEXTWIDTH, TEXTHEIGHT)); checkingRadio.setSelected(true); textEntry.add(entry); // Creates the checking and savings accounts makeAccounts(checkingStartingBalance, savingsStartingBalance); } /** // Action listeners withdrawButton.addActionListener(new WithdrawButtonListener()); depositButton.addActionListener(new DepositButtonListener()); transferToButton.addActionListener(new TransferToButtonListener()); balanceButton.addActionListener(new BalanceButtonListener()); * Methods * @return */ // This method returns the text in the text entry field as a double public double getEntryValue() { try { return Double.parseDouble(entry.getText()); } catch (NumberFormatException e) { System.out.println("Caught in get Entry Value"); clearEntryValue(); return 0; } } // Clears the text entry field public void clearEntryValue() { entry.setText(""); } private void setFrame(int width, int height) { setSize(width, height); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void display() { // makes frame visible, by default it is invisivble setVisible(true); } public static void main(String args) { /* To open a new account you must deposite atleast $50 in the account * Creates a new ATM object that will have $50 * in each account at the start * This is to show the owners has already deposited $50 * to have the accounts opened */ ATM frame = new ATM(50, 50); frame.display(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions