午餐订单没有收取总费用

时间:2017-02-21 17:09:02

标签: java

首先让我开始这是一项我认为已完成的学校作业,但已经被要求返工,并且已被困在其中数小时。

我有它工作,但硬编码我现在评论的总成本。我需要将每个项目的单个价格传递到我的食品类中,让它在那里进行所有计算并返回它并且我不明白为什么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);
    }

 }

1 个答案:

答案 0 :(得分:0)

不要将totalCost值传递给实例。我的建议是为Food实例创建一个getter方法,它只返回当前的食物总费用:

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