7 Replies - 22888 Views - Last Post: 18 February 2011 - 02:44 PM Rate Topic: -----

#1 benz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 11

Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 11:22 AM

I am writing a graphical application for a bank account class. I'm trying to use buttons and text fields. When I run it, it's not depositing or withdrawing money from the initial balance. Can anyone help me to understand what I am doing wrong?


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;

public class BankAccount extends JFrame
{
   private static final int FRAME_WIDTH = 400;
   private static final int FRAME_HEIGHT = 250;
   private static final double INITIAL_BALANCE = 1000;   
   double result;
   private JLabel initiallabel;
   private JLabel depositlabel;
   private JLabel withdrawlabel;
   private JTextField depositfield;   
   private JTextField withdrawfield;
   private JButton depositbutton;
   private JButton withdrawbutton;
   private JLabel resultlabel;
   private JPanel panel;
   private BankAccount account;
 
   public InvestmentFrame()
   {  
      account = new BankAccount(INITIAL_BALANCE);
      createTextField();
      createButton();
      createPanel();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   private void createTextField()
   {
	  final int FIELD_WIDTH = 10;
	  
	  initiallabel = new JLabel("Current Balance  $" + INITIAL_BALANCE);
	    
	  depositlabel = new JLabel("Deposit: ");
      depositfield = new JTextField(FIELD_WIDTH);
            
      withdrawlabel = new JLabel("Withdraw: ");
      withdrawfield = new JTextField(FIELD_WIDTH);
      
      resultlabel = new JLabel("New Balance: $" + result);
   }
   
   private void createButton()
   {
      depositbutton = new JButton("Deposit");
      class DepositListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            double dp = Double.parseDouble(depositfield.getText());
            double dpamount = account.getBalance() + dp;
            account.deposit(dpamount);
            result = dpamount;
            resultlabel = new JLabel("New Balance: $" + result);
         }            
      }
      ActionListener d = new DepositListener();
      depositbutton.addActionListener(d);
      
      
      withdrawbutton = new JButton("Withdraw");
      class WithdrawListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            double wd = Double.parseDouble(depositfield.getText());
            double wdamount = account.getBalance() - wd;
            account.withdraw(wdamount);
            result = wdamount;
            resultlabel = new JLabel("New Balance: $" + result);
         }            
      }
      ActionListener w = new WithdrawListener();
      withdrawbutton.addActionListener(w);
      
   }

   private void createPanel()
   {
      panel = new JPanel();
      panel.add(initiallabel);
      panel.add(depositlabel);
      panel.add(depositfield);
      panel.add(depositbutton);
      panel.add(resultlabel);
      panel.add(withdrawlabel);
      panel.add(withdrawfield);
      panel.add(withdrawbutton);
      panel.add(resultlabel);
      add(panel);
   }
 }



import javax.swing.JFrame;

public class BankAccountViewer
{  
   public static void main(String[] args)
   {  
      JFrame frame = new BankAccountFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}


This post has been edited by macosxnerd101: 18 February 2011 - 11:23 AM
Reason for edit:: Please use code tags!!!


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a GUI Bank Account Class in Java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 12:03 PM

You call account.withdraw() in your withdraw button listener, but I don't see that method.
Was This Post Helpful? 1
  • +
  • -

#3 benz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 11

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 12:12 PM

View PostGregBrannon, on 18 February 2011 - 12:03 PM, said:

You call account.withdraw() in your withdraw button listener, but I don't see that method.


I don't understand. Can you explain a little more?
Was This Post Helpful? 0
  • +
  • -

#4 rekless  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 08-January 09

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 01:41 PM

where is the method withdraw();

you havn't shown us all of your code or you are trying to use methods that havn't been created yet.
Was This Post Helpful? 1
  • +
  • -

#5 benz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 11

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 01:57 PM

//A bank account has a balance that can be changed by deposits and withdrawals.

public class BankAccount
{  
   private double balance;

   //Constructs a bank account with a zero balance.
   
   public BankAccount()
   {   
      balance = 0;
   }

   //Constructs a bank account with a given balance.
  
   public BankAccount(double initialBalance)
   {   
      balance = initialBalance;
   }

   //Deposits money into the bank account.
   
   public void deposit(double amount)
   {  
      double newBalance = balance + amount;
      balance = newBalance;
   }

   //Withdraws money from the bank account.
   
   public void withdraw(double amount)
   {   
      double newBalance = balance - amount;
      balance = newBalance;
   }
   
   //Gets the current balance of the bank account.
      
   public double getBalance()
   {   
      return balance;
   }
}



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;

public class AccountFrame extends JFrame
{
   private static final int FRAME_WIDTH = 400;
   private static final int FRAME_HEIGHT = 250;
   private static final double INITIAL_BALANCE = 1000;   
   double result;
   private JLabel initiallabel;
   private JLabel depositlabel;
   private JLabel withdrawlabel;
   private JTextField depositfield;   
   private JTextField withdrawfield;
   private JButton depositbutton;
   private JButton withdrawbutton;
   private JLabel resultlabel;
   private JPanel panel;
   private BankAccount account;
 
   public AccountFrame()
   {  
      account = new BankAccount(INITIAL_BALANCE);
      
      resultlabel = new JLabel("New Balance: $" + account.getBalance());
      
      createTextField();
      createButton();
      createPanel();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   private void createTextField()
   {
	  final int FIELD_WIDTH = 10;
	  
	  initiallabel = new JLabel("Current Balance  $" + INITIAL_BALANCE);
	    
	  depositlabel = new JLabel("Deposit: ");
      depositfield = new JTextField(FIELD_WIDTH);
            
      withdrawlabel = new JLabel("Withdraw: ");
      withdrawfield = new JTextField(FIELD_WIDTH);
   }
   
   private void createButton()
   {
      depositbutton = new JButton("Deposit");
      class DepositListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            double dp = Double.parseDouble(depositfield.getText());
            double dpamount = account.getBalance() + dp;
            account.deposit(dpamount);
            result = dpamount;
            resultlabel.setText("Balance: " + result);
         }            
      }
      ActionListener d = new DepositListener();
      depositbutton.addActionListener(d);
      
      
      withdrawbutton = new JButton("Withdraw");
      class WithdrawListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            double wd = Double.parseDouble(withdrawfield.getText());
            double wdamount = account.getBalance() - wd;
            account.withdraw(wdamount);
            result = wdamount;
            resultlabel.setText("Balance: " + result);
         }            
      }
      ActionListener w = new WithdrawListener();
      withdrawbutton.addActionListener(w);
      
   }

   private void createPanel()
   {
      panel = new JPanel();
      panel.add(initiallabel);
      panel.add(depositlabel);
      panel.add(depositfield);
      panel.add(depositbutton);
      panel.add(withdrawlabel);
      panel.add(withdrawfield);
      panel.add(withdrawbutton);
      panel.add(resultlabel);
      add(panel);
   }
 }




import javax.swing.JFrame;

public class Account
{  
   public static void main(String[] args)
   {  
      JFrame frame = new AccountFrame();
      frame.setTitle("Bank Account");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}



Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 02:30 PM

Thanks for the additional code.

This:

      withdrawbutton = new JButton("Withdraw");
      class WithdrawListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            double wd = Double.parseDouble(withdrawfield.getText());
            double wdamount = account.getBalance() - wd;
            account.withdraw(wdamount);
            result = wdamount;
            resultlabel.setText("Balance: " + result);
         }            
      }
      ActionListener w = new WithdrawListener();
      withdrawbutton.addActionListener(w);




Determines the withdrawal amount, wd, from the withdrawfield, assigns

wdamount = balance - wd;


then calls method widthdraw( wdamount ) which, I think, will zero your balance. I think it's a math/logic error. Try using account.withdraw( wdamount ), and result = getBalance() instead. You don't need result (maybe it's used elsewhere ), but you could instead say,

resultlabel.setText( "Balance: " + account.getBalance() );



after calling the withdraw() method.
Was This Post Helpful? 1
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 02:35 PM

Your logic was wrong in your withdraw method.



Withdraw Method


public void actionPerformed(ActionEvent event)
    	  {   /*Get value from field*/
    		  double wd = Double.parseDouble(withdrawfield.getText());
    		  /*Withdraw passing value needed to withdraw as an argument*/
    		  account.withdraw(wd);
    		  /*Get new balance*/
    		  resultlabel.setText("Balance: " + account.getBalance());
    	  }                      


This post has been edited by m-e-g-a-z: 18 February 2011 - 02:36 PM

Was This Post Helpful? 1
  • +
  • -

#8 benz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 11

Re: Creating a GUI Bank Account Class in Java

Posted 18 February 2011 - 02:44 PM

Thanks for all the help everyone. It's working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1