Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Question Adding Interest to a Bank Account

    Hi, im am very new to Java (started 3 days ago) and i am also quite young (10) and i am trying to make a button that you press and it adds interest to 3 accounts. I got some of the code here from a youtube video and added a lot but there is an error that you'll see near the end of the code. The error says "NAME_account cannot be resolved". I'll add some of the other .classes if you need them. I'm using Eclipse for editing. Please help me! Here is the Button Code:
    InterestButton.java
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    public class InterestButton extends JFrame {
    	
    	private JButton custom;
    	
    	public InterestButton() {
    		super("Interest Adder");
    		setLayout(new FlowLayout());
    		
    		Icon IB = new ImageIcon(getClass().getResource("IButton.png"));
    		Icon IJB = new ImageIcon(getClass().getResource("IButtonB.png"));
    		
    		custom = new JButton("Add Interest",IB);
    		custom.setRolloverIcon(IJB);
    		add(custom);
    		
    		InterestAddButton handler = new InterestAddButton();
    		custom.addActionListener(handler);
    		
    	}
    	
    	private class InterestAddButton extends AccountDemo implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			myAccount.addMonthlyInterest();
    			petersAccount.addMonthlyInterest();
    			mansAccount.addMonthlyInterest();
    			removeAll();
    			System.out.println ("Man1's Balance: $" +
    					Math.round(petersAccount.getbalance()));
    				System.out.println ("Man2's Balance: $" +
    					Math.round(myAccount.getbalance()));
    				System.out.println ("Man3's Balance: $" +
    					Math.round(mansAccount.getbalance()));
    			repaint();
    			validate();
    				
    				
    			JOptionPane.showMessageDialog(null, String.format("%s","Interest Added"));	
    
    	
    			}
    	}
    }
    AccountDemo.java
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    import javax.swing.JFrame;
    
    class AccountDemo
    {
    	static InterestBearingAccount myAccount = new InterestBearingAccount();
    	static InterestBearingAccount petersAccount = new InterestBearingAccount();
    	static InterestBearingAccount mansAccount = new InterestBearingAccount();
    
    
    
    		public static void main(String args[])
    		{
     
    
    	
    
    
    			
    			InterestButton win = new InterestButton();
    			win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			win.setSize(400,128);
    			win.setVisible(true);
    				
    
    			
    		
    		petersAccount.deposit(2500000.00);
    		myAccount.deposit(54256.00);
    		mansAccount.deposit(14.00);
        
    		myAccount.withdraw(80.00);
    		petersAccount.withdraw(15346);
    		mansAccount.withdraw(451);
                  
    		System.out.println ("Calculating Accounts...");
    		try
    		{
    		Thread.sleep(3000);
    		}
    		catch(InterruptedException e)
    		{
    		e.printStackTrace();
    		}
                    
    		myAccount.addMonthlyInterest();
    		petersAccount.addMonthlyInterest();
    		petersAccount.addMonthlyInterest();
    		System.out.println ("Adding interest to account(s)...");
    		try
    		{
    		Thread.sleep(1000);
    		}
    		catch(InterruptedException e)
    		{
    		e.printStackTrace();
    		}
                   
    		System.out.println ("Man1's Balance: $" +
    			Math.round(petersAccount.getbalance()));
    		System.out.println ("Man2's Balance: $" +
    				Math.round(myAccount.getbalance()));
    		System.out.println ("Man3's Balance: $" +
    				Math.round(mansAccount.getbalance()));
    
    	
    
    			
    		}
    }
    Thanks!
    Last edited by petur170; 05-24-2011 at 02:31 AM.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    275
    Rep Power
    4

    Default

    10 years? Amazing, so where is your "NAME_account" I don't see it in your code?

  3. #3
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    well, there are three of them,
    my_account,
    peters_account,
    and mans_account, so i just put NAME_account to represent all three of them

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    275
    Rep Power
    4

    Default

    Have you declared and instantiated those variables. From error description it looks like compiler don't know what those *_accounts are.
    And try to make your code more java: myAccount, petersAccount, mansAccount, addMonthlyInterest()...

  5. #5
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Yes i made them in AccountDemo.java
    AccountDemo.java:
    Java Code:
    
    
    
    class AccountDemo
    {
    
    
    
    		public static void main(String args[])
    	{
                    // Create an empty account
    		InterestBearingAccount myAccount = new InterestBearingAccount();
    		InterestBearingAccount petersAccount = new InterestBearingAccount();
    		InterestBearingAccount mansAccount = new InterestBearingAccount();
                    // Deposit money
    		petersAccount.deposit(2500000.00);
    		myAccount.deposit(54256.00);
    		mansAccount.deposit(835145.00);
                    // Print current balance
    
    
                    // Withdraw money
    		myAccount.withdraw(80.00);
    		petersAccount.withdraw(15346);
    		mansAccount.withdraw(451);
                    // Print remaining balance
    		System.out.println ("Calculating Accounts...");
    		try
    		{
    		Thread.sleep(3000);
    		}
    		catch(InterruptedException e)
    		{
    		e.printStackTrace();
    		}
                    // Allow a month to pass
    		myAccount.addMonthlyInterest();
    		petersAccount.addMonthlyInterest();
    		petersAccount.addMonthlyInterest();
    		System.out.println ("Adding interest to account(s)...");
    		try
    		{
    		Thread.sleep(1000);
    		}
    		catch(InterruptedException e)
    		{
    		e.printStackTrace();
    		}
                    // Print remaining balance
    		System.out.println ("Man1's Balance: $" +
    			Math.round(petersAccount.getbalance()));
    		System.out.println ("Man2's Balance: $" +
    				Math.round(myAccount.getbalance()));
    		System.out.println ("Man3's Balance: $" +
    				Math.round(mansAccount.getbalance()));
    		
    	
    					
    		
    	
    			}
    
    
    	
    
    }

  6. #6
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Since you declared the accounts as local variables in the AccountDemo class, their values can only be accessed in the Account Demo class. That's why you're getting an error because the compiler cannot find the variables you're trying to access. In order to access them in the InterestButton class you must make them class variables in the AccountDemo Class. So your code should look more like this (with better indentation, not sure how to do it in this post)...

    Java Code:
    class AccountDemo
    {
    //Create Empty Accounts
    InterestBearingAccount myAccount = new InterestBearingAccount();
    InterestBearingAccount petersAccount = new InterestBearingAccount();
    InterestBearingAccount mansAccount = new InterestBearingAccount();
    
    
    		public static void main(String args[])
    	{   
                    // Deposit money
    		petersAccount.deposit(2500000.00);
    		myAccount.deposit(54256.00);
    		mansAccount.deposit(835145.00);
                    // Print current balance
    
                     .........etc.
    Then in your InterestButton class you should access the accounts by proceeding them with the class name they are contained in, so the interestaddbutton method would something like this (again forgive any indentation problems):

    Java Code:
    private class InterestAddButton extends AccountDemo implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			AccountDemo.my_account.add_monthly_interest(); //THIS HAS A ERROR
    			AccountDemo.peters_account.add_monthly_interest(); //THIS HAS A ERROR
    			AccountDemo.mans_account.add_monthly_interest(); //THIS HAS A ERROR
    		}
    	}
    and like milovan said, i'm pretty sure that in java underscores aren't used as commonly for method names or for variables unless they are constants..

    Hope this helps!

  7. #7
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Well it fixes the error in the button code, but in accountdemo there are now errors every time petersAccount, mansAccount, and myAccount is mentioned inside the brackets of "public static void"

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    there are now errors
    Please copy and paste the full text of the error messages here.

  9. #9
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Sorry, I forgot to mention that you need to make the accounts "static" when you declare them in AccountDemo.

    Thus,
    Java Code:
    class AccountDemo
    {
    //Create Empty Accounts
    static InterestBearingAccount myAccount = new InterestBearingAccount();
    static InterestBearingAccount petersAccount = new InterestBearingAccount();
    static InterestBearingAccount mansAccount = new InterestBearingAccount();
    .....etc.
    Is it a problem if those variables are static for your program?

  10. #10
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    No i don't think it matters if its static. Thanks, but if i press the "Add Interest" Button, it says" interest added" but then nothing happens.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    but then nothing happens.
    Sounds like you need to do some debugging. Does your IDE have interactive debugging?
    Otherwise add println statements to show where the execution flow goes and how variable values change.

  12. #12
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Oh, I see what I did wrong, when I add interest to the accounts, it doesn't change the text on the screen because I didn't reprint it. The problem is, when I print it again it just appears right before the old one. Is there a way I can remove text that was printed before? Thanks.

  13. #13
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    where do you want it to change the text? what are you using to display it?
    i think i needa see some more code

    generally though, if its a component you could probably use the removeAll() method and then change the text and repaint() and validate() to update it.

  14. #14
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Ok, heres all my code (thats necessary). I'll update the post too.
    AccountDemo.java
    Java Code:
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    import javax.swing.JFrame;
    
    class AccountDemo
    {
    	static InterestBearingAccount myAccount = new InterestBearingAccount();
    	static InterestBearingAccount petersAccount = new InterestBearingAccount();
    	static InterestBearingAccount mansAccount = new InterestBearingAccount();
    
    
    
    		public static void main(String args[])
    		{
     
    
    	
    
    
    			
    			InterestButton win = new InterestButton();
    			win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			win.setSize(400,128);
    			win.setVisible(true);
    				
    
    			
    		
    		petersAccount.deposit(2500000.00);
    		myAccount.deposit(54256.00);
    		mansAccount.deposit(14.00);
        
    		myAccount.withdraw(80.00);
    		petersAccount.withdraw(15346);
    		mansAccount.withdraw(451);
                  
    		System.out.println ("Calculating Accounts...");
    		try
    		{
    		Thread.sleep(3000);
    		}
    		catch(InterruptedException e)
    		{
    		e.printStackTrace();
    		}
                    
    		myAccount.addMonthlyInterest();
    		petersAccount.addMonthlyInterest();
    		petersAccount.addMonthlyInterest();
    		System.out.println ("Adding interest to account(s)...");
    		try
    		{
    		Thread.sleep(1000);
    		}
    		catch(InterruptedException e)
    		{
    		e.printStackTrace();
    		}
                   
    		System.out.println ("Man1's Balance: $" +
    			Math.round(petersAccount.getbalance()));
    		System.out.println ("Man2's Balance: $" +
    				Math.round(myAccount.getbalance()));
    		System.out.println ("Man3's Balance: $" +
    				Math.round(mansAccount.getbalance()));
    
    	
    
    			
    		}
    }
    InterestButton.java
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    public class InterestButton extends JFrame {
    	
    	private JButton custom;
    	
    	public InterestButton() {
    		super("Interest Adder");
    		setLayout(new FlowLayout());
    		
    		Icon IB = new ImageIcon(getClass().getResource("IButton.png"));
    		Icon IJB = new ImageIcon(getClass().getResource("IButtonB.png"));
    		
    		custom = new JButton("Add Interest",IB);
    		custom.setRolloverIcon(IJB);
    		add(custom);
    		
    		InterestAddButton handler = new InterestAddButton();
    		custom.addActionListener(handler);
    		
    	}
    	
    	private class InterestAddButton extends AccountDemo implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			myAccount.addMonthlyInterest();
    			petersAccount.addMonthlyInterest();
    			mansAccount.addMonthlyInterest();
    			removeAll();
    			System.out.println ("Man1's Balance: $" +
    					Math.round(petersAccount.getbalance()));
    				System.out.println ("Man2's Balance: $" +
    					Math.round(myAccount.getbalance()));
    				System.out.println ("Man3's Balance: $" +
    					Math.round(mansAccount.getbalance()));
    			repaint();
    			validate();
    				
    				
    			JOptionPane.showMessageDialog(null, String.format("%s","Interest Added"));	
    
    	
    			}
    	}
    }

  15. #15
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Are you talking about trying to change text displayed in the console (the bottom box when running it in eclipse) or in a separate window component?

  16. #16
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    text displayed in the console. But later im gonna put it in a applet

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    print outs for applets go in the browser's Java console.

  18. #18
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    to my knowledge you can't change text that's been printed to the console using System.out.print..
    but i could be wrong, maybe norm can offer some insight here?
    if you were to display the information that you would like to change inside of a component then you could use the method that i said earlier (removeAll())

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    You would only use the console for debug output. You need to create a GUI for output to be shown to the user.

  20. #20
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    yeah, i would suggest maybe creating a JFrame and then adding JPanels or JLabels to that in order to display your information

Page 1 of 2 12 LastLast

Similar Threads

  1. Bank Account Project
    By Haal in forum New To Java
    Replies: 0
    Last Post: 03-20-2011, 07:30 PM
  2. java bank account problem
    By awil121 in forum New To Java
    Replies: 1
    Last Post: 10-13-2010, 10:47 PM
  3. Create a java class for a bank account!!?
    By singh345 in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 04:26 PM
  4. Bank Account
    By HPcompaq256 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 09:05 PM
  5. calculating Bank interest rate.
    By dotnet007 in forum New To Java
    Replies: 10
    Last Post: 05-13-2008, 09:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •