Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help figuring out what is wrong with this Code: for Recipe and RecipeBox Recipe: package SteppingStones; import java.util.Scanner; import java.util.ArrayList; public class SteppingStone5_Recipe

I need help figuring out what is wrong with this Code: for Recipe and RecipeBox

Recipe:

package SteppingStones;

import java.util.Scanner;

import java.util.ArrayList;

public class SteppingStone5_Recipe {

private String recipeName;

private int servings; //Number of servings

private ArrayList recipeIngredients = new ArrayList(); //an array list for all ingredients in the recipe

private double totalRecipeCalories = 0.0; //this is the total calories of the recipe

public SteppingStone5_Recipe() {

this.recipeName = "";

this.servings = 0;

this.recipeIngredients = new ArrayList<>(); //Array list starts empty

this.totalRecipeCalories = 0;

}

public SteppingStone5_Recipe(String recipeName, int servings,

ArrayList recipeIngredients, double totalRecipeCalories)

{

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = 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 void printRecipe() {

double singleServingCalories = totalRecipeCalories / servings;

System.out.println("Recipe: " + recipeName);

System.out.println("Serves: " + 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(recipeName + " has " + servings + " Servings and each serving has " + singleServingCalories + " Calories.");

System.out.println(recipeName + " has a total of " + totalRecipeCalories + " calories."); }

public SteppingStone5_Recipe createNewRecipe(SteppingStone5_Recipe newRecipe) {

//public static void main(String[] args) {

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("Please enter the number of servings: ");

int servings = scnr.nextInt();

//this loop starts the ingredient array list and finishes when users types 'end'

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;

}

SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,

servings, recipeIngredients, totalRecipeCalories);

return newRecipe;

}

}

RecipeBox:

package SteppingStones;

import SteppingStones.SteppingStone5_Recipe; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner;

public class SteppingStone6_RecipeBox {

private ArrayList listOfRecipes;

public ArrayList getListOfRecipes() { return listOfRecipes; }

public void setListOfRecipes(ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; }

public SteppingStone6_RecipeBox( ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; }

public SteppingStone6_RecipeBox() { this.listOfRecipes = new ArrayList<>(); }

void printAllRecipeDetails(String selectedRecipe){ for(SteppingStone5_Recipe recipe1: listOfRecipes) { if(recipe1.getRecipeName().equalsIgnoreCase(selectedRecipe)) { recipe1.printRecipe(); return; } } System.out.println("No Recipe found with name: " + selectedRecipe); }

void printAllRecipeNames(){ for(SteppingStone5_Recipe selectedRecipe: listOfRecipes) { System.out.println(selectedRecipe.getRecipeName()); } }

public void addRecipe(SteppingStone5_Recipe tmpRecipe) { listOfRecipes.add(tmpRecipe); }

public static void main(String[] args) { SteppingStone6_RecipeBox myRecipeBox = new SteppingStone6_RecipeBox(); //Uncomment for main method Scanner menuScnr = new Scanner(System.in); System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:"); while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) { System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:"); int input = menuScnr.nextInt();

if (input == 1) { myRecipeBox.newRecipe(); } else if (input == 2) { System.out.println("Which recipe? "); String selectedRecipeName = menuScnr.next(); myRecipesBox.printAllRecipeDetails(selectedRecipeName); } else if (input == 3) { for (int j = 0; j < myRecipesBox.listOfRecipes.size(); j++) { System.out.println((j + 1) + ": " + myRecipesBox.listOfRecipes.get(j).getRecipeName()); } } else { System.out.println(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:"); }

System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions

Question

City Securities has just announced (who, whom) it will hire as CEO.

Answered: 1 week ago