Your method cashReserves()
never modifies the value of currentCash
.
You have a commented out line, //currentCash=newCash;
which would modify the value of currentCash
, but... it's commented out.
EDIT: Given the current edit to the original question, in which you've uncommented the line in question and moved it out of the else
block, I can only guess that the value of your price
variable isn't properly set. It doesn't matter what newCash
is set to. If price
is 0
, then the line:
newCash = currentCash - price;
is the same as
newCash = currentCash - 0;
or just
newCash = currentCash;
So later in your method when you do:
currentCash = newCash;
And expect currentCash
to have changed (and are claiming it's "resetting"), your problem is actually that you simply aren't ever changing the value of currentCash
. It doesn't matter what newCash
was before you called this method. If price
is 0
, your method sets newCash = currentCash
, then you set currentCash = newCash
... which is the same as just saying currentCash = currentCash
so it just stays at 59
.
EDIT2: Per another edit of the answer... my suspicions in edit1 were exactly correct. You have:
price=0;
In your constructor.
newCash = currentCash /*59*/ - price /*0*/;
//newCash = 59
currentCash = newCash /*59*/;
//currentCash = 59
Nothing is resetting. You're just never changing the value of currentCash
.
Perhaps you're taking user input in another class and need to use that to set price
. In this case, you need to create a setter
for price
:
public void setPrice(double newPrice) {
price = newPrice;
}
Or let cashReserves
set price
(the former would be preferable to the latter, I think).
public void cashReserves(double newPrice) {
price = newPrice;
//do everything else your cashReserves method is currently doing
}