Results 1 to 4 of 4
  1. #1
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default 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
        }
    }
    current account
    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)
        {
          
        }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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);.

  3. #3
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    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);      
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kev670 View Post
    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)"
    Well, the obvious question for most of us here is... does the class Account have a method deposit(double)? Only you know since you have the code for this class; we don't.

Similar Threads

  1. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 08:17 AM
  2. Replies: 2
    Last Post: 04-18-2010, 04:48 AM
  3. How to pass a variable to another frame.
    By DJCali in forum New To Java
    Replies: 10
    Last Post: 10-14-2009, 03:57 AM
  4. How pass variable from jsp to servelet
    By shiva in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-22-2009, 01:55 PM
  5. Replies: 1
    Last Post: 03-04-2009, 06:14 PM

Posting Permissions

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