Results 1 to 4 of 4
- 11-24-2010, 09:45 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
pass a variable from one object to another
each customer is an object. there are 3 classes. the main class, a current account class and an abstract account. i want one customer to be able to transfer money to another but have no idea how. when i withdraw from one customer can i then automatically deposit to another customer that amount. in other words can i reference the other object from within the current account class. The transfer method is at the very end. Any help would be great thatnks.
The 3 classes are below:
main
Java Code:class BankAccountTester { public static void main(String[] args) { CurrentAccount acc1 = new CurrentAccount("Ann Smith", 300); CurrentAccount acc2 = new CurrentAccount("Ben Murphy"); acc1.deposit(7550); acc2.deposit(200); acc1.transfer } }
Java Code:public class CurrentAccount extends Account { private int accountNo; private static int accNo; public CurrentAccount(String name) { super(name); } public CurrentAccount(String name, double number) { super(name, number); } public int accNo() { return accNo; } public void deposit(double deposit) { double b; b = deposit + super.getCurrentBal(); super.setCurrentBal(b); System.out.println("********" + getCurrentBal()); } public void withdraw(double withdraw) { double b; b = super.getCurrentBal() - withdraw; super.setCurrentBal(b); System.out.println("********" + getCurrentBal()); } public void transfer(double number) { } }
- 11-25-2010, 12:39 AM #2
In addition to "double number" in your parameters for transfer(), you will want to pass another CurrentAccount object. Then, you can do something like, this.widthdraw(number); and otherAccount.deposit(number);. Then you could call it from your main like acc1.transfer(500,acc2);.
- 11-25-2010, 01:19 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
I tried adding the following to the transfer method but it wouldn't compile for me. its giving me an error: "cannot find symbol - method deposit(double)"
Java Code:public void transfer(double number, Account acc) { withdraw(number); acc.deposit(number); }
-
Similar Threads
-
static variable /pass by reference
By katturv in forum New To JavaReplies: 15Last Post: 10-03-2010, 09:17 AM -
How can I Pass Variable from JApplet to PHP?
By fantasyme in forum JDBCReplies: 2Last Post: 04-18-2010, 05:48 AM -
How to pass a variable to another frame.
By DJCali in forum New To JavaReplies: 10Last Post: 10-14-2009, 04:57 AM -
How pass variable from jsp to servelet
By shiva in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-22-2009, 02:55 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 07:14 PM
Bookmarks