Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I replace a recipe name or the entire recipe entered by the user? This is the code I have thus far and the

How would I replace a recipe name or the entire recipe entered by the user? This is the code I have thus far and the class that I am calling from. The area that I am referring to is the method replaceRecipeName.

package Milestones;

import java.util.ArrayList;

import java.util.Scanner;

class RecipeBox {

private ArrayList listOfRecipes;

public ArrayList getListOfRecipes() {

return listOfRecipes;

}

public void setListOfRecipes(ArrayList listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

public RecipeBox() {

this.listOfRecipes = new ArrayList();

}

public RecipeBox(ArrayList listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

public void printAllRecipeDetails(String selectedRecipeName) {

for (int i = 0; i < listOfRecipes.size(); i++) {

if (listOfRecipes.get(i).getRecipeName().equals(selectedRecipeName)) {

listOfRecipes.get(i).printRecipe();

}

}

}

public void printAllRecipeNames() {

for (Recipe currentRecipe : listOfRecipes) {

System.out.println(currentRecipe.getRecipeName());

}

}

public void replaceRecipeName(Recipe selectedRecipeName){

int replace = 0;

for (Recipe recipe: listOfRecipes) {

if (recipe.equals(selectedRecipeName)){

replace = listOfRecipes.indexOf(selectedRecipeName);

}

}

}

public void addNewRecipe() {

listOfRecipes.add(new Recipe().createNewRecipe());

}

public void deleteRecipe(Recipe selectedRecipeName) {

int delete = 0;

for (Recipe recipe : listOfRecipes) {

if (recipe.equals(selectedRecipeName)) {

delete = listOfRecipes.indexOf(selectedRecipeName);

}

}

listOfRecipes.remove(selectedRecipeName);

}

public static void main(String[] args) {

RecipeBox myRecipeBox = new RecipeBox();

Scanner menuScnr = new Scanner(System.in);

System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " +

"3. Print All Recipe Names " + "4. Edit A Recipe Name " + "5. Delete A Recipe " +

"Please select a menu item:");

while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {

int input = menuScnr.nextInt();

if (input == 1) {

myRecipeBox.addNewRecipe();

} else if (input == 2) {

System.out.println("Which recipe? ");

String selectedRecipeName = menuScnr.next();

myRecipeBox.printAllRecipeDetails(selectedRecipeName);

} else if (input == 3) {

myRecipeBox.printAllRecipeNames();

}

else if (input == 4){

System.out.println("Which recipe? ");

String selectedRecipeName;

selectedRecipeName = menuScnr.next();

int replaceRecipeName;

}

else if(input == 5) {

System.out.print("Choose a recipe to delete. > ");

myRecipeBox.printAllRecipeNames();

String selectedRecipeName = menuScnr.next();

int deleteMenu;

if (menuScnr.hasNext()) {

deleteMenu = menuScnr.nextInt();

if (deleteMenu<= myRecipeBox.listOfRecipes.size() && deleteMenu > 0) {

myRecipeBox.deleteRecipe(myRecipeBox.listOfRecipes.get(deleteMenu - 1));

}

}

}

else {

System.out.println(" Menu " + "1. Add A Recipe " + "2. Print Recipe " +

"3. Print All Recipe Names " + "4. Edit A Recipe Name " + "5. Delete A Recipe "

+ " Please select a menu item:");

}

System.out.println("Menu " + "1. Add A Recipe " + "2. Print All Recipe Details "

+ "3. Print All Recipe Names " + "4. Edit A Recipe Name " + "5. Delete A Recipe "

+ " Please select a menu item:");

}

}

}

Below is the class that I am calling from.

package Milestones;

import java.util.ArrayList;

import java.util.Scanner;

class Recipe

{

private String recipeName;

private int servings = 0;

private ArrayList recipeIngredients = new ArrayList<>();

private double totalRecipeCalories = 0.0;

private ArrayList recipeInstructions = new ArrayList<>();

public Recipe()

{

this.recipeName = "";

this.recipeIngredients = new ArrayList();

this.totalRecipeCalories = 0.0;

this.recipeInstructions = new ArrayList();

}

public Recipe(String recipeName, ArrayList recipeIngredients)

{

}

public void setRecipeName(String recipeName)

{

this.recipeName = recipeName;

}

public String getRecipeName()

{

return recipeName;//Accessor

}

public void setServings(int servings)

{

this.servings = servings;

}

public int getServings()

{

return servings;

}

public void setRecipeIngredients(ArrayList recipeIngredients)

{

this.recipeIngredients = recipeIngredients;

}

public ArrayList getRecipeIngredients()

{

return recipeIngredients;

}

public void setRecipeInstructions(ArrayList recipeInstructions)

{

this.recipeInstructions = recipeInstructions;

}

public ArrayList getRecipeInstructions()

{

return recipeInstructions;

}

public void setTotalRecipeCalories(double totalRecipeCalories)

{

this.totalRecipeCalories = totalRecipeCalories;

}

public double getTotalRecipeCalories()

{

return totalRecipeCalories;

}

public class Instructions

{

private ArrayList recipeInstructions = new ArrayList();

public Instructions()

{

this.recipeInstructions = new ArrayList();

}

public Instructions(ArrayList recipeInstructions)

{

this.recipeInstructions = recipeInstructions;

}

}

public Recipe(String recipeName, int servings,

ArrayList recipeIngredients, double totalRecipeCalories,

ArrayList recipeInstructions)

{

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = totalRecipeCalories;

this.recipeInstructions = recipeInstructions;

}

public void printRecipe()

{

double singleServingCalories;

singleServingCalories = totalRecipeCalories / servings;

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

System.out.println("Servings: " + servings);

System.out.println("Ingredients: ");

for(int i = 0; i < recipeIngredients.size(); i++)

{

System.out.println(recipeIngredients.get(i));

}

System.out.println("Each serving has " + singleServingCalories + " calories.");

System.out.println("The total calories in this recipe is " + totalRecipeCalories

+ " calories.");

System.out.println("Instructions:");

for(int i = 0; i < recipeInstructions.size(); i++)

{

System.out.println(recipeInstructions.get(i));

}

}

public Recipe createNewRecipe()

{

double totalRecipeCalories = 0.0;

ArrayList recipeIngredients = new ArrayList();

ArrayList ingredientAmounts = new ArrayList();

boolean addMoreIngredients = true;

boolean addMoreInstructions = true;

String ingredientName;

int ingredientCalories = 0;

double ingredientAmount = 0.0;

String recipeInstruction;

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();

do // do while loop

{

System.out.println("Please enter the ingredient name " +

"or type end if you are finished entering ingredients: ");

scnr.nextLine();

ingredientName = scnr.nextLine();

if (ingredientName.toLowerCase().equals("end"))

{

addMoreIngredients = false;

}

else

{

recipeIngredients.add(ingredientName);

System.out.println("Please enter the ingredient amount: ");

ingredientAmount = scnr.nextDouble();

ingredientAmounts.add(ingredientAmount);

System.out.println("Please enter the ingredient calories: ");

ingredientCalories = scnr.nextInt();//Ingredient Calories are entered

totalRecipeCalories += ingredientCalories * ingredientAmount;

}

}

while (!ingredientName.equals("end"));

Instructions instructions = new Instructions();

do

{

System.out.println("Please enter an instruction in creating the recipe"

+ " or type end if finished: ");

recipeInstruction = scnr.nextLine();

if (recipeInstruction.toLowerCase().equals("end"))

{

addMoreInstructions = false;

}

else

{

instructions.recipeInstructions.add(recipeInstruction);

}

}

while (!recipeInstruction.equals("end"));

Recipe recipe1 = new Recipe(recipeName,

servings, recipeIngredients, totalRecipeCalories, instructions.recipeInstructions);

recipe1.printRecipe();

return recipe1;

}

}

class RecipeBox {

private ArrayList listOfRecipes;

public ArrayList getListOfRecipes() {

return listOfRecipes;

}

public void setListOfRecipes(ArrayList listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

}

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions