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;
5 Years
Discussion Span
Last Post by stultuske

'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.

Edited by stultuske: n/a


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.