Question: Need help getting this to work. I feel like I am making it worse everytime I try and fix any errors I have. Here is
Need help getting this to work. I feel like I am making it worse everytime I try and fix any errors I have. Here is the Java class:
Recipe.java:
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 int getServings() { return servings; }
public void setServings(int servings) { this.servings = servings; }
public ArrayList getRecipeIngredients() { return recipeIngredients; }
/** * @param recipeIngredients the recipeIngredients to set */ public void setRecipeIngredients(ArrayList recipeIngredients) { this.recipeIngredients = recipeIngredients; }
public double getTotalRecipeCalories() { return totalRecipeCalories; }
public void setTotalRecipeCalories(double totalRecipeCalories) { this.totalRecipeCalories = totalRecipeCalories; }
public Recipe() { this.recipeName = ""; this.servings = 0; this.recipeIngredients = new ArrayList<>(); this.totalRecipeCalories = 0; }
public Recipe(String recipeName, int servings, ArrayList recipeIngredients, double totalRecipeCalories) { this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = totalRecipeCalories; }
public void printRecipe() { double singleServingCalories = totalRecipeCalories / servings; System.out.println("Recipe: " + getRecipeName()); System.out.println("Yield: " + 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(currentIngredientName); } System.out.println("Total Calories per serving: " + singleServingCalories); }
public Recipe createNewRecipe() { //Comment out for Stepping Stone 6 //public Recipe createNewRecipe() { //uncomment for Stepping Stone 6 double totalRecipeCalories = 0; ArrayList 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("How many servings: "); int servings = scnr.nextInt();
do { System.out.println("Please enter the ingredient name or type 'e' if you are done: "); String ingredientName = scnr.next(); if (ingredientName.toLowerCase().equals("e")) { addMoreIngredients = false; } else { Ingredient tempIngredient = new Ingredient().enterNewIngredient(ingredientName); recipeIngredients.add(tempIngredient); } } while (addMoreIngredients); for (int i = 0; i < recipeIngredients.size(); i++) { Ingredient currentIngredient = recipeIngredients.get(i); float ingredientAmount = currentIngredient.getIngredientAmount(); int ingredientCalories = currentIngredient.getIngredientCalories(); double ingredientTotalCalories = ingredientAmount * ingredientCalories; totalRecipeCalories += ingredientTotalCalories; } Recipe newRecipe = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories); //newRecipe.printRecipe();//comment out for Stepping Stone 6 return newRecipe; //uncomment for Stepping Stone 6
}
}
Ingredient.java:
package recipebox;
import java.util.Scanner;
public class Ingredient { private String nameOfIngredient; private float numberCups; private int numberCaloriesPerCup; 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 numberCups */ public float getNumberCups() { return numberCups; }
/** * @param numberCups the numberCups to set */ public void setNumberCups(float numberCups) { this.numberCups = numberCups; }
/** * @return the numberCaloriesPerCup */ public int getNumberCaloriesPerCup() { return numberCaloriesPerCup; }
/** * @param numberCaloriesPerCup the numberCaloriesPerCup to set */ public void setNumberCaloriesPerCup(int numberCaloriesPerCup) { this.numberCaloriesPerCup = numberCaloriesPerCup; }
/** * @return the totalCalories */ public double getTotalCalories() { return totalCalories; }
/** * @param totalCalories the totalCalories to set */ public void setTotalCalories(double totalCalories) { this.totalCalories = totalCalories; } public Ingredient() { this.nameOfIngredient = ""; this.numberCups = 0; this.numberCaloriesPerCup = 0; this.totalCalories = 0.0; } public Ingredient(String nameOfIngredient, float numberCups, int numberCaloriesPerCup, double totalCalories) { this.nameOfIngredient = nameOfIngredient; this.numberCups = numberCups; this.numberCaloriesPerCup = numberCaloriesPerCup; this.totalCalories = totalCalories; } public Ingredient addIngredient(String tempNameOfIngredient) { //tempNameOfIngredient = tempNameOfIngredient; float tempNumberCups; int tempNumberCaloriesPerCup; double tempTotalCalories; Scanner scnr = new Scanner(System.in); System.out.println("Please enter the name of the ingredient: "); tempNameOfIngredient = scnr.next(); System.out.println("Please enter the number of cups of " + nameOfIngredient + " we'll need: "); tempNumberCups = scnr.nextFloat(); System.out.println("Please enter the name of calories per cup: "); tempNumberCaloriesPerCup = scnr.nextInt(); tempTotalCalories = numberCups * numberCaloriesPerCup; Ingredient tempNewIngredient = new Ingredient(tempNameOfIngredient, tempNumberCups, tempNumberCaloriesPerCup, tempTotalCalories); return tempNewIngredient; } }
Recipebox.java:
Recipebox.java: package recipebox; import java.util.ArrayList; import java.util.Scanner; public class RecipeBox { private ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
