I have a programming excersize that I need help with, specifically P12.8 which comes from Cay horstmann's 2nd Edition Big Java Book.
"Write a graphical front end for a bank account class. Supply text fields and buttons for depositing and withdrawing money, and for displaying the current balance in a label."
If someone could help me out here, it would be appreciated.
THANKS,
Dan S
Here is my bank account class:
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
private double balance;
}
Here are some other classes to show you my ability and style:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
This program displays the growth of an investment.
*/
public class InvestmentViewer1
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
// The button to trigger the calculation
JButton button = new JButton("Add Interest");
// The application adds interest to this bank account
final BankAccount account = new BankAccount(INITIAL_BALANCE);
// The label for displaying the results
final JLabel label = new JLabel(
"balance=" + account.getBalance());
// The panel that holds the user interface components
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);
class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double interest = account.getBalance()
* INTEREST_RATE / 100;
account.deposit(interest);
label.setText(
"balance=" + account.getBalance());
}
}
ActionListener listener = new AddInterestListener();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 100;
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
An action listener that prints a message.
*/
public class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
This program demonstrates how to install an action listener.
*/
public class ButtonTester
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;
}
THANKS


