-2

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.

2 Answers2

2

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
}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • I tried that, but doesnt work, so I commented it out. – healthcare.gov programmer Oct 30 '13 at 15:33
  • Define "doesnt work"? And I'm not suggesting that uncommenting the line will produce the result that you want, just that this commented line is the only line which would even modify your `currentCash` value. – nhgrif Oct 30 '13 at 15:35
  • It does work. I suspect the problem is that it's inside the `else { }` block, which is never reached unless `newCash <= 0`. – CmdrMoozy Oct 30 '13 at 15:36
  • Where is `price` initialized? If `price = 0`, then `newCash = currentCash - price` means `newCash = currentCash - 0`, so `currentCash = newCash` effectively does nothing, since you already assigned it the other way... EDIT: And if `newCash > 0`, then you never go in to `currentCash = newCash` anyway because you execute the `if`, not the `else`. – nhgrif Oct 30 '13 at 15:37
  • actually still doesnt work. Still resets to 59 – healthcare.gov programmer Oct 30 '13 at 15:47
  • @user2803251 We can't guess at what is wrong with your code. You have to edit the original question to include the changes you've made. And while you're doing that, please include the lines of code for which you initialize `newCash` and `price` because these are absolutely crucial to the functionality of your code and you're not showing that to us. – nhgrif Oct 30 '13 at 15:48
  • And as a note, unless you're using breakpoints and stepping through your program step by step and seeing the value of `currentCash` change to something else and then resetting to 59, I'm going to bet that nothing is resetting, it's just not being changed at all. – nhgrif Oct 30 '13 at 15:49
  • In included my setter and getter for price. I tested it by printing out my getPrice which is what the user types in, however, when I go to the next Food object on my list and have the user type in at price, again, currentCash goes back to 59. – healthcare.gov programmer Oct 30 '13 at 15:59
  • But dont you want to have default constructor price as set as 0? Then user types in the price? – healthcare.gov programmer Oct 30 '13 at 16:02
  • Use a debugger or print the values every step of the way. Until you can demonstrate that you've actually changed the value of `currentCash`, you can't claim it's "resetting". You have nothing that demonstrates you're properly changing it's value. – nhgrif Oct 30 '13 at 16:02
  • I seem to have had a problem with a another class. I was trying to solve this problem with another solution (hardwiring prices into the code), and after deleting that area from my code, it seems to work now. I will accept your answer though and thank you. – healthcare.gov programmer Oct 30 '13 at 16:21
  • @user2803251 Always build and test one class at a time. Don't integrate the classes until you've tested them individually. – nhgrif Oct 30 '13 at 16:22
  • thank you for the input and following up on the edits I made. – healthcare.gov programmer Oct 30 '13 at 16:23
1

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.

  • the user types it in main using: (Fruit)Object.setPrice(keyboard.nextDouble()); – healthcare.gov programmer Oct 30 '13 at 16:27
  • The way you are taking input is fine and the currentCash is also defined as static that means whenever it get modified by any object it will reflect the changes value in future use of it. Better you use curentCash = currentCash - price – user2885596 Oct 30 '13 at 17:17