Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to figure out how to allow the user to input a 2nd recipe before the program executes an output? I also want

I am trying to figure out how to allow the user to input a 2nd recipe before the program executes an output?

I also want to provide way for users to reinput data if they enter a value thats not valid.

Can anyone help me out?

The 3 different java files of the program are shown below.

Ingredient.java

package finalproject2;

import java.util.ArrayList;

public class Ingredient {

// variables

/** The ingredient name. */

private String ingredientName;

/** The ingredient amount. */

private double ingredientAmount;

/** The unit measurement. */

private String unitMeasurement;

/** The total calories. */

private double totalCalories;

/**

* Instantiates a new ingredient.

*/

public Ingredient() {

this.ingredientName = "";

this.ingredientAmount = 0;

this.unitMeasurement = "";

this.totalCalories = 0;

}

// constructor

/**

* Instantiates a new ingredient.

*

* @param ingredientName the ingredient name

* @param ingredientAmount the ingredient amount

* @param unitMeasurement the unit measurement

* @param totalCalories the total calories

*/

public Ingredient(String ingredientName, double ingredientAmount, String unitMeasurement, double totalCalories) {

super();

this.ingredientName = ingredientName;

this.ingredientAmount = ingredientAmount;

this.unitMeasurement = unitMeasurement;

this.totalCalories = totalCalories;

}

// getter and setter

/**

* Gets the ingredient name.

*

* @return the ingredient name

*/

public String getIngredientName() {

return ingredientName;

}

/**

* Sets the ingredient name.

*

* @param ingredientName the new ingredient name

*/

public void setIngredientName(String ingredientName) {

this.ingredientName = ingredientName;

}

/**

* Gets the ingredient amount.

*

* @return the ingredient amount

*/

public double getIngredientAmount() {

return ingredientAmount;

}

/**

* Sets the ingredient amount.

*

* @param ingredientAmount the new ingredient amount

*/

public void setIngredientAmount(double ingredientAmount) {

this.ingredientAmount = ingredientAmount;

}

/**

* Gets the unit measurement.

*

* @return the unit measurement

*/

public String getUnitMeasurement() {

return unitMeasurement;

}

/**

* Sets the unit measurement.

*

* @param unitMeasurement the new unit measurement

*/

public void setUnitMeasurement(String unitMeasurement) {

this.unitMeasurement = unitMeasurement;

}

/**

* Gets the total calories.

*

* @return the total calories

*/

public double getTotalCalories() {

return totalCalories;

}

/**

* Sets the total calories.

*

* @param totalCalories the new total calories

*/

public void setTotalCalories(double totalCalories) {

this.totalCalories = totalCalories;

}

/**

* Change measurement.

*/

public void changeMeasuremt() {

if (this.unitMeasurement.equalsIgnoreCase("English")) {

ingredientAmount = 0.035 * ingredientAmount;

} else {

ingredientAmount = ingredientAmount / 0.035;

}

}

// print item detail

/**

* Prints the item details.

*/

public void printItemDetails() {

if(unitMeasurement.equalsIgnoreCase("English")) {

System.out.println("ingredientName " + this.ingredientName);

System.out.println("ingredientAmount " + this.ingredientAmount+" Grams");

System.out.println("totalCalories " + this.totalCalories);

System.out.println("unitMeasurement " + this.unitMeasurement);

}

else {

System.out.println("ingredientName " + this.ingredientName);

System.out.println("ingredientAmount " + this.ingredientAmount+" Ounces");

System.out.println("totalCalories " + this.totalCalories);

System.out.println("unitMeasurement " + this.unitMeasurement);

}

}

}

Recipe.java

package finalproject2;

import java.util.ArrayList;

import java.util.Scanner;

/**

*

* @author 1614984_snhu

*/

public class Recipe {

// variables

private ArrayList recipeIngredients;

private ArrayList instructions;

private String recipeName;

private int servings;

private double totalRecipeCalories;

// constructor

public Recipe() {

this.recipeName = "";

this.recipeIngredients = new ArrayList<>();

this.instructions = new ArrayList<>();

this.servings = 0;

this.totalRecipeCalories = 0.0;

}

public Recipe(String recipeName, int servings, ArrayList ingredients, double totalRecipeCalories2) {

this.recipeName = recipeName;

this.recipeIngredients = ingredients;

this.instructions = new ArrayList<>();

this.servings = servings;

this.totalRecipeCalories = totalRecipeCalories2;

}

// get IngredientList

public ArrayList getIngredientList() {

return recipeIngredients;

}

// set IngredientList

public void setIngredientList(ArrayList recipeIngredients) {

this.recipeIngredients = recipeIngredients;

}

/**

*

* @return the recipeName

*

*/

public String getRecipeName() {

return recipeName;

}

/**

*

* @param recipeName the recipeName to set

*

*/

public void setRecipeName(String recipeName) {

this.recipeName = recipeName;

}

/**

*

* @return the servings

*

*/

public int getServings() {

return servings;

}

/**

*

* @param servings the servings to set

*

*/

public void setServings(int servings) {

this.servings = servings;

}

/**

*

* @return the totalRecipeCalories

*

*/

public double getTotalRecipeCalories() {

return totalRecipeCalories;

}

/**

*

* @param totalRecipeCalories the totalRecipeCalories to set

*

*/

public void setTotalRecipeCalories(double totalRecipeCalories) {

this.totalRecipeCalories = totalRecipeCalories;

}

// add Ingredient in recipe

public boolean addIngredient(Ingredient ingredient) {

this.totalRecipeCalories = this.totalRecipeCalories + ingredient.getTotalCalories();

return this.recipeIngredients.add(ingredient);

}

// remove Ingredient in recipe

public boolean removeIngredient(Ingredient ingredient) {

if (this.recipeIngredients.remove(ingredient)) {

this.totalRecipeCalories = this.totalRecipeCalories - ingredient.getTotalCalories();

return true;

}

else

return false;

}

// remove all Ingredient in recipe

public boolean removeAllIngredient() {

return this.recipeIngredients.removeAll(recipeIngredients);

}

// print Ingredients in recipe

public void printRecipe() {

int singleServingCalories = (int) (totalRecipeCalories / servings);

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

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

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

for (Ingredient ingredient : recipeIngredients) {

ingredient.printItemDetails();

}

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

}

// create new recipe

public static Recipe createNewRecipe(String recipeName, int servings, ArrayList ingredients,

double totalRecipeCalories) {

return new Recipe(recipeName, servings, ingredients, totalRecipeCalories);

}

// insert instruction

public boolean insertInstruction(String instruction) {

return this.instructions.add(instruction);

}

public void printInstruction() {

instructions.toString();

}

}

TestRecipe.java

package finalproject2;

import java.util.ArrayList;

import java.util.Scanner;

public class TestRecipe {

public static void main(String[] args) {

double totalRecipeCalories = 0;

Scanner scnr = new Scanner(System.in);

ArrayList recipeIngredients = new ArrayList<>();

System.out.print("Please enter the recipe name: ");

String recipeName = scnr.nextLine();

System.out.print("Please enter the number of servings: ");

int servings = scnr.nextInt();

System.out.print("How many ingredients you want to add: ");

int numberOfIngredient = scnr.nextInt();

scnr.nextLine();

for (int i = 0; i < numberOfIngredient; i++) {

System.out.println("Enter details of ingredient " + (i + 1));

System.out.print("Enter Ingredient name: ");

String name = scnr.nextLine();

System.out.print("Enter Ingredient Amount: ");

double amount = scnr.nextDouble();

scnr.nextLine();

System.out.print("Enter UnitMeasurement(Enter Metric or English): ");

String unit = scnr.nextLine();

System.out.print("Enter total calories of Ingredient: ");

double totalCalories = scnr.nextDouble();

scnr.nextLine();

totalRecipeCalories += totalCalories;

recipeIngredients.add(new Ingredient(name, amount, unit, totalCalories));

}

Recipe myRecipe = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);

myRecipe.printRecipe();

scnr.close();

}

}

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Students also viewed these Programming questions