简体   繁体   中英

How to make objects interact with each other in java?

I have recently started learning java. This might be a silly question, but is it possible to make a method that transfer balance from 'firstAccount' object to 'secondAccount' object?

public class SavingsAccount{
  int balance;

  public SavingsAccount(String name, int balance){
    this.balance = balance;
    this.name = name;
  }

  public static void main(String[] args){
    SavingsAccount firstAccount = new SavingsAccount("A", 5000);
    SavingsAccount secondAccount = new SavingsAccount("B", 3000);
  }
}

The best I came up with is this,

public void transfer(int amountToTransfer){
    firstAccount.balance += amountToTransfer;
    secondAccount.balance -= amountToTransfer;
  }

and of course it doesn't work. How can I fix it?
Thanks in advance!

You can either make it a static method that requires you to pass the two accounts to be used (you can name the variables whatever you want) or make a non-static method that requires one account as a parameter while using the instance.

Here is the static variation:

public static void transfer(int amountToTransfer, SavingsAccount toAccount, SavingsAccount fromAccount){
    toAccount.balance += amountToTransfer;
    fromAccount.balance -= amountToTransfer;
}

This would be used in a static context such as a main , and would be called using YourClass.transfer(yourAmount, firstAccount, secondAccount) .

Here is the non-static variation that would be inside your SavingsAccount class, you can decide whether it makes more sense to transfer to the instance, or from the instance:

public void transfer(int amountToTransfer, SavingsAccount toAccount){
    toAccount.balance += amountToTransfer;
    this.balance -= amountToTransfer;
}

This would be used with your instance firstAccount.transfer(amount, secondAccount) , and would be transferring the amount from firstAccount to secondAccount . I recommend using this way rather using the static option.

Below is an example on how to use both from within your main :

public static void main(String[] args){
   SavingsAccount firstAccount = new SavingsAccount("A", 5000);
   SavingsAccount secondAccount = new SavingsAccount("B", 3000);

   int amount = 500;
   firstAccount.transfer(amount, secondAccount); //This is the non-static variation
   transfer(amount, firstAccount, secondAccount); //Static variation, you might need to use the Class.transfer
}

It is very possible but in your code, your SavingsAccount class doesn't have a variable called 'name'. You should create that first. Also, you should add the two savings account variables to the class and make them static. Then, you will be able to access those savings accounts from the function. You should also make the function to transfer money static. Your final code could look like this:

public static class SavingsAccount {

    int balance;
    String name;

    public static SavingsAccount firstAccount = new SavingsAccount("A", 5000);
    public static SavingsAccount secondAccount = new SavingsAccount("B", 3000);

    public SavingsAccount(String name, int balance) {
        this.name = name;
        this.balance = balance;
    }

    public static void transfer(int amountToTransfer){
        firstAccount.balance += amountToTransfer;
        secondAccount.balance -= amountToTransfer;
    }

    public static void main(String[] args) {
        transfer(put the amount to transfer here);
    }

}

but is it possible to make a method that transfer balance from 'firstAccount' object to 'secondAccount' object?

There are many ways. Apart from the static method approach already mentioned.

Here are some variations. But ultimately, the idea is the same - you need the 2 object instances.

Idea 1 :

//Letting the object itself interact with another object (simplified):

public class SavingsAccount{
    String name;
    int balance;

    public SavingsAccount(String name, int balance){
      this.balance = balance;
      this.name = name;
    }

    public void transferTo(SavingsAccount receivingAccount, int amt){
        this.balance -= amt;
        receivingAccount += amt;    //recommended to use setter instead
    }  
}

Idea 2 :

//Letting the object itself interact with another object:

public class SavingsAccount{
    String name;
    int balance;

    public SavingsAccount(String name, int balance){
      this.balance = balance;
      this.name = name;
    }

    public int deductBalance(int amt){
        this.balance -= amt;
        return amt;
    } 

    public int addBalance(int amt){
        this.balance += amt;
        return amt;
    }  

    public void transferTo(SavingsAccount receivingAccount, int amt){
        receivingAccount.addBalance(this.deductBalance(amt));
    }
}

class TesterClass{
    SavingsAccount accountA = new SavingsAccount("A", 5000);
    SavingsAccount accountB = new SavingsAccount("B", 3000);
    accountA.transferTo(accountB, 100);   //transfer $100 from A to B
}

Idea 3 :

//Letting a 3rd party to let both objects intract. 
//The 2 objects doesn't need to each other's existance.

public class SavingsAccount{
    String name;
    int balance;

    public SavingsAccount(String name, int balance){
      this.balance = balance;
      this.name = name;
    }

    public void deductBalance(int amt){
        this.balance -= amt;
    } 

    public void addBalance(int amt){
        this.balance += amt;
    }    
}

class TesterClass{
    SavingsAccount accountA = new SavingsAccount("A", 5000);
    SavingsAccount accountB = new SavingsAccount("B", 3000);

    public static void transferMoney(SavingsAccount fromAcc, SavingsAccount toAcc, int amt){
        fromAcc.deductBalance(amt);
        toAcc.addBalance(amt);
    }
}

The number of ways goes on.. here are just a few..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM