Source of VisualBankAccount.java


  1: //VisualBankAccount.java
  2: //Adapted from an example by Tony Sintes.

  4: //As Sintes himself points out, this program is a bad
  5: //example of OOD because it merges the model, the view,
  6: //and the controller into a single class.

  8: //For our purposes it also illustrates a number of "Swing" components.
  9: import javax.swing.JPanel;
 10: import javax.swing.JFrame;
 11: import javax.swing.JLabel;
 12: import javax.swing.JTextField;
 13: import javax.swing.JButton;

 15: import java.awt.BorderLayout;

 17: import java.awt.event.ActionListener;
 18: import java.awt.event.ActionEvent;
 19: import java.awt.event.WindowListener;
 20: import java.awt.event.WindowAdapter;
 21: import java.awt.event.WindowEvent;


 24: public class VisualBankAccount
 25: extends JPanel
 26: implements ActionListener
 27: {    
 28:     public static void main(String[] args)
 29:     {
 30:         JFrame frame = new JFrame();
 31:         WindowAdapter wa = new WindowAdapter()
 32:         //Anonymous inner class:
 33:         {
 34:             public void windowClosing(WindowEvent e)
 35:             {
 36:                 System.exit(0);
 37:             }
 38:         };
 39:         frame.addWindowListener(wa);        
 40:         frame.getContentPane().add(new VisualBankAccount(10000.00));
 41:         frame.pack();
 42:         frame.show();
 43:     }


 46:     //Private data and user interface elements
 47:     private double     balance;
 48:     private JLabel     balanceLabel   = new JLabel();
 49:     private JTextField amountField    = new JTextField(10);
 50:     private JButton    depositButton  = new JButton("Deposit");
 51:     private JButton    withdrawButton = new JButton("Withdraw");


 54:     //Constructor
 55:     public VisualBankAccount(double initialDeposit)
 56:     {   
 57:         setBalance(initialDeposit);
 58:         buildUI();
 59:     }
 60: 
 61:     public void actionPerformed(ActionEvent e)
 62:     {
 63:         if (e.getSource() == depositButton)
 64:         {
 65:             double amount = Double.parseDouble(amountField.getText());
 66:             depositFunds(amount);
 67:         }
 68:         if (e.getSource() == withdrawButton)
 69:         {
 70:             double amount = Double.parseDouble(amountField.getText());
 71:             if (amount > getBalance()) amount = getBalance();
 72:             withdrawFunds(amount);
 73:         }
 74:     }

 76:     //Builds the display as seen by the user
 77:     private void buildUI()
 78:     {    
 79:         setLayout(new BorderLayout());

 81:         JPanel buttons = new JPanel(new BorderLayout());
 82:         JPanel balance = new JPanel(new BorderLayout());
 83:         buttons.add(depositButton, BorderLayout.WEST);
 84:         buttons.add(withdrawButton, BorderLayout.EAST);
 85:         balance.add(balanceLabel, BorderLayout.NORTH);
 86:         balance.add(amountField, BorderLayout.SOUTH);
 87:         add(balance, BorderLayout.NORTH);
 88:         add(buttons, BorderLayout.SOUTH);

 90:         //Set up the "callbacks" so that the buttons do something.
 91:         //The deposit button should call depositFunds() and
 92:         //the withdraw button should call withdrawFunds().
 93:         depositButton.addActionListener(this);
 94:         withdrawButton.addActionListener(this);
 95:     }


 98:     //Methods for adjusting state of the account
 99:     private void setBalance(double newBalance)
100:     {
101:         balance = newBalance;
102:         balanceLabel.setText("Balance: " + balance);
103:     }

105:     private double getBalance()
106:     {
107:         return balance;
108:     }

110:     private void depositFunds(double amount)
111:     {
112:         setBalance(getBalance() + amount);
113:     }

115:     private void withdrawFunds(double amount)
116:     {
117:         setBalance(getBalance() - amount);    
118:     }
119: }