Hello. I am working on a project that creates a GUI for an ATM. I
Hello. I am working on a project that creates a GUI for an ATM. I
wrote the code and it compiles, but it is not using the amounts I set for the checking and savings accounts and I am struggling to make the GUI look like I have it designed in my form. My code and design is below:
/** * File: ATMMachine.java * Author: Lutz,Zechariah * Date: 15 November 2019 * Purpose: ATM Machine program */ package atm; // Import swing and abstract toolkits import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; public class ATMMachine extends JFrame { // Declare fields for frame design static final int WINDOWWIDTH = 600, WINDOWHEIGHT = 400, TEXTWIDTH = 200, TEXTHEIGHT = 20; // Declare fields to construct the JFrame elements private JButton withdrawButton = new JButton(“Withdraw”); private JButton depositButton = new JButton(“Deposit”); private JButton transferButton = new JButton(“Transfer To:”); private JButton balanceButton = new JButton(“Balance”); private JRadioButton checkingRadio = new JRadioButton(“Checking”); private JRadioButton savingsRadio = new JRadioButton(“Savings”); private JTextField entry = new JTextField(“”); private ButtonGroup radios = new ButtonGroup(); private JPanel frame = new JPanel(); // Objects for checking and savings accounts private static Account checking = new Account().new Checking(); private static Account savings = new Account().new Savings(); private static DecimalFormat df = new DecimalFormat(“$0.00″); // Methods to create checking and savings accounts public static void makeAccounts(double checkingBalance, double savingsBalance){ checking.setBalance(checkingBalance); savings.setBalance(savingsBalance); } // Error checking for invalid input public void errorNumber(){ JOptionPane.showMessageDialog(frame,”Error: Please enter valid amount ” + “\nPlease use increments of $20:”); } class withdrawListener implements ActionListener { // Listener to withdraw money @Override public void actionPerformed(ActionEvent e) { try { if (getEntryValue() > 0 && getEntryValue() % 20 == 0) { // Withdrawing money from checking if (checkingRadio.isSelected()) { checking.withdraw(getEntryValue()); JOptionPane.showMessageDialog(frame , df.format(getEntryValue() + ” withdrawn from Checking.”)); // Withdrwing money from savings } else if (savingsRadio.isSelected()) { savings.withdraw(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + ” withdrawn from Savings.”); } // Catching errors // Informing user there are not enough funds in account clearEntryValue(); } else errorNumber(); clearEntryValue(); } catch (InsufficientFunds insufficientFunds) { System.out.println(“Insufficient funds”); } } } // Listener for deposit class depositListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (getEntryValue() > 0) { // Deposit into checking if (checkingRadio.isSelected()) { checking.deposit(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + ” deposited into Checking.”); // Deposit into savings } else if (savingsRadio.isSelected()) { savings.deposit(getEntryValue()); JOptionPane.showMessageDialog(frame, df.format(getEntryValue()) + ” deposited into Savings”); } // Catch errors clearEntryValue(); } else errorNumber(); clearEntryValue(); } } // Listener for tranfering funds class transferListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { try { if (getEntryValue() > 0) { // Transfering funds from savings to checking if (checkingRadio.isSelected()) { savings.transferFrom(getEntryValue()); checking.transferTo(getEntryValue()); JOptionPane.showMessageDialog(frame , df.format(getEntryValue()) + ” transferred from Savings to Checking.”); // Transferring funds from checking to savings } else if (savingsRadio.isSelected()) { checking.transferFrom(getEntryValue()); savings.transferTo(getEntryValue()); JOptionPane.showMessageDialog(frame , df.format(getEntryValue()) + ” transferred from Checking to Savings”); } // Catch errors clearEntryValue(); } else errorNumber(); clearEntryValue(); } catch (InsufficientFunds insufficientFunds) { System.out.println(“Insufficient funds.”); } } } // Listener to display balance class balanceListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // Retrieves balance if (checkingRadio.isSelected()) { JOptionPane.showMessageDialog(frame , “Your checking account balance is: \n” + df.format(checking.getBalance())); } else if (savingsRadio.isSelected()) { JOptionPane.showMessageDialog(frame, “Your savings account balance is: \n” + df.format(savings.getBalance())); // Catches input error }else errorNumber(); clearEntryValue(); } } // Constructor to build JFrame public ATMMachine(double checkingBalance, double savingsBalance) { super(“ATM Machine”); setLayout( new GridLayout()); GridLayout layout = new GridLayout(); setFrame(WINDOWHEIGHT, WINDOWWIDTH); JPanel buttonPanel = new JPanel(); JPanel textEntry = new JPanel(); setResizable(false); 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(transferButton); buttonPanel.add(balanceButton); radios.add(checkingRadio); radios.add(savingsRadio); buttonPanel.add(checkingRadio); buttonPanel.add(savingsRadio); textEntry.setPreferredSize(new Dimension(TEXTWIDTH, TEXTHEIGHT)); checkingRadio.setSelected(true); textEntry.add(entry); // Action listeners withdrawButton.addActionListener(new withdrawListener()); depositButton.addActionListener(new depositListener()); transferButton.addActionListener(new transferListener()); balanceButton.addActionListener(new balanceListener()); } // Retrieves entered value public double getEntryValue() { try { return Double.parseDouble(entry.getText()); // Catches entry errors } catch (NumberFormatException e) { System.out.println(“Caught in entry\n”); clearEntryValue(); return 0; } } // Clears entry public void clearEntryValue() { entry.setText(“”); } // Sets frame size and function private void setFrame(int width, int height) { setSize(width, height); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void display() { setVisible(true); } // Creates ATM object with $500 in checking and $1000 in savings public static void main(String[] args) { ATMMachine frame = new ATMMachine(500, 1000); frame.display(); } }
/** * File: Account.java * Author: Lutz, Zechariah * Date: 15 November 2019 * Purpose: Access Account data */ package atm; import javax.swing.*; public class Account { // Fields for account including charge for excess actions private double balance; private int count; private final double charge = 1.50; public Account() { } // Setters and getters for balance public void setBalance(double balance){ this.balance = balance; } public double getBalance() { return this.balance; } // Classes for checking and savings public class Checking extends Account { public Checking(){ } } public class Savings extends Account { public Savings(){ } } // Withdraw method public void withdraw(double withdrawAmount) throws InsufficientFunds { if(this.balance – withdrawAmount < 0) { throw new InsufficientFunds(); } this.balance = this.balance – withdrawAmount; count++; if(count >= 3){ JOptionPane warning = new JOptionPane(); JOptionPane.showMessageDialog(warning, “You have reached the number of ” + “free withdrawls. A $1.50 service fee will be applied” + ” to your next withdrawl”); } if(count >= 4){ this.balance = this.balance – charge; } } // Deposit method public void deposit(double depositAmount) { this.balance = this.balance + depositAmount; } // Transfer methods public void transferTo(double transferAmount){ this.balance = this.balance + transferAmount; } public void transferFrom(double transferAmount) throws InsufficientFunds{ if(this.balance – transferAmount < 0) { throw new InsufficientFunds(); } this.balance = this.balance – transferAmount; } }
/** * File: InsufficientFunds.java * Author: Lutz, Zechariah * Date: 15 November 2015 * Purpose: Checked exception when user requesting more money than available */ package atm; import javax.swing.*; public class InsufficientFunds extends Exception { // Shows pane that displays Insufficient funds public InsufficientFunds() { JOptionPane frame = new JOptionPane(); JOptionPane.showMessageDialog(frame, “Insufficient Funds”); } InsufficientFunds(String msg) { throw new UnsupportedOperationException(“Not supported. Please try again.”); } }