How do I add: a. print out a recipe with amounts adjusted for a different number
ID: 3740801 • Letter: H
Question
How do I add:
a. print out a recipe with amounts adjusted for a different number of servings
b. create an additional list or ArrayList that allow users to insert step-by-step recipe instructions
c. conversion of ingredient amounts from English to metric (or vice versa)
d. calculate select nutritional information
e. calculate recipe cost
To the following code:
package SteppingStone_5
import java.util.ArrayList;
import java.util.Scanner;
public class SteppingStone5_Recipe {
private String recipeName;
private int servings;
private ArrayList recipeIngredients;
private double totalRecipeCalories = 0;
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 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 SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
}
public SteppingStone5_Recipe(String recipeName, int servings,
ArrayList recipeIngredients, double totalRecipeCalories)
{
this.servings = 0;
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() {
int singleServingCalories = (int) (totalRecipeCalories/servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
recipeIngredients.stream().forEach((ingredient) -> {
System.out.println(ingredient);
});
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
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();
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 {
recipeIngredients.add(ingredientName);
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);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
recipe1.printRecipe();
}
}
Explanation / Answer
import java.util.ArrayList;
/**
* Ingredient class :
* items that will be stored in each recipe
*
*/
public class Ingredient {
//variables
private String ingredientName;
private double ingredientAmount;
private String unitMeasurement;
private double totalCalories;
public Ingredient() {
}
//constructor
public Ingredient(String ingredientName, double ingredientAmount, String unitMeasurement, double totalCalories) {
super();
this.ingredientName = ingredientName;
this.ingredientAmount = ingredientAmount;
this.unitMeasurement = unitMeasurement;
this.totalCalories = totalCalories;
}
//Seter 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);
}
}
import java.util.ArrayList;
/**
* Recipe class :
* create one recipe
*
*/
public class Recipe {
//variables
private ArrayList<Ingredient> ingredientList;
private ArrayList<String> instructions;
private String recipeName;
private double totalRecipeCalories;
//constructor
public Recipe(String recipeName) {
this.recipeName = recipeName;
this.ingredientList = new ArrayList<>();
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("Reciepe 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();
}
}
import java.util.ArrayList;
/**
* ReciepeBox class :
* collection of recipe
*
*/
public class ReciepeBox {
private ArrayList<Recipe> recipes;
public ReciepeBox() {
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 printAllRecipies(){
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("Anchovies",20,"gram",300);
recipe1.addIngredient(ingredient1);
Recipe recipe2 = Recipe.createNewRecipe("Ramen");
Ingredient ingredient2 = new Ingredient("Noodles",50,"gram",200);
recipe2.addIngredient(ingredient2);
// add in box
ReciepeBox box = new ReciepeBox();
box.addItem(recipe1);
box.addItem(recipe2);
box.printAllRecipies();
}
}
Sample OUTPUT:
Recipe 1
Reciepe contains:
1
ingredientName Anchovies
ingredientAmount 20.0
totalCalories 300.0
unitMeasurement gram
Recipe 2
Reciepe contains:
1
ingredientName Noodles
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.