These two parts to my final project need fixed: When uncommented myRecipeBox has
ID: 3908773 • Letter: T
Question
These two parts to my final project need fixed:
When uncommented myRecipeBox has a red squiggly line
/**
* This could should be uncommented when using the
* public static main() method
*
* Comment out this section when using the
* SteppingStone6_RecipeBox_tester.
if (input == 1) {
myRecipeBox.newRecipe();
} else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next();
myRecipeBox.printAllRecipeDetails(selectedRecipeName);
} else if (input == 3) {
for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {
System.out.println((j + 1) + ": " + myRecipeBox.listOfRecipes.get(j).getRecipeName());
}
}else {
System.out.println(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:");
}*/
I need to finish this as well:
Final Project Details:
For your final project submission, you should add a menu item and a method
to access the custom method you developed for the Recipe class
based on the Stepping Stone 5 Lab.
Here is the Recipe class code:
package recipe.collection;
import java.util.ArrayList;
import java.util.Scanner;
public class Recipe {
private String recipeName;
private int servings;
private ArrayList<Ingredient>recipeIngredients;
private double totalRecipeCalories;
public Recipe() {
this.recipeName = "";
this.servings = 0; //<--- assignment value with appropriate data type
this.recipeIngredients = new ArrayList<Ingredient>(); //<-- assignment value for empty ArrayList
this.totalRecipeCalories = 0;
}
public static void main(String[] args){
Recipe r = createNewRecipe();//constructor to create new recipe
r.printRecipe();//Accessor for the printRecipe() method
}
//accessor that retrieves the recipe name
public String getRecipeName() {
return recipeName;
}
//mutator that sets the recipe name
public void setRecipeName(String recipeName){
this.recipeName = recipeName;
}
//accessor to retrieve the serving size
public int getServing() {
return servings;
}
//mutator to set the serving size
public void setServing(int serving) {
this.servings = serving;
}
// calls the Array that holds the recipe ingredients
public ArrayList<Ingredient> getRecipeIngredients() {
return recipeIngredients;
}
//sets the ingredients held by the array
public void setRecipeIngredients(ArrayList<Ingredient> recipeIngredients){
this.recipeIngredients = recipeIngredients;
}
//accessor that calls the total recipe calories
public double getTotalRecipeCalories(){
return totalRecipeCalories;
}
//mutator that sets the total recipe calories
public void setTotalRecipeCalories(double totalRecipeCalories){
this.totalRecipeCalories = totalRecipeCalories;
}
//Recipe class
public Recipe(String recipeName, int servings,
ArrayList<Ingredient> 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() {
//printRecipeWithDifferentServings(servings);
double singleServingCalories = totalRecipeCalories / servings;
System.out.println("Recipe: " + getRecipeName());
System.out.println("Yield: " + getServing() + " servings");
System.out.println("Ingredients:");
for (int i = 0; i < recipeIngredients.size(); i++) {
Ingredient currentIngredient = recipeIngredients.get(i);
String currentIngredientName = currentIngredient.getNameOfIngredient();
System.out.println(currentIngredientName);
}
System.out.print(".Each serving has " + singleServingCalories + " calories");
}
//CUSTOM METHOD
public static Recipe createNewRecipe(){
double totalRecipeCalories = 0;
ArrayList <Ingredient> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
String reply="";
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: ");
@SuppressWarnings("LocalVariableHidesMemberVariable")
int servings = scnr.nextInt();
do {
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 {
Ingredient tempIngredient = new Ingredient().addIngredient();
recipeIngredients.add(tempIngredient);
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
totalRecipeCalories = ingredientCalories * ingredientAmount;
System.out.println("Do you want to continue. Y/N");
reply = scnr.nextLine();
}
} while (!reply.equals("n")) ;
scnr.close();
Recipe recipe1 = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
return recipe1;
}
}
Here is the collection box code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package recipe.collection;
import java.util.ArrayList;
import java.util.Scanner;
public class RecipeBox {
private ArrayList<Recipe> listOfRecipes;
//Accessor Recipe Array
public ArrayList<Recipe> getListOfRecipes(){
return listOfRecipes;
}
//Mutator for Array List
public void setListOfRecipes(ArrayList<Recipe> listOfRecipes){
this.listOfRecipes = listOfRecipes;
}
//Constructor for recipe box
void RecipeBox(){
listOfRecipes = new ArrayList<Recipe>();
}
void RecipeBox(ArrayList<Recipe> listOfRecipes)
{
this.listOfRecipes=listOfRecipes;
}
public void printAllRecipeDetails(String selectedRecipe)
{
for (int i = 0; i < listOfRecipes.size(); i++)
{ // traverse ArrayList
// fetches the element at i position and compare its name with the aargument string
if(listOfRecipes.get(i).getRecipeName().equals(selectedRecipe))
{
listOfRecipes.get(i).printRecipe(); // if matches then prints Recipes detail
}
}
}
public void printAllRecipeNames()
{
System.out.println("List of Recipes are : ");
for (int i = 0; i < listOfRecipes.size(); i++){
System.out.println(listOfRecipes.get(i).getRecipeName()); // just prints Recipe names
}
}
void addRecipe()
{
Recipe r= new Recipe();
Recipe tmpRecipe = r.createNewRecipe(); // creates new Recipe
listOfRecipes.add(tmpRecipe); // adds it to the ArrayList
}
public void menu() {
// Create a Recipe Box
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) {
addRecipe();
} else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next();
printAllRecipeDetails(selectedRecipeName);
} else if (input == 3) {
for (int j = 0; j < listOfRecipes.size(); j++) {
System.out.println((j + 1) + ": " + listOfRecipes.get(j).getRecipeName());
}
} else {
System.out.println(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:");
}
}
/**
* This could should be uncommented when using the
* public static main() method
*
* Comment out this section when using the
* SteppingStone6_RecipeBox_tester.
if (input == 1) {
myRecipeBox.newRecipe();
} else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next();
myRecipeBox.printAllRecipeDetails(selectedRecipeName);
} else if (input == 3) {
for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {
System.out.println((j + 1) + ": " + myRecipeBox.listOfRecipes.get(j).getRecipeName());
}
}else {
System.out.println(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:");
}*/
}
}
Explanation / Answer
New Implemented Source Code:-
---------------------------------------
package com.koothada;
import java.util.ArrayList;
class Ingredient
{
//variables
private String ingredientName;
private double ingredientAmount;
private String unitMeasurement;
private double totalCalories;
public Ingredient(String ingredientName, double ingredientAmount, String unitMeasurement, double totalCalories)
{
super();
this.ingredientName = ingredientName;
this.ingredientAmount = ingredientAmount;
this.unitMeasurement = unitMeasurement;
this.totalCalories = totalCalories;
}
//Setter and getter
public String getIngredientName()
{
return ingredientName;
}
public void setIngredientName(String ingredientName)
{
this.ingredientName=ingredientName;
}
public double getIngredientAmount()
{
return ingredientAmount;
}
public void setIngredientAmount(double ingredientAmount)
{
this.ingredientAmount=ingredientAmount;
}
public String getUnitMeasurement()
{
return unitMeasurement;
}
public void setUnitMeasurement(String unitMeasurement)
{
this.unitMeasurement = unitMeasurement;
}
public double getTotalCalories()
{
return totalCalories;
}
public void setTotalCalories(double totalCalories)
{
this.totalCalories = totalCalories;
}
//print item detail
public void printItemDetails()
{
System.out.println("ingredientName " + this.ingredientName);
System.out.println("ingredientAmount " + this.ingredientAmount);
System.out.println("totalCalories " + this.totalCalories);
System.out.println("unitMeasurement " + this.unitMeasurement);
}
}
class Recipe
{
//variables
private ArrayList<Ingredient>ingredientList;
private ArrayList<String>instructions;
//constructor
public Recipe(String recipeName)
{
this.ingredientList = new ArrayList<Ingredient>();
this.instructions = new ArrayList<>();
}
//get IngredientList
public ArrayList<Ingredient> getIngredientList()
{
return ingredientList;
}
//set IngredientList
public void setIngredientList(ArrayList<Ingredient> ingredientList)
{
this.ingredientList = ingredientList;
}
// add Ingredient in recipe
public boolean addIngredient(Ingredient ingredient)
{
return this.ingredientList.add(ingredient);
}
// remove Ingredient in recipe
public boolean removeIngredient(Ingredient ingredient)
{
return this.ingredientList.remove(ingredient);
}
// remove all Ingredient in recipe
public boolean removeAllIngredient()
{
return this.ingredientList.removeAll(ingredientList);
}
// print Ingredients in recipe
public void printRecipe()
{
int i = 0;
System.out.println("Recipe contains:");
for (Ingredient ing : ingredientList)
{
i++;
System.out.println(i);
ing.printItemDetails();
}
}
//create new recipe
public static Recipe createNewRecipe(String recipeName)
{
return new Recipe(recipeName);
}
//insert instruction
public boolean insertInstruction(String instruction)
{
return this.instructions.add(instruction);
}
public void printInstruction()
{
instructions.toString();
}
}
public class RecipeBox
{
private ArrayList<Recipe>recipes;
public RecipeBox()
{
this.recipes = new ArrayList<>();
}
//add a Recipe in collection
public void addItem(Recipe recipe)
{
recipes.add(recipe);
}
//delete a Recipe in collection
public void deleteItem(Recipe recipe)
{
recipes.remove(recipe);
}
//print all recipes
public void printAllRecipes()
{
int i = 0;
for(Recipe rec: recipes)
{
i++;
System.out.println("Recipe " + i);
rec.printRecipe();
}
}
public static void main(String[] args)
{
//create some demo recipes
Recipe recipe1 = Recipe.createNewRecipe("Pizza");
Ingredient ingredient1 = new Ingredient("Pepperoni",20,"gram",300);
recipe1.addIngredient(ingredient1);
Recipe recipe2 = Recipe.createNewRecipe("Meatballs");
Ingredient ingredient2 = new Ingredient("Gound beef",50,"gram",200);
recipe2.addIngredient(ingredient2);
// add in box
RecipeBox box = new RecipeBox();
box.addItem(recipe1);
box.addItem(recipe2);
box.printAllRecipes();
}
}
Sample Output:-
---------------------
Recipe 1
Recipe contains:
1
ingredientName Pepperoni
ingredientAmount 20.0
totalCalories 300.0
unitMeasurement gram
Recipe 2
Recipe contains:
1
ingredientName Gound beef
ingredientAmount 50.0
totalCalories 200.0
unitMeasurement gram
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.