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.