Results 1 to 20 of 21
- 05-21-2011, 11:19 PM #1
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
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
AccountDemo.javaJava 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")); } } }
Thanks!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())); } }Last edited by petur170; 05-24-2011 at 02:31 AM.
- 05-21-2011, 11:26 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
10 years? Amazing, so where is your "NAME_account" I don't see it in your code?
- 05-21-2011, 11:47 PM #3
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
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
- 05-22-2011, 08:23 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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()...
- 05-22-2011, 04:52 PM #5
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
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())); } }
- 05-22-2011, 07:14 PM #6
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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)...
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: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.
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..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 } }
Hope this helps!
- 05-22-2011, 08:02 PM #7
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
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"
- 05-22-2011, 08:13 PM #8
Please copy and paste the full text of the error messages here.there are now errors
- 05-23-2011, 01:22 AM #9
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
Sorry, I forgot to mention that you need to make the accounts "static" when you declare them in AccountDemo.
Thus,
Is it a problem if those variables are static for your program?Java Code:class AccountDemo { //Create Empty Accounts static InterestBearingAccount myAccount = new InterestBearingAccount(); static InterestBearingAccount petersAccount = new InterestBearingAccount(); static InterestBearingAccount mansAccount = new InterestBearingAccount(); .....etc.
- 05-23-2011, 03:10 AM #10
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
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.
- 05-23-2011, 03:47 AM #11
Sounds like you need to do some debugging. Does your IDE have interactive debugging?but then nothing happens.
Otherwise add println statements to show where the execution flow goes and how variable values change.
- 05-23-2011, 04:40 AM #12
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
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.
- 05-23-2011, 04:56 AM #13
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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.
- 05-24-2011, 02:29 AM #14
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Ok, heres all my code (thats necessary). I'll update the post too.
AccountDemo.java
InterestButton.javaJava 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())); } }
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")); } } }
- 05-24-2011, 05:07 AM #15
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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?
- 05-24-2011, 11:49 PM #16
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
text displayed in the console. But later im gonna put it in a applet
- 05-24-2011, 11:54 PM #17
print outs for applets go in the browser's Java console.
- 05-25-2011, 11:44 PM #18
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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())
- 05-25-2011, 11:52 PM #19
You would only use the console for debug output. You need to create a GUI for output to be shown to the user.
- 05-26-2011, 02:25 AM #20
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Bank Account Project
By Haal in forum New To JavaReplies: 0Last Post: 03-20-2011, 07:30 PM -
java bank account problem
By awil121 in forum New To JavaReplies: 1Last Post: 10-13-2010, 10:47 PM -
Create a java class for a bank account!!?
By singh345 in forum New To JavaReplies: 1Last Post: 03-17-2010, 04:26 PM -
Bank Account
By HPcompaq256 in forum New To JavaReplies: 11Last Post: 02-26-2010, 09:05 PM -
calculating Bank interest rate.
By dotnet007 in forum New To JavaReplies: 10Last Post: 05-13-2008, 09:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks