      1  import java.awt.event.ActionEvent;

      2  import java.awt.event.ActionListener;

      3  import javax.swing.JButton;

      4  import javax.swing.JFrame;

      5  import javax.swing.JLabel;

      6  import javax.swing.JPanel;

      7  import javax.swing.JTextField;

      8

      9  public class InvestmentFrame extends JFrame

     10  {

     11     private JButton button;

     12     private JLabel label;

     13     private JPanel panel;

     14     private BankAccount account;

     15

     16     private static final int FRAME_WIDTH = 400;

     17     private static final int FRAME_HEIGHT = 100;

     18

     19     private static final double INTEREST_RATE = 10;

     20     private static final double INITIAL_BALANCE = 1000;

     21

     22     public InvestmentFrame()

     23     {

     24        account = new BankAccount(INITIAL_BALANCE);

     25

     26        // Use instance variables for components

     27        label = new JLabel("balance: " + account.getBalance());

     28

     29        // Use helper methods

     30        createButton();

     31        createPanel();

     32

     33        setSize(FRAME_WIDTH, FRAME_HEIGHT);

     34     }

     35

     36     private void createButton()

     37     {

     38        button = new JButton("Add Interest");

     39        ActionListener listener = new AddInterestListener();

     40        button.addActionListener(listener);

     41     }

     42

     43     private void createPanel()

     44     {

     45        panel = new JPanel();

     46        panel.add(button);

     47        panel.add(label);

     48        add(panel);

     49     }

     50

     51     class AddInterestListener implements ActionListener

     52     {

     53        public void actionPerformed(ActionEvent event)

     54        {

     55           double interest = account.getBalance() * INTEREST_RATE / 100;

     56           account.deposit(interest);

     57           label.setText("balance: " + account.getBalance());

     58        }

     59     }

     60  }