I have been hard at work revising my code, with the assistance of my peers here, and have come along ways. There are two issues I am facing that I would appreciate assistance on. First is within my MyListener class. Per previous tutorials and examples I looked at used "JButton source = (JButton) e.getSource();" to check which action is being utilized. This was fine when I only used Buttons but now that I have RadioButtons it throws a castClassException. Not sure of a workaround for this. The next issue I face is my InsufficientFunds class, I placed big block quotes at these sections for better visualization. This class is set to throw exceptions when the balance is lower than the amount that is requested to be withdrawn or transferred. I set in place manual triggers for these events as I was unable to setup the class correctly to handle the situation. How would I properly set this class up to handle the exceptions and how would I implement them into the correct events under the Account class (This is where it is suppose to be utilized)? Finally if anyone has further suggestions to my current code I would appreciate it as I am attempting to finalize the project.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JFrame;
class Account {
private double balance;
public Account(double initialBalance) {
balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
} // End class Account
class InsufficientFunds extends Exception {
/*
*
*
* Needs to create InsufficientFUnds() and be implemented in Account class
*
*
*/
} // End class InsufficientFunds
public class ATM extends JFrame {
private final JButton withdrawButton;
private final JButton depositButton;
private final JButton transferButton;
private final JButton balanceButton;
private final JRadioButton checkingRadio;
private final JRadioButton savingsRadio;
private final JTextField valueText;
private static final int FRAME_WIDTH = 325;
private static final int FRAME_HEIGHT = 200;
static Account checkingAccount;
static Account savingsAccount;
public ATM() {
// Sets Frame and Layout
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("ATM Machine");
setResizable(false);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
// Create withdrawButton Button and places is on display
withdrawButton = new JButton("Withdraw");
constraints.insets = new Insets(5, 50, 2, 5);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
add(withdrawButton, constraints);
// Create depositButton Button and places is on display
depositButton = new JButton("Deposit");
constraints.insets = new Insets(5, 5, 2, 50);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 1;
add(depositButton, constraints);
// Create transferButton Button and places is on display
transferButton = new JButton("Transfer To");
constraints.insets = new Insets(1, 50, 2, 5);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 2;
add(transferButton, constraints);
// Create balanceButton Button and places is on display
balanceButton = new JButton("Balance");
constraints.insets = new Insets(1, 5, 2, 50);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 1;
add(balanceButton, constraints);
// Create checkingRadio RadioButton and places is on display
checkingRadio = new JRadioButton("Checking");
constraints.insets = new Insets(1, 50, 5, 5);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 3;
add(checkingRadio, constraints);
// Create savingsRadio RadioButton and places is on display
savingsRadio = new JRadioButton("Savings");
constraints.insets = new Insets(1, 5, 5, 50);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.gridx = 1;
add(savingsRadio, constraints);
// Create valueText TextField and places is on display
valueText = new JTextField(10);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(1, 35, 30, 35);
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.gridy = 4;
add(valueText, constraints);
//Group RadioButtons so that only one selection allowed
ButtonGroup radioButtons = new ButtonGroup();
radioButtons.add(checkingRadio);
radioButtons.add(savingsRadio);
// Group Buttons so that only one selection allowed
ButtonGroup buttons = new ButtonGroup();
buttons.add(withdrawButton);
buttons.add(depositButton);
buttons.add(transferButton);
buttons.add(balanceButton);
// Sets the frame open and close
JFrame frame = new JFrame("ATM Machine");
frame.pack();
frame.getContentPane();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adds ActionListeners to each of the Buttons
withdrawButton.addActionListener(new MyListener());
depositButton.addActionListener(new MyListener());
transferButton.addActionListener(new MyListener());
balanceButton.addActionListener(new MyListener());
checkingRadio.addActionListener(new MyListener());
savingsRadio.addActionListener(new MyListener());
valueText.addActionListener(new MyListener());
}
// Handles Events for the JButtons
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if ("Withdraw".equalsIgnoreCase(source.getText())) {
if (checkingRadio.isSelected()) {
try {
String amountString = valueText.getText();
double withdrawAmount = Double.parseDouble(amountString);
if (withdrawAmount % 20 == 0) {
if (withdrawAmount <= checkingAccount.getBalance()) {
checkingAccount.withdraw(Double.parseDouble(valueText.getText()));
JOptionPane.showMessageDialog(null, "Withdraw Complete");
} else {
JOptionPane.showMessageDialog(null, "Insufficient Funds");
}
} else {
JOptionPane.showMessageDialog(null, "Please Use Increments of $20");
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter a Numerical Value");
}
} // End withdraw/checking
if (savingsRadio.isSelected()) {
try {
String amountString = valueText.getText();
double withdrawAmount = Double.parseDouble(amountString);
if (withdrawAmount % 20 == 0) {
if (withdrawAmount <= savingsAccount.getBalance()) {
savingsAccount.withdraw(Double.parseDouble(valueText.getText()));
JOptionPane.showMessageDialog(null, "Withdraw Complete");
} else {
JOptionPane.showMessageDialog(null, "Insufficient Funds");
}
} else {
JOptionPane.showMessageDialog(null, "Please Use Increments of $20");
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter a Numerical Value");
}
} // End withdraw/savings
valueText.setText("");
} // End Withdraw Events
if ("Deposit".equalsIgnoreCase(source.getText())) {
if (checkingRadio.isSelected()) {
try {
checkingAccount.deposit(Double.parseDouble(valueText.getText()));
JOptionPane.showMessageDialog(null, "Deposit Complete ");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please Enter a Numerical Value");
}
} // End deposit/checking
if (savingsRadio.isSelected()) {
try {
savingsAccount.deposit(Double.parseDouble(valueText.getText()));
JOptionPane.showMessageDialog(null, "Deposit Complete");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please Enter a Numerical Value");
}
} // End deposit/savings
valueText.setText("");
} // End Deposit Events
if ("Transfer To".equalsIgnoreCase(source.getText())) {
if (checkingRadio.isSelected()) {
try {
String amountString = valueText.getText();
double transferAmount = Double.parseDouble(amountString);
if (transferAmount <= savingsAccount.getBalance()) {
savingsAccount.withdraw(transferAmount);
checkingAccount.deposit(transferAmount);
JOptionPane.showMessageDialog(null, "Transfer Complete ");
} else {
JOptionPane.showMessageDialog(null, "Insufficient Funds");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please Enter a Numerical Value");
}
} // End transfer/checking
if (savingsRadio.isSelected()) {
try {
String amountString = valueText.getText();
double transferAmount = Double.parseDouble(amountString);
if (transferAmount <= checkingAccount.getBalance()) {
checkingAccount.withdraw(transferAmount);
savingsAccount.deposit(transferAmount);
JOptionPane.showMessageDialog(null, "Transfer Complete ");
} else {
JOptionPane.showMessageDialog(null, "Insufficient Funds");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please Enter a Numerical Value");
}
} // End transfer/savings
valueText.setText("");
} // End Transfer Events
if ("Balance".equalsIgnoreCase(source.getText())) {
double currentBalance;
DecimalFormat df = new DecimalFormat("###,###,###,###,##0.00");
if (checkingRadio.isSelected()) {
currentBalance = checkingAccount.getBalance();
JOptionPane.showMessageDialog(null, "Current Balance: $" + df.format(currentBalance));
} else if (savingsRadio.isSelected()) {
currentBalance = savingsAccount.getBalance();
JOptionPane.showMessageDialog(null, "Current Balance: $" + df.format(currentBalance));
} else {
JOptionPane.showMessageDialog(null, "Please Select an Account");
}
} // End Balance Events
} // End ActionPerformed
} // End class myListener
public static void main(String[] args) {
ATM atm = new ATM();
// Checking and Savings Objects
checkingAccount = new Account(0);
savingsAccount = new Account(0);
} // End Main
} // End class ATM