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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With