java hw. Abstract class with a running cash reserve, cash always returning to initialized number [closed]

StackOverflow https://stackoverflow.com/questions/19686920

  •  01-07-2022
  •  | 
  •  

Question

I have an abstract class called Food where I initialize a double currentCash to 59. I have 2 subclasses of Food; Fruit and Meat where I subtract the price the user types in from my cash reserves of 59. After the user types in a price, then proceeds to the next Food object, whether its a Fruit or Meat type, the cash reserve returns to 59 again.

 abstract public class Food implements Interface {//abstract class. 

 static public double currentCash=59;
 double newCash=0; 
 }



public class Fruit extends Food implements Interface {



public  String name;
public  double price;
public  String setName;
public  int priority;



public Fruit() {

    name="no name yet.";
    price=0;
    priority=0;
    realPrice=0;
}

public Fruit(String initialName, int initialPriority, double initalPrice, double initalrealPrice){
    name=initialName;
    priority=initialPriority;
    price=initalPrice;
    realPrice=initalrealPrice;
}

public int getPriority() {
    return priority;
}


public void setPriority(int priority) {
    if (priority > 0 && priority <= 7) {

        this.priority = priority;
    } else {

        System.err.println("Error, enter 1 through 7"); 

    }
}


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}

public void setrealPrice(double realPrice){
    this.realPrice=realPrice;
}

@Override
public void writeOutput(){

    System.out.println ("Name: "+getName()+" Priority: "+priority +" Price: "+price );



}
public boolean hasSameName (Fruit otherFruit){
    return this.name.equalsIgnoreCase(otherFruit.name);
}

public void setPrice(double price) {
    this.price=price;

}

public double getPrice(){
return price;

}

public double realPrice(){
    return realPrice;
}
@Override
public String eatable() {
    String eatablePrint= "Interface eatable";
    return eatablePrint;
}



@Override
public void cashReserves() {



    newCash= currentCash-price;

    if (newCash>0){
        System.out.println("You have "+newCash+" dollars left for your list.\n");
    }
    else 
    {
    String k = "out of cash";
        System.out.println(k);


    }

    currentCash=newCash;

    }

@Override
public void realPrices() {

    double realCash;
    realCash=currentCash-realPrice;// the price you want to pay is what you type in, but the actual price is here. 
    System.out.println("The grocery store price is " +realPrice+" dollars, and you have "+ realCash+" dollars left.\n");
    if (realCash<10){
    System.out.println("You're running out of real cash");
    }

    if (realCash<=0){
        System.out.println("You're out of real cash");
    }

}

}

After each of these cashReserves methods, currentCash returns to 59 again, not the new value after user types in a price.

You entered yes

what kind of apple

mac

enter priority

1

enter price you want to pay

1

Type of apple : mac

Apple's Price you want to pay : 1.0 dollars.

You have 58.0 dollars left for your list.

Was it helpful?

Solution

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
}

OTHER TIPS

How you are taking price as input?

or

You can try your code like this as well

currentCash= currentCash-price;

No need to define a new variable to store the difference value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top