2017-02-21 63 views
-3

首先讓我從這是一個學校的任務,我認爲是完整的,但已被要求返工並被困在它上幾個小時。午餐訂單不收取總成本

我是有工作,但硬編碼的,我現在已經註釋掉的總成本。我需要將每件商品的單獨價格轉移到我的食品類中,讓它將所有計算結果都返回並返回,我不明白爲什麼totalCost只保持蘇打水的價格,並且不增加其他任何東西。我相信我的問題在於setPrice()和total(),但任何信息將不勝感激。

package lunchOrder; 


import java.util.Scanner; 
import java.lang.String; 
import java.text.NumberFormat; 

public class Lunch { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    double totalCost = 0; 
    NumberFormat money = NumberFormat.getCurrencyInstance(); 

    System.out.print("Enter number of hamburgers: "); 
    double hamburgerTotal = input.nextInt(); 
    Food hamburger = new Food("Hamburger", 1.85, 9.0, 33, 1, hamburgerTotal); 
    System.out.println(hamburger + "\n"); 
//  totalCost += hamburgerTotal = * 1.85;         rework 


    System.out.print("Enter number of salads: "); 
    double saladTotal = input.nextInt(); 
    Food salad = new Food("Salad", 2.00, 1, 11, 5, saladTotal); 
    System.out.println(salad + "\n"); 
    //totalCost += saladTotal * 2.00;          rework 


    System.out.print("Enter number of french fries: "); 
    double frenchFrieTotal = input.nextInt(); 
    Food frenchFrie = new Food("French fries", 1.30, 11, 36, 4, frenchFrieTotal); 
    System.out.println(frenchFrie + "\n"); 
    //totalCost += frenchFrieTotal * 1.30;         rework 


    System.out.print("Enter number of sodas: "); 
    double sodaTotal = input.nextInt(); 
    Food soda = new Food("Soda", 0.95, 0, 38, 0, sodaTotal); 
    System.out.println(soda + "\n"); 
    //totalCost += sodaTotal * .95;           rework 

    System.out.println(soda.setPrice()); 
} 

} 

package lunchOrder; 
import java.lang.String; 
import java.text.NumberFormat; 



public class Food { 
String item; 
double price; 
double fat; 
double carb; 
double fiber; 
double total; 
double foodTotal; 
double totalCost = 0; 
NumberFormat money = NumberFormat.getCurrencyInstance(); 



public Food (String nItem, double nPrice, double nFat, double nCarb, double nFiber, double nfoodTotal){ 
    item = nItem; 
    price = nPrice; 
    fat = nFat; 
    carb = nCarb; 
    fiber = nFiber; 
    foodTotal = nfoodTotal; 
    totalCost = totalCost +(price * foodTotal); 



} 
public void total(){ 

    double totalCost = price * foodTotal; 
    totalCost += (price * foodTotal); 
    System.out.print(totalCost); 
} 

public String setPrice(){ 
    String priceString; 


    priceString = "Your order comes to: " + totalCost; 
    return(priceString); 
} 

public String toString() { 
     String orderString; 



     orderString = "Each " + item + " has " + fat + "g of fat, " 
      + carb + "g of carbs, and " + fiber + ".g of fiber."; 
     return(orderString); 
    } 

} 
+3

對不起,這不是StackOverflow的是如何工作的。問題形式_「這是我的一堆代碼,請爲我調試」_被認爲是無關緊要的。請訪問[幫助]並閱讀[問]以獲取更多信息。 –

+0

對不起,我很新,我會編輯我的問題。 – MikeyBeans

回答

0

沒有通過TOTALCOST值的情況。我的建議是創造一個getter方法Food例如,它只返回當前的糧食總成本:

public double getCost(){ 
    return price * foodTotal; 
} 

改變構造函數Food (String nItem, double nPrice, double nFat, double nCarb, double nFiber),消除nfoodtotal。

double totalCost = 0.0; 
Food hamburger = new Food(....); 
totalCost += hamburger.getCost(); // sum up the cost of hamburger 

Food salad = new Food(...); 
totalCost += salad.getCost(); // sum up the cost of salad 

System.out.println("total cost" + totalCost); 
+0

我最終不得不在構造函數中保留nfoodTotal,以便知道他們訂購了多少,但我最終得到了它。非常感謝你的幫助,我真的很感激。 – MikeyBeans