I have a bank class below. What is the best way to add amounts to any instance variable in any of the constructors to make it o when i add different amounts from 2 or more different classes it will update in a way that when i call the method it should print out the overall amount added between the variables placed of all the classes.

If you have a better method of doing such please tell.

public class Person
{
    public double balance = 0.0;
    public double happy = 0.0;
    public double newBalance = 0.0;
    public double newHappy = 0.0;
    public Person(){
        balance = 0;
        happy = 0;
    }
    public double getPaid(double moneys){
        newBalance = balance + moneys;
        return newBalance;
    }
    public double getSpend(double moneys){
        newBalance = balance - moneys;
        return newBalance;
    }
    public double getHappy(double happys){
        newHappy = happy + happys;
        return newHappy;
    }
    public double getSad(double happys){
        newHappy = happy - happys;
        return newHappy;
    }
   
}

Recommended Answers

All 3 Replies

'any of the constructors' ... ? do you know what a constructor is? I hope you do know you only have one constructor there.
create a 'addSom' method which takes a double as a parameter and has either void or boolean as returnValue. void (since you don't really need a result), or boolean, which sends true if all goes to plan, false if an error occurs.

So basically if i call the getPaid class once and get paid 10 and then it goes through an if statement if i want to call it a again so i do and addas another 10 it stays at 10 instead of going to 20.

well ... you also have your methods a bit ... wrong.
your get-methods aren't supposed to take parameters, nor ar they supposed to change the values.

all they should do is return the current values. in order to change the values of your variables, you should use set-methods.

that it stays ten is because you do (each time) 0 + newValue. if that newValue is 10 each time, than the result will remain 10.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.