2017-02-21 4 views
-3

Zunächst einmal lassen Sie mich damit beginnen ist eine Schule Aufgabe, die ich dachte, war abgeschlossen, aber wurden gebeten, nachzuarbeiten und stecken seit Stunden darauf.Mittagessen Bestellung nicht sammeln Gesamtkosten

Ich hatte es funktioniert aber hart codiert die Gesamtkosten, die ich jetzt auskommentiert habe. Ich muss den individuellen Preis jedes Einzelteils in meine Essensklasse geben, habe es alle Berechnungen dort machen und es zurückbringen und ich verstehe nicht, warum totalCost nur den Preis des Sodas hält und nichts anderes hinzufügt. Ich glaube, dass mein Problem innerhalb der SetPrice() und Total() liegt, aber jede Information würde geschätzt werden.

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

} 

und

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

Sorry, so funktioniert StackOverflow nicht. Fragen der Form _ "Hier ist eine Reihe von meinem Code, bitte debuggen Sie für mich" _ sind Off-Topic. Bitte besuchen Sie die [Hilfe] und lesen Sie [fragen] für weitere Informationen. –

+0

Entschuldigung, ich bin sehr neu dazu werde ich meine Frage bearbeiten. – MikeyBeans

Antwort

0

Sie nicht den Wert Instanzen Totalcost passieren. Mein Vorschlag ist eine Getter-Methode für Food Instanz erstellen, es gibt nur die aktuell Lebensmittelgesamtkosten:

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

Änderung Konstruktor Food (String nItem, double nPrice, double nFat, double nCarb, double nFiber), nfoodtotal entfernt.

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

Ich musste am Ende das nfoodTotal im Konstruktor behalten, damit es wusste, wie viele es bestellt hatte, aber ich habe es am Ende bekommen. Vielen Dank für Ihre Hilfe. Ich schätze es sehr. – MikeyBeans

Verwandte Themen