2013-10-30 34 views
-2

我有一個叫做Food的抽象類,其中我將一個double currentCash初始化爲59.我有2個Food類;水果和肉類,其中我從我的現金儲備59中減去用戶輸入的價格。在用戶鍵入價格後,進入下一個食品對象,無論其是水果還是肉類,現金儲備再次返回59 。java hw。具有運行現金儲備的抽象類,現金總是返回初始化的數字

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"); 
    } 

} 

}

每一種cashReserves方法後,currentCash返回到59再一次,不是在價格用戶類型後的新價值。

你是進入

什麼種類的蘋果都

MAC

進入優先

輸入的價格,你願意支付

蘋果的種類:mac

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

您還有58.0美元可供您購買。

+0

你在哪裏修改'currentCash'的值? – nhgrif

+1

讓我們從簡單的東西開始:你不應該用float或double來代表金錢。使用BigDecimal或int/long。 – Bobby

+0

你在哪裏聲明並賦予'newCash'和'price'值的代碼在哪裏? – nhgrif

回答

2

您的方法cashReserves()永遠不會修改currentCash的值。

你有一條註釋掉的線,//currentCash=newCash;它會修改currentCash的值,但是......它被註釋掉了。


編輯:鑑於當前編輯原來的問題,在你註釋掉這行,並搬出來的else塊的,我只能猜測你的price變量ISN」的價值正確設置。 newCash設置爲什麼並不重要。如果price0,那麼該行:

newCash = currentCash - price; 

相同

newCash = currentCash - 0; 

或只是

newCash = currentCash; 

所以後來在你的方法,當你做:

currentCash = newCash; 

和期望currentCash已經改變(並聲稱它是「重置」),你的問題實際上是你根本不會改變currentCash的值。在調用此方法之前調用newCash並不重要。如果price0,則您的方法設置爲newCash = currentCash,然後您設置currentCash = newCash ...這與剛纔說的currentCash = currentCash相同,所以它只停留在59


編輯2:每隔一個編輯的答案......我在edit1中的懷疑是完全正確的。你有:

price=0; 

在你的構造函數。

newCash = currentCash /*59*/ - price /*0*/; 
//newCash = 59 

currentCash = newCash /*59*/; 
//currentCash = 59 

什麼都沒有重置。你永遠不會改變currentCash的價值。

也許你正在用另一個類的用戶輸入,並需要使用它來設置price。在這種情況下,你需要創建一個setterprice

public void setPrice(double newPrice) { 
    price = newPrice; 
} 

還是讓cashReserves設置price(前者會更優於後者,我認爲)。

public void cashReserves(double newPrice) { 
    price = newPrice; 
    //do everything else your cashReserves method is currently doing 
} 
+0

我試過了,但沒有奏效,所以我評論了一下。 –

+0

定義「不工作」?而且我並不是暗示取消註釋行會產生你想要的結果,只是這條註釋行是唯一可以修改你的'currentCash'值的行。 – nhgrif

+0

它確實有效。我懷疑問題在於它在'else {}'塊內,除非'newCash <= 0',否則永遠不會到達該塊。 – CmdrMoozy

1

您如何以價格作爲投入?

你可以嘗試這樣的代碼以及

currentCash= currentCash-price; 

不需要定義一個新的變量來存儲差值。

+0

用戶在主要類型中使用它: (Fruit)Object.setPrice(keyboard.nextDouble()); –

+0

你正在接受輸入的方式很好,currentCash也被定義爲靜態,意味着每當它被任何對象修改時,它都會反映未來使用它時的變化值。 更好地使用curentCash = currentCash - price – user2885596