-
Bank Account Project
I am designing a bank account for a final project and I am having some troubles with my math and thought maybe someone out there could help me out. I want it to update the balance and previous balance every time the user clicks the calculate button. As of right now it will only calculate one time. Here is my code (sorry it is a little long).
Code:
package bankaccount;
/**
* Final Project
* Validation Assignment
*
* @author ###########
* @created March 14, 2011
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.print.*;
public class BankAccount
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
JFrame frame = new BankFrame();
frame.setVisible(true);
}
}
class BankFrame extends JFrame
{
public BankFrame()
{
setTitle("Bank of Jarred");
centerWindow(this);
setSize(250, 250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new BankPanel();
this.add(panel);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
}
}
class BankPanel extends JPanel implements ActionListener
{
private JTextField nameTextField,
amountTextField,
balanceTextField,
previousBalanceTextField;
private JLabel nameLabel,
amountLabel,
previousBalanceLabel,
balanceLabel;
private JRadioButton depositRadioButton,
withdrawRadioButton;
private JButton calculateButton,
printButton,
exitButton;
private JComboBox nameComboBox;
public BankPanel()
{
Border loweredBorder
= BorderFactory.createBevelBorder(BevelBorder.LOWERED);
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// name label
nameLabel = new JLabel("Name:");
displayPanel.add(nameLabel);
// name combo box
String[] names = { "Tom", "Jason", "Mary", "Tyler", "Sue", "Jan",
"Michael", "Pam", "Stephanie", "Alex", "Hector", "Rose" };
// sort the name array in alphabetical order
Arrays.sort(names);
String prototypeValue="XXXXXXXXXXXXXXX";
nameComboBox = new JComboBox(names);
nameComboBox.addActionListener(this);
nameComboBox.setPrototypeDisplayValue(prototypeValue);
displayPanel.add(nameComboBox);
// amount label
amountLabel = new JLabel("Amount:");
displayPanel.add(amountLabel);
// amount text field
amountTextField = new JTextField(12);
displayPanel.add(amountTextField);
// radio button panel
JPanel radioPanel = new JPanel();
ButtonGroup typeGroup = new ButtonGroup();
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radioPanel.setBorder(
BorderFactory.createTitledBorder(loweredBorder,"Type:"));
// deposit radio button
depositRadioButton = new JRadioButton("Deposit", true);
depositRadioButton.addActionListener(this);
typeGroup.add(depositRadioButton);
radioPanel.add(depositRadioButton);
// withdraw radio button
withdrawRadioButton = new JRadioButton("Withdraw");
withdrawRadioButton.addActionListener(this);
typeGroup.add(withdrawRadioButton);
radioPanel.add(withdrawRadioButton);
// Previous Balance label
previousBalanceLabel = new JLabel("Balance:");
displayPanel.add(previousBalanceLabel);
//Previous Balance text field
previousBalanceTextField = new JTextField(12);
previousBalanceTextField.setEditable(false);
previousBalanceTextField.setFocusable(false);
displayPanel.add(previousBalanceTextField);
// Balance label
balanceLabel = new JLabel("New Balance:");
displayPanel.add(balanceLabel);
// Balance text field
balanceTextField = new JTextField(12);
balanceTextField.setEditable(false);
balanceTextField.setFocusable(false);
displayPanel.add(balanceTextField);
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
buttonPanel.add(calculateButton);
//print button
printButton = new JButton("Print");
printButton.addActionListener(this);
buttonPanel.add(printButton);
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(radioPanel, BorderLayout.NORTH);
this.add(buttonPanel, BorderLayout.SOUTH);
}
// check to see if amount is empty
private boolean validateFeed()
{
boolean valid = true;
if (this.amountTextField.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(null, "Amount must not be empty.", "Error", JOptionPane.ERROR_MESSAGE);
valid = false;
}
if (this.previousBalanceTextField.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(null, "Amount must not be empty.", "Error", JOptionPane.ERROR_MESSAGE);
valid = false;
}
return valid;
}
public void actionPerformed(ActionEvent e)
{
// Adds balances depending on the person selected
int selected = nameComboBox.getSelectedIndex();
// format to currency
// setting the previous balanced based on customer
if(selected==0)
{
previousBalanceTextField.setText("420");
}
if(selected== 1)
{
previousBalanceTextField.setText("4000");
}
if(selected==2)
{
previousBalanceTextField.setText("4500");
}
if(selected==3)
{
previousBalanceTextField.setText("100");
}
if(selected==4)
{
previousBalanceTextField.setText("10000");
}
if(selected==5)
{
previousBalanceTextField.setText("3556");
}
if(selected==6)
{
previousBalanceTextField.setText("56");
}
if(selected==7)
{
previousBalanceTextField.setText("1");
}
if(selected==8)
{
previousBalanceTextField.setText("560");
}
if(selected==9)
{
previousBalanceTextField.setText("123");
}
if(selected==10)
{
previousBalanceTextField.setText("0");
}
if(selected==11)
{
previousBalanceTextField.setText("9");
}
//preforms action when one of the 3 buttons are checked
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculateButton)
{
// validate feed checks to see if amount is empty
if (validateFeed())
{
//try and catch clock to catch non numeric data
try
{
//reading from new balance and amount text fields
double balance = Double.parseDouble(previousBalanceTextField.getText());
double newBalance;
double amount = Double.parseDouble(amountTextField.getText());
// format to currency
NumberFormat currency = NumberFormat.getCurrencyInstance();
if (depositRadioButton.isSelected())
{
newBalance = balance + amount;
balanceTextField.setText(currency.format(newBalance));
}
if (withdrawRadioButton.isSelected())
{
newBalance = balance - amount;
if (newBalance < 0)
{
JOptionPane.showMessageDialog(null, "You are about to overdraft! Canceling withdraw.", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
balanceTextField.setText(currency.format(newBalance));
}
}
}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Non Numeric Data.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
else if (source == printButton)
{
PrintUtilities.printComponent(this);
}
}
}