Question
I am having trouble with the following problems in this Java code: 1. I am unable to get menu item #3 to display the objects
I am having trouble with the following problems in this Java code:
1. I am unable to get menu item #3 to display the objects in the ArrayList ListOfRecipes
2. I am unable to elicit user input on the chosen recipe because it always defaults to the first recipe instead of the one created by menu item #1 (create new recipe)
3. For menu item #4, adjust recipe servings, I need to get the ingredients and the ingredient amount to adjust the ingredient amount per serving but am unable to get that to display to calculate the new servings, ingredient, adjusted ingredient amount.
Here is the 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 recipebox;
import java.util.Scanner; import java.util.ArrayList; import java.util.Iterator;
/** * * @author jeremy altice */ public class Ingredient { private ArrayList
/** * * @param ingredientName to set */ public void setIngredientName(String ingredientName) { this.ingredientName = ingredientName; }
/** * * @return amount of ingredient */ public double getIngredientAmount() { return ingredientAmount; }
/** * * @param ingredientAmount to set */ public void setIngredientAmount(double ingredientAmount) { this.ingredientAmount = ingredientAmount; }
/** * * @return calories in ingredient per unit measurement */ public double getIngredientCalories() { return ingredientCalories; }
/** * * @param ingredientCalories to set */ public void setIngredientCalories(double ingredientCalories) { this.ingredientCalories = ingredientCalories; }
/** * * @return total calories in recipe (ingredientAmount * ingredientCalories) */ public double getTotalRecipeCalories() { return totalRecipeCalories; }
/** * * @param totalRecipeCalories to set */ public void setTotalRecipeCalories(double totalRecipeCalories) { this.totalRecipeCalories = totalRecipeCalories; } }
/** * * @author davidamonahan */ public class RecipeBox {
private static String recipeName;
private double totalRecipeCalories;
private ArrayList
private static ArrayList
private double servings;
private double ingredientAmount;
private double ingredientAmountPerServing;
private String unitMeasurement;
/** * * @param recipeName * @param totalRecipeCalories * @param recipeIngredients * @param listOfRecipes * @param servings * @param ingredientAmount * @param ingredientAmountPerServing * @param unitMeasurement */ public RecipeBox(String recipeName, double totalRecipeCalories, ArrayList
/** * lists the recipes created with the program by recipeName */ public RecipeBox() { this.listOfRecipes = new ArrayList<>(); }
/** * returns the list of created recipes by name * @return * */ public ArrayList
/** * uses the list of recipes created in Ultimate Recipe Builder * @param listOfRecipes */ public void setListOfRecipes(ArrayList
this.listOfRecipes = listOfRecipes; }
/** * * adds list of recipes created by Ultimate Recipe Builder to RecipeBox * @param listOfRecipes */ public RecipeBox(ArrayList
/** * prints recipe name, servings, ingredients, total calories per serving for * chosen recipe */ public void printAllRecipeDetails() {
getListOfRecipes().stream().forEach((_recipeName) -> { printRecipe(); }); }
/** * gets the amount of each ingredient * @return ingredient amount */ public double getIngredientAmount() { return ingredientAmount; } /** * gets the ingredient amount for each serving * @return the ingredientAmountPerServing */ public double getIngredientAmountPerServing() { return ingredientAmountPerServing; }
/** * @param ingredientAmountPerServing the ingredientAmountPerServing to set */ public void setIngredientAmountPerServing(double ingredientAmountPerServing) { this.ingredientAmountPerServing = ingredientAmountPerServing; }
/** gets the unit of measurement for ingredient (cup, oz, tsp, etc.) * @return the unitMeasurement */ public String getUnitMeasurement() { return unitMeasurement; }
/** sets the unit of measurement for the ingredient * @param unitMeasurement the unitMeasurement to set */ public void setUnitMeasurement(String unitMeasurement) { this.unitMeasurement = unitMeasurement; }
/** * @param ingredientAmount * @param recipeName * @param servings * @param recipeIngredients * @param totalRecipeCalories */ public RecipeBox(String recipeName, double servings, ArrayList recipeIngredients, double ingredientAmount, double totalRecipeCalories) { // for final project add ingredient amount per serving this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
this.ingredientAmount = ingredientAmount;
this.ingredientAmountPerServing = ingredientAmountPerServing; }
/** * returns name of recipe * @return */ public String getRecipeName() {
return recipeName;
}
/** * name of recipe to set * @param recipeName */ public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
/** * * @return total calories per recipe */ public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
/** * total calories per recipe to set * @param totalRecipeCalories */ public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
/** * * @return list of ingredients for recipe */ public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
/** * sets list of ingredients for recipe * @param recipeIngredients */ public void setRecipeIngredients(ArrayList recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
/** * * @return # servings for recipe */ public double getServings() {
return servings;
}
/** * # of servings to set * @param servings */ public void setServings(double servings) {
this.servings = servings;
}
/** * * @return amount of ingredient */ public double getingredientAmount() {
return getingredientAmount(); }
/** * * @param ingredientAmount */ public void setIngredientAmount(double ingredientAmount) {
this.ingredientAmount = ingredientAmount; }
/** * prints recipe name */ public void printRecipeName() {
//To change body of generated methods, choose Tools | Templates. }
/** * prints list of recipes */ public void printAllRecipeNames() { for (RecipeBox currentRecipe : listOfRecipes) { System.out.println(currentRecipe.getRecipeName());
} }
/** * prints recipe name, servings, ingredients, single serving calories */ public void printRecipe() { // print method to display recipe
double singleServingCalories = (double) (getTotalRecipeCalories() / getServings());
String unitMeasurement = "";
System.out.println("Recipe: " + getRecipeName());
System.out.println("Serves: " + getServings());
getRecipeIngredients().stream().forEach((ingredient) -> { System.out.println(ingredient); //+ " Use " + getIngredientAmountPerServing() this part is under //+ getUnitMeasurement() + " per serving"); }); //FIX ME- need to attach ingredient amount to each ingredient in array!
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
/** * adds new recipe to list of recipes */ public void addNewRecipe() { listOfRecipes.add(new RecipeBox().createNewRecipe()); }
/** * menu options to create recipe, display list of recipes, delete recipe, * adjust ingredient amount per serving, or print details of selected recipe * @param args * */ @SuppressWarnings("unchecked") public static void main(String[] args) { RecipeBox recipe = createNewRecipe();
recipe.printRecipe();
RecipeBox myRecipeBox = new RecipeBox(); ArrayList
System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {
System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); int input = menuScnr.nextInt();
if (input == 1) {
myRecipeBox.createNewRecipe(); listOfRecipes.add(recipeName); recipe.printRecipe(); // FIX ME! the only thing this changes is recipeName, the ingredients from the // first recipe are still there System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe ");
} else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next();
recipe.printRecipe(); System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe ");
} else if (input == 3) { for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) { System.out.println((j + 1) + ": " + myRecipeBox.listOfRecipes.get(j).getRecipeName()); // this will NOT give me any recipes/recipeNames! System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); }
} else if (input == 4){ System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next();
listOfRecipes.stream().forEach((RecipeBox_recipeName) -> { }); recipe.printRecipe(); System.out.println ("How many servings would you like?"); while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) { input = menuScnr.nextInt(); //FIX ME, do not know how to get this because unable to attach ingredient amount //to ingredient }
} else if (input == 5){ System.out.println("Which recipe? "); String selectedRecipeName = menuScnr.next();
listOfRecipes.stream().forEach((RecipeBox_recipeName) -> { }); // FIX ME need to display new list of recipes or confirm that it was removed but // unable because CAN"T display original list! listOfRecipes.remove(recipeName); System.out.println("Please select a menu item: " + "1. Add Recipe " + "2. Print Recipe " + "3. Print Recipe Names " + "4. Adjust Recipe Servings " + "5. Delete Recipe "); } } }
/** * creates a new recipe * @return new recipe */ public static RecipeBox createNewRecipe() {
Scanner scnr = new Scanner(System.in);
ArrayList
System.out.println("Please enter the recipe name: "); String recipeName = scnr.nextLine(); // gets name of recipe
while (!recipeName.matches("[a-zA-Z_]+")) { System.out.println("Invalid name, use letters only");
recipeName = scnr.nextLine(); // validates user input is letters, repeats statement until letters entered }
listOfRecipes.add(recipeName);
System.out.println("Please enter the number of servings: "); // gets # servings
while (!scnr.hasNextDouble()) { // verifies input is pos # System.out.println("Please enter the number of servings" + ". Use numbers only "); // validates user inputs numbers for servings, repeats until numbers entered scnr.nextLine();
} servings = scnr.nextDouble();
scnr.nextLine();
do{ System.out.println("Please enter an ingredient name using letters only. Enter 'e' if you are finished"); // gets first ingredient String ingredientName = scnr.next(); if (ingredientName.toLowerCase().equals("e")) { // breaks loop of adding more ingredients, goes to print recipe addMoreIngredients = false; // allow to add more ingredients } else{ while (!ingredientName.matches(".*[a-z].*")) { System.out.println("Please enter an ingredient name. Use letters only"); ingredientName = scnr.next(); // validates user input is letters, repeats until letters entered } recipeIngredients.add(ingredientName); // adds ingredient to array list System.out.println("Please enter the unit of measure for the ingredient " + "(ex.- cup, oz, tsp, tbsp, pt). Use letters only:)"); String unitMeasurement = scnr.nextLine(); while (!unitMeasurement.matches(".*[a-z].*")) { System.out.println("Please enter the unit of measure for the ingredient " + "(ex.- cup, oz, tsp, tbsp, pt). Use letters only:)"); unitMeasurement = scnr.nextLine(); } System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!"); // gets ingredient amount while (!scnr.hasNextDouble()) { System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!"); // validates user input is numbers for amount, repeats until numbers entered scnr.nextLine(); } ingredientAmount = scnr.nextDouble(); System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!"); // gets # calories per amount of ingredient while (!scnr.hasNextDouble()) { System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!"); // validates user input # calories is numbers, repeats until number entered scnr.nextLine(); } ingredientCalories = scnr.nextDouble(); scnr.nextLine(); totalRecipeCalories += (ingredientCalories * ingredientAmount); }
}while(addMoreIngredients);
listOfRecipes.add(recipeName);
RecipeBox recipe = new RecipeBox(recipeName, servings, recipeIngredients, ingredientAmount, totalRecipeCalories); // creates recipe- name, # servings, ingredients, total calories, calories per serving
return recipe; // prints recipe } }
Here is Ultmate Recipe Builder Class
/* * 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 ultimaterecipebuilder;
import java.util.Scanner; import java.util.ArrayList;
/** * * @author jeremy altice */ public class UltimateRecipeBuilder {
private String recipeName;
private double totalRecipeCalories;
private ArrayList
/** * * @param index * @param element */ public void add(int index, String element) { listOfRecipes.add(0, recipeName); }
private ArrayList
/** * * @param listOfRecipes */ public UltimateRecipeBuilder(ArrayList
/** * * @return */ public ArrayList
/** * * @param listOfRecipes */ public void setListOfRecipes(ArrayList
/** * */ public void printListOfRecipes() {
for (String recipeName : listOfRecipes) { System.out.println(recipeName);
}
}
/** * */ public void printAllRecipeDetails() {
listOfRecipes.stream().forEach((_item) -> { printRecipe(); }); } private double servings;
private double ingredientAmount;
/** * */ public UltimateRecipeBuilder() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
this.ingredientAmount = 0;
}
/** * * @param recipeName * @param servings * @param recipeIngredients * @param totalRecipeCalories */ public UltimateRecipeBuilder(String recipeName, double servings, ArrayList recipeIngredients, double totalRecipeCalories) { // for final project add ingredient amount per serving this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
this.ingredientAmount = ingredientAmount; }
/** * * @return */ public String getRecipeName() {
return recipeName;
}
/** * * @param recipeName */ public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
/** * * @return */ public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
/** * * @param totalRecipeCalories */ public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
/** * * @return */ public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
/** * * @param recipeIngredients */ public void setRecipeIngredients(ArrayList recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
/** * * @return */ public double getServings() {
return servings;
}
/** * * @param servings */ public void setServings(double servings) {
this.servings = servings;
}
/** * * @return */ public double getingredientAmount() {
return getingredientAmount(); }
/** * * @param ingredientAmount */ public void setIngredientAmount(double ingredientAmount) {
this.ingredientAmount = ingredientAmount; }
/** * */ public void printRecipe() { // print method to display recipe at end of code
double singleServingCalories = (double) (totalRecipeCalories / servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
for (String ingredient : recipeIngredients) {
System.out.println(ingredient);
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
/** * * @param args */ public static void main(String[] args) { // method creates new recipe
UltimateRecipeBuilder recipe1 = createNewRecipe();
recipe1.printRecipe();
}
/** * * @return */ public static UltimateRecipeBuilder createNewRecipe() {
Scanner scnr = new Scanner(System.in);
ArrayList
double totalRecipeCalories = 0; // total of calories from all ingredients
double ingredientCalories = 0; // # calories per each ingredient
double ingredientAmount = 0; // amount of ingredient defined by unit of measure (cup, tsp, etc.)
double servings = 0; // # servings per recipe
String unitMeasurement = ""; // how ingredient is measured
String ingredientName = ""; // name of ingredient
System.out.println("Please enter the recipe name: ");
String recipeName = scnr.nextLine(); // gets name of recipe
while (!recipeName.matches("[a-zA-Z_]+")) {
System.out.println("Invalid name, use letters only");
recipeName = scnr.nextLine(); // validates user input is letters, repeats statement until letters entered }
System.out.println("Please enter the number of servings: "); // gets # servings
while (!scnr.hasNextDouble()) { // verifies input is pos # System.out.println("Please enter the number of servings" + ". Use numbers only "); // validates user inputs numbers for servings, repeats until numbers entered
scnr.nextLine(); } servings = scnr.nextDouble();
scnr.nextLine();
System.out.println("Please enter an ingredient name using letters only."); // gets first ingredient ingredientName = scnr.next();
while (!ingredientName.matches(".*[a-z].*")) {
System.out.println("Please enter an ingredient name. Use letters only");
ingredientName = scnr.next(); // validates user input is letters, repeats until letters entered }
recipeIngredients.add(ingredientName); // adds ingredient to array list System.out.println("Please enter the unit of measure for the" + " ingredient (ex.- cup, oz, tsp, tbsp, pt). Use only" + " letters, no numbers: "); // gets unit of measurement for ingredient (cup, oz, tsp. etc.) unitMeasurement = scnr.nextLine();
while (!unitMeasurement.matches( ".*[a-z].*")) { //validates input is letters System.out.println("Please enter the unit of measure for the" + " ingredient (ex.- cup, oz, tsp, tbsp, pt). Use only" + " letters, no numbers: "); //validates user input is letters, repeats until letters entered unitMeasurement = scnr.nextLine(); }
System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!"); // gets ingredient amount while (!scnr.hasNextDouble()) {
System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!"); // validates user input is numbers for amount, repeats until numbers entered scnr.nextLine(); }
ingredientAmount = scnr.nextDouble();
System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!"); // gets # calories per amount of ingredient while (!scnr.hasNextDouble()) {
System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!"); // validates user input # calories is numbers, repeats until number entered
scnr.nextLine(); } ingredientCalories = scnr.nextDouble();
scnr.nextLine();
totalRecipeCalories += (ingredientCalories * ingredientAmount); // calculates total calories for this ingredient-used to work (all of my recipes // are very low cal! FIXME // pseudocode for final project: CALCULATE ingredient amount per serving = // ingredientAmount / servings
System.out.println("Please enter an ingredient name. Use letters only");
ingredientName = scnr.nextLine();
while (!ingredientName.matches(".*[a-z].*")) {
System.out.println("Please enter an ingredient name. Use letters only");
ingredientName = scnr.nextLine(); }
recipeIngredients.add(ingredientName);
System.out.println("Please enter the unit of measure for the" + " ingredient (ex.- cup, oz, tsp, tbsp, pt). Use only" + " letters, no numbers: ");
unitMeasurement = scnr.nextLine();
while (!unitMeasurement.matches(".*[a-z].*")) { //validates input is letters System.out.println("Please enter the unit of measure for the" + " ingredient (ex.- cup, oz, tsp, tbsp, pt). Use only" + " letters, no numbers: ");
unitMeasurement = scnr.nextLine(); }
System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!");
while (!scnr.hasNextDouble()) {
System.out.println("How many " + unitMeasurement + "s of " + ingredientName + "?. Enter numbers only!");
scnr.nextLine(); }
ingredientAmount = scnr.nextDouble();
scnr.nextLine();
System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!");
while (!scnr.hasNextDouble()) {
System.out.println("Please enter the number of calories per " + unitMeasurement + ". Use numbers only!");
scnr.nextLine(); }
ingredientCalories = scnr.nextDouble();
scnr.nextLine();
totalRecipeCalories += (ingredientAmount * ingredientCalories); // pseudocode for final project: CALCULATE ingredient amount per serving = // ingredientAmount / servings
UltimateRecipeBuilder recipe1 = new UltimateRecipeBuilder(recipeName, servings, recipeIngredients, totalRecipeCalories); // creats recipe- name, # servings, ingredients, total calories, calories per serving
// final project will add ingredientAmount per serving return recipe1; // prints recipe } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started