我在理解如何从这个ArrayList中获取变量时遇到了问题

时间:2018-04-12 06:27:38

标签: java arrays

我正在为学校制作食谱盒,我需要帮助了解如何访问ArrayList中的单个变量。我只需要访问配方文件中的totalCalories变量,而不是配料,这样我就可以在配方中添加卡路里总数。这是代码。

package recipebox;

import java.util.Scanner;
import java.util.ArrayList;

public class Recipe {

private String recipeName;
private int servings;
private ArrayList recipeIngredients;
private double totalRecipeCalories;


public String getRecipeName() {
    return recipeName;
}

public void setRecipeName(String recipeName) {
    this.recipeName = recipeName;
}

public double getTotalRecipeCalories() {
    return totalRecipeCalories;
}

public void setTotalRecipeCalories(double totalRecipeCalories) {
    this.totalRecipeCalories = totalRecipeCalories;
}

public ArrayList getRecipeIngredients() {
    return recipeIngredients;
}

public void setRecipeIngredients(ArrayList recipeIngredients) {
    this.recipeIngredients = recipeIngredients;
}

public int getServings() {
    return servings;
}

public void setServings(int servings) {
    this.servings = servings;
}

public Recipe() {
    this.recipeName = "";
    this.servings = 0;
    this.recipeIngredients = new ArrayList<>();
    this.totalRecipeCalories = 0;

}
public Recipe(String recipeName, int servings, ArrayList <String> recipeIngredients, double totalRecipeCalories) {
    this.recipeName = recipeName;
    this.servings = servings;
    this.recipeIngredients = recipeIngredients;
    this.totalRecipeCalories = totalRecipeCalories;
}

public void printRecipe() {
    int singleServingCalories = (int)(totalRecipeCalories/getServings());
    System.out.println("Recipe: " + getRecipeName());
    System.out.println("Serves: " + getServings());
    System.out.println("Ingredients:");
    System.out.println(recipeIngredients);
    System.out.println("Each serving has " + singleServingCalories + " Calories.");
}

public void addIngredient() {
    recipeIngredients.add(Ingredient.createNewIngredient());
}

public static void main(String[] args) {
    createNewRecipe();
}

public static Recipe createNewRecipe() {
    double totalRecipeCalories = 0;
    ArrayList <String> recipeIngredients = new ArrayList();
    boolean addMoreIngredients = true;

    Recipe myRecipe = new Recipe();

    Scanner scnr = new Scanner(System.in);

    System.out.println("Please enter the recipe name: ");
    while (!scnr.hasNextLine()){ 
        System.out.println("Invalid input");
        System.out.println("Please enter the recipe name: ");
        scnr.nextLine();
    }
    String recipeName = scnr.nextLine();

    System.out.println("Please enter the number of servings: ");
    while (!scnr.hasNextInt()){
        System.out.println("Invalid input");
        System.out.println("Please enter the number of servings: ");
        scnr.nextLine();
    }
    int servings = scnr.nextInt();


    do {
        myRecipe.addIngredient();
totalRecipeCalories += recipeIngredients.get(totalCalories);

我没有包括其余部分,因为它不重要。最后一行是错误的,是需要修复的。现在这里是成分代码。

package recipebox;

import java.util.Scanner;

public class Ingredient {

private String nameOfIngredient;
private float numberUnits;
private String unitMeasurement;
private int numberCaloriesPerUnit;
private double totalCalories;

/**
 * @return the nameOfIngredient
 */
public String getNameOfIngredient() {
    return nameOfIngredient;
}

/**
 * @param nameOfIngredient the nameOfIngredient to set
 */
public void setNameOfIngredient(String nameOfIngredient) {
    this.nameOfIngredient = nameOfIngredient;
}

/**
 * @return the numberUnits
 */
public float getNumberUnits() {
    return numberUnits;
}

/**
 * @param numberUnits the numberUnits to set
 */
public void setNumberUnits(float numberUnits) {
    this.numberUnits = numberUnits;
}

/**
 * @return the numberCaloriesPerUnit
 */
public int getNumberCaloriesPerUnit() {
    return numberCaloriesPerUnit;
}

/**
 * @param numberCaloriesPerUnit the numberCaloriesPerUnit to set
 */
public void setNumberCaloriesPerUnit(int numberCaloriesPerUnit) {
    this.numberCaloriesPerUnit = numberCaloriesPerUnit;
}

/**
 * @return the totalCalories
 */
public double getTotalCalories() {
    return totalCalories;
}

/**
 * @param totalCalories the totalCalories to set
 */
public void setTotalCalories(double totalCalories) {
    this.totalCalories = totalCalories;
}

/**
 * @return the unitMeasurement
 */
public String getUnitMeasurement() {
    return unitMeasurement;
}

/**
 * @param unitMeasurement the unitMeasurement to set
 */
public void setUnitMeasurement(String unitMeasurement) {
    this.unitMeasurement = unitMeasurement;
}

public Ingredient() {
    this.nameOfIngredient = "";
    this.numberUnits = 0.00f;
    this.unitMeasurement = "";
    this.numberCaloriesPerUnit = 0;
    this.totalCalories = 0.0;
}

public Ingredient(String nameOfIngredient, float numberUnits, String unitMeasurement, int numberCaloriesPerUnit, double totalCalories) {
    this.nameOfIngredient = nameOfIngredient;
    this.numberUnits = numberUnits;
    this.unitMeasurement = unitMeasurement;
    this.numberCaloriesPerUnit = numberCaloriesPerUnit;
    this.totalCalories = totalCalories;
}

public static Ingredient createNewIngredient() {

    Scanner scnr = new Scanner(System.in);

    System.out.println("Please enter the name of the ingredient: ");
    while (!scnr.hasNextLine()){ 
        System.out.println("Invalid input");
        System.out.println("Please enter the name of the ingredient: ");
        scnr.nextLine();
    }
    String nameOfIngredient = scnr.nextLine();

    System.out.println("What unit of measurement will you be using? ");
    while (!scnr.hasNextLine()){
        System.out.println("Invalid input");
        System.out.println("What unit of measurement will you be using? ");
        scnr.nextLine();
    }
    String unitMeasurement = scnr.nextLine();

    System.out.println("Please enter the number of " + unitMeasurement + " of " + nameOfIngredient + " we will need: ");
    while (!scnr.hasNextFloat()) {
        System.out.println("Invalid input");
        System.out.println("Please enter the number of " + unitMeasurement + " of " + nameOfIngredient + " we will need: ");
        scnr.nextLine();
    }
    float numberUnits = scnr.nextFloat();

    System.out.println("Please enter the number of calories per " + unitMeasurement + ": ");
    while (!scnr.hasNextInt()) {
        System.out.println("Invalid input");
        System.out.println("Please enter the number of calories per " + unitMeasurement + ": ");
        scnr.nextLine();
    }
    int numberCaloriesPerUnit = scnr.nextInt();

    double totalCalories = numberUnits * numberCaloriesPerUnit;
    System.out.println(nameOfIngredient + " uses " + numberUnits + " " + unitMeasurement + " and has " + totalCalories + " calories.");

recipebox.Ingredient tempIngredient = new recipebox.Ingredient(nameOfIngredient, numberUnits, unitMeasurement, numberCaloriesPerUnit, totalCalories);
return tempIngredient;
}

}

现在当我调试配方文件时,在从获取成分返回之后,我可以在变量列表中看到我需要的变量,但我无法弄清楚如何到达它。

This is what the debug variable menu looks like

如果有人可以帮助我访问这个变量,并且可以用初学者的术语解释你是如何做到的,我会非常感激。感谢。

1 个答案:

答案 0 :(得分:2)

变量recipeIngredients是List而不是对象。该列表包含成分类型的对象。因此,对于列表中某种成分的总卡路里值,您需要首先访问该列表中的特定成分,然后获取其属性。

所以这将是recipeIngredients.get(0).getTotalCalories() - 获取列表中第一个成分的总卡路里

如果你想要整个收据,你需要一个遍历该列表并计算的循环。类似的东西:

int calories;
for(Object ingredient: recipeIngredients)
 calories+= ((Ingredient)ingredient).getTotalCalories();

同样在代码中:

 System.out.println(recipeIngredients); 

出于同样的原因,您不会打印任何有用的内容。你需要仔细检查每种成分并以漂亮的方式打印出来。

您也可以考虑使用仿制药制作成分类型列表,这样您就不需要进行施法,而且您只是将它用于成分<​​/ p>