#How to change an objects field in a method?

1 messages · Page 1 of 1 (latest)

honest star
#

class BankAccount {

BankAccount BankAccount = new BankAccount();

BankAccount toBankAccount = new BankAccount();

}

private String name;

private Integer accountNumber;

private Integer pin;

public Double balance;

public Double amount;

private double transferFunds(Double amount,BankAccount toBankAccount) {

return (this.balance - amount) && toBankAccount.balance + amount;

}

Why can't I add to the 'toBankAccount' balance here ? help me rewrite this method return properly, I've been stuck on this for a while now.

raw schoonerBOT
#

<@&987246399047479336> please have a look, thanks.

midnight heath
#

well

#

okay

#

so

#

as written this code will not compile

#
    class BankAccount {
        BankAccount BankAccount = new BankAccount();
        
        BankAccount toBankAccount = new BankAccount();
    }
    
    private String name;
    
    private Integer accountNumber;
    
    private Integer pin;
    
    public Double balance;
    
    public Double amount;
    
    private double transferFunds(Double amount,BankAccount toBankAccount) {
    
        return (this.balance - amount) && toBankAccount.balance + amount;
    }
#

notice how your private fields aren't even in a class?

#

i'd expect

#
class Something {
    class BankAccount {
        BankAccount BankAccount = new BankAccount();
        
        BankAccount toBankAccount = new BankAccount();
    }
    
    private String name;
    
    private Integer accountNumber;
    
    private Integer pin;
    
    public Double balance;
    
    public Double amount;
    
    private double transferFunds(Double amount,BankAccount toBankAccount) {
    
        return (this.balance - amount) && toBankAccount.balance + amount;
    }
}
#

At which point your problem is that you have a nested class

#

which has two instance fields

#

but you have no instances of that class (which holds 2 references to instances of the same class? - i'm 99% sure if you said new BankAccount() your program would crash immediately)

#

TL;DR; your code is a bit too broken for me to answer that question

honest star
#

//Dylan_Schulze
class BankAccount {
public static void main(String[] args) {
BankAccount BankAccount = new BankAccount();
BankAccount toBankAccount = new BankAccount();
}

private String name;
private Integer accountNumber;
private Integer pin;
public Double balance;
public Double amount;

private BankAccount() {
name = "No Name Given";
accountNumber = null;
pin = null;
balance = null;
}
private BankAccount(String name) {
this.name = name;
}
private BankAccount(Integer accountNumber) {
this.accountNumber = accountNumber;
}
private BankAccount(Integer accountNumber, Integer pin) {
this.accountNumber = accountNumber;
this.pin = pin;
}
private double withdrawl(Double withdrawAmount, Double balance) {
return balance - withdrawAmount;
}
private double deposit(Double depositAmount, Double balance) {
return balance + depositAmount;
}
private double transferFunds(Double amount,BankAccount toBankAccount) {
return (this.balance - amount) && toBankAccount.deposit(amount) + amount;
}
private String stringFullInfo() {
return "{id: " + accountNumber + " name: " + name + " pin: " + pin + " Balance: " + balance + "}";
}
}

#

This is the full code if it helps, I'm a beginner so im super lost here, the transferFunds method is the only one giving me problems and I've tried debugging for hours now with no luck and am super frustrated. If you could rewrite the return statement to properly do what im trying to do I would be so so grateful.

midnight heath
#

CODE HERE

#

to send code

#
//Dylan_Schulze
class BankAccount {
   public static void main(String[] args) {
       BankAccount BankAccount = new BankAccount();
       BankAccount toBankAccount = new BankAccount();
   }

   private String name;
   private Integer accountNumber;
   private Integer pin;
   public Double balance;
   public Double amount;

   private BankAccount() {
       name = "No Name Given";
       accountNumber = null;
       pin = null;
       balance = null;
   }
   private BankAccount(String name) {
       this.name = name;
   }
   private BankAccount(Integer accountNumber) {
       this.accountNumber = accountNumber;
   }
   private BankAccount(Integer accountNumber, Integer pin) {
       this.accountNumber = accountNumber;
       this.pin = pin;
   }
   private double withdrawl(Double withdrawAmount, Double balance) {
       return balance - withdrawAmount;
   }
   private double deposit(Double depositAmount, Double balance) {
       return balance + depositAmount;
   }
   private double transferFunds(Double amount,BankAccount toBankAccount) {
       return (this.balance - amount) && toBankAccount.deposit(amount) + amount;
   }
   private String stringFullInfo() {
       return "{id: " + accountNumber + " name: " + name + " pin: " + pin + " Balance: " + balance + "}";
   }
}
#

for now on

#

and i'm not sure what the issue is

#

the transferFunds method needs to return a double as per its definition

#

which...eh?

#

i think you want

#
   private void transferFunds(Double amount,BankAccount toBankAccount) {
       if (this.balance > amount) {
           this.balance -= amount;
           toBankAccount.balance += amount;
       }
   }
#

but your definitions of deposit and withdrawal look off too

#

since they aren't actually changing the balance

#

they are just returning what the balance would be if you did a deposit or a withdrawal

#

which i don't think was your intention

#

also

#

stop putting private on things

#

just delete all the publics and privates from your code for now

#

you don't need them really and you are confused about too many other things

#

we can add them later

honest star
#

Thanks man I appreciate this

#

I’m a beginner coder and java isn’t my preferred language to begin with so I get kinda stuck sometimes😅

midnight heath
#

is this an assignment you can ask your teacher about?

#

or is this just you doing stuff

honest star
#

Assignment for a class

#

I only came here bc I request help for code frequently from him and I feel bad always emailing him

midnight heath
honest star
#

I now have to write a tester class to make sure everything in the class I just made runs smooth !

midnight heath
#

and if college, which college

honest star
#

Freshman, college

#

Rowan University in New Jersey

midnight heath
#

thanks

#

(I'm making a list)

honest star
#

I’m ngl, not a huge fan of Java so far, if it was up to me I’d have done this in Python but using Java is a requirement to pass this course

midnight heath
honest star
#

I agree

#

They keep switching the languages on us

midnight heath
#
#

idk what your deadline for this particular assignment is

#

but there is a good chance you will need to actually know java in the future

#

we generally point people at that course

honest star
#

I’ll definitely check it out

midnight heath
honest star
#

I’m very serious about becoming a better coder

#

I try to study a lot on my own time aswell outside of school