Stuck trying to use a loop to iterate through the ingredients.
Stuck trying to use a loop to iterate through the ingredients. I am getting an error code I can make no sense of?
Did I initialize the variable at the start incorrectly?
private ArrayList<String> recipeIngredients;
SteppingStone5_recipe.java is the starter code I have to complete.
SteppingStone5_RecipeTest.java is the tester code I have to run against it.
Both are included below.
package SteppingStones;
import java.util.Scanner;
import java.util.ArrayList;
/*
*
*/
public class SteppingStone5_Recipe {
private String recipeName;
/**
* Add three variables:
*
* 1. a variable 'servings' to store how many people the recipe will feed;
*
* 2. an ArrayList variable 'recipeIngredients' to store the text for the
* names* (recipeName) each recipe ingredient added
*
* 3. a variable totalRecipeCalories
*/
private int servings;
private ArrayList<String> recipeIngredients;
private double totalRecipeCalories;
// Add mutators and accessors for the class variable.
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public ArrayList<String> getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(ArrayList<String> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
// End mutators and accessors
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0; // <--- assignment value with appropriate data type
this.recipeIngredients = new ArrayList<>(); // <-- assignment value for empty ArrayList
this.totalRecipeCalories = 0.0;
}
public SteppingStone5_Recipe(String recipeName, int servings, ArrayList<String> recipeIngredients,
double totalRecipeCalories) // <-- use appropriate data type for the ArrayList and the servings arguments
{
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() {
/**
* Declare an int variable singleServingCalories. Assign
* singleServingCalories to the totalRecipeCalories divided by the
* servings
*/
double singleServingCalories = (totalRecipeCalories / servings);
/**
* Print the following recipe information: Recipe: <<recipeName>>
* Serves:
* <<servings>> Ingredients: <<Ingredient1>> <<Ingredient2>> ... <<Last
*
* Ingredient>>
* Each serving has <<singleServingCalories>> Calories.
* HINT --> Use a for loop to iterate through the ingredients
*/
System.out.println ("Recipe: " + getRecipeName());
System.out.println ("Serves: " + getServings() + " servings.");
System.out.println ("Ingredients:");
for (int i = 0; i < recipeIngredients.size(); i++){
Ingredient currentIngredient = recipeIngredients.get (i);
String currentIngredientName = currentIngredient.getIngredientName();
}
System.out.println ("total Calories per serving: " + singleServingCalories);
}
public static SteppingStone5_Recipe createNewRecipe() {
double totalRecipeCalories = 0.0;
ArrayList<Ingredient> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the recipe name: ");
String recipeName = scnr.nextLine();
System.out.println("Please enter the number of servings: ");
int servings = scnr.nextInt(); // correct data type & Scanner assignment method for servings variable
do {
Ingredient thisIngredient = new Ingredient();
System.out.println("Please enter the ingredient name or type end if you are finished entering ingredients: ");
String ingredientName = scnr.next();
if (ingredientName.toLowerCase().equals("end")) {
addMoreIngredients = false;
} else {
//is this needed??: thisIngredient.setNameOfIngredient(ingredientName); // adding ingredient to recipe
/**
* Add the ingredient name to recipeIngredients
*/
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
/**
* Add the total Calories from this ingredient
* (ingredientCalories * ingredientAmount) to the
* totalRecipeCalories
*/
totalRecipeCalories = totalRecipeCalories + (ingredientCalories * ingredientAmount);
recipeIngredients.add(thisIngredient);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
return recipe1;
}
}
Stuck trying to use a loop to iterate through the ingredients. package SteppingStones;
import java.util.ArrayList;
public class SteppingStone5_RecipeTest {
public static void main(String[] args) {
// Create first recipe using constructor and setter methods
ArrayList<String> recipeIngredients = new ArrayList<>();
recipeIngredients.add("Peanut butter");
recipeIngredients.add("Jelly");
recipeIngredients.add("Bread");
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(
"Peanut butter & jelly sandwich", 2, recipeIngredients, 300);
System.out.println("RECIPE 1");
recipe1.printRecipe();
// Now change some of the values using mutator methods
recipe1.setRecipeName("Turkey sandwich");
recipe1.setServings(5);
recipe1.setTotalRecipeCalories(500);
System.out.println(); // blank line
System.out.println("RECIPE 1 (Modified)");
// printRecipe() will show these values have changed but
// recipeIngredients was not modified
recipe1.printRecipe();
System.out.println(); // blank line
System.out.println("RECIPE 2");
// Create second recipe using our static createNewRecipe() method
// This is similar to a well-known design pattern called Factory
SteppingStone5_Recipe recipe2 = SteppingStone5_Recipe.createNewRecipe();
// No need to set hard-coded values here as createNewRecipe() will
// prompt the user for ingredient info
recipe2.printRecipe();