Hello,
The following JAVA Recipe Box class needs a method to access the custom method I created in the Recipe Items class. Honestly I do not understand what exactly I am doing wrong because, the Recipe Box does not run as expected. Box classes are included. Please help and include explanation.
Thank you.
package recipemanager;
import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner;
public class RecipeBox { private static ArrayList listOfRecipes = new ArrayList<>(); private static PrintStream PrintStream;
/** * * @return */ public ArrayList getListOfRecipes() { return listOfRecipes; }
public void setListOfRecipes(ArrayList listOfRecipes) { RecipeBox.listOfRecipes = listOfRecipes;//INFO NEXT REVIEW }
public void Recipe() { this.listOfRecipes = new ArrayList<>(); }
public void RecipeBox(ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; }
public void printAllRecipeDetails() { RecipeItems tmpRecipe = new RecipeItems();
int i = 0;
for (i = 0; i < listOfRecipes.size(); i++) { tmpRecipe.printRecipe(); } }
public void printAllRecipeNames() {
int i = 0;
for (i = 0; i < listOfRecipes.size(); i++) {
System.out.println(listOfRecipes.get(i)); } }
public void addNewRecipe() {
RecipeItems tmpRecipe2 = new RecipeItems();
listOfRecipes.add(tmpRecipe2.addNewRecipe()); }
public void deleteRecipe(){ Scanner deleteScnr = new Scanner(System.in); String recipeToremove = deleteScnr.next(); for (int h = 0; h < listOfRecipes.size(); h++){ if (listOfRecipes.get(h).recipeName.equals(recipeToremove)){ listOfRecipes.remove(h); break; } } } public static void main(String[] args) { // Create Recipe Box 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. Remove Recipe " + " Please select a menu item:");
while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {
int input = menuScnr.nextInt();
if (input == 1) {
myRecipeBox.newRecipe();
} else if (input == 2) {
System.out.println("Which recipe? ");
String selectedRecipeName = menuScnr.next();
RecipeItems selectedRecipe = new RecipeItems();
selectedRecipe.setRecipeName(selectedRecipeName);
myRecipeBox.printAllRecipeDetails(selectedRecipe);
} else if (input == 3) { for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {
System.out.println((j + 1) + ":" + myRecipeBox.listOfRecipes.get(j).getRecipeName()); } } else if (input ==4){ myRecipeBox.deleteRecipe(); }
else {
System.out.println(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + "4. Remove Recipe " + " Please select a menu item:"); }
System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + "4. Remove Recipe " + " Please select a menu item:"); }
}
}
OUTPUT:
WHEN RUN THE CODE TROWS AN EXCEPTION... IF THE USER ENTER OPTION 1 NOTHING HAPPENS... I GUESS THE ADDNEWRECIPE SHOULD BE INCLUDED IN THE RecipeItems class but I do not understand where or why...
SECOND CLASS:
package recipemanager;
import java.util.ArrayList; import java.util.Scanner;
public class RecipeItems { String recipeName; private int servings; ArrayList recipeIngredients = new ArrayList(); ArrayList recipeDetails; //Storage of amounts/units private int totalRecipeCalories; public void setRecipeName(String recipeName){ this.recipeName = recipeName; } public String getRecipeName(){ return recipeName; } 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 setRecipeDetails(ArrayList recipeDetails){ this.recipeDetails = recipeDetails; } public ArrayList getRecipeDetails(){ return recipeDetails; } public void setTotalRecipeCalories(int totalRecipeCalories){ this.totalRecipeCalories = totalRecipeCalories; } public int getTotalRecipeCalories(){ return totalRecipeCalories; } public RecipeItems() { this.recipeName = ""; this.servings = 0; this.recipeIngredients = new ArrayList<>(); this.recipeDetails = new ArrayList<>(); this.totalRecipeCalories = 0; } public RecipeItems(String recipeName, int servings, ArrayList recipeDetails, ArrayList recipeIngredients, double totalRecipeCalories){ this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.recipeDetails = recipeDetails; this.totalRecipeCalories = (int) totalRecipeCalories; } //print ingredient adjusted amount per required servings public void printAdjustedAmt(){ System.out.println(" How many servings do you require? "); Scanner scnr = new Scanner(System.in); int serves = scnr.nextInt(); // Adjusted amount ingredients list ArrayList amountAdj = new ArrayList(); // to get ingredients amount from recipeDetails for (String string : recipeDetails){ //Amount form recipeDetails String quantityInString = string.split(" ") [0]; double amount = Double.parseDouble(quantityInString); double amountPerServings = amount / servings; amountAdj.add(amountPerServings * serves + " " + string.split(" ") [1]); } System.out.println("Recipe: " + recipeName); System.out.println("Ingredients: "); for (int i = 0; i < recipeIngredients.size(); i++) { String ingredient = recipeIngredients.get(i); String amountAndMeasurement = amountAdj.get(i); System.out.println(ingredient + " " + amountAndMeasurement); } } public void addRecipeDetails(){ String details = ""; boolean addMoreRecipeInstructions = true; Scanner scnr = new Scanner(System.in); do { System.out.println("Please input a recipe instruction or type end"); details = scnr.nextLine(); if (details.toLowerCase().equals("end")) { addMoreRecipeInstructions = false; } else { recipeDetails.add(details); addMoreRecipeInstructions = true; } } while (addMoreRecipeInstructions == true); } public void printRecipe() { int singleServingCalories = (totalRecipeCalories / servings); System.out.println("Recipe: " + recipeName); System.out.println("Serves: " + servings); System.out.println("Ingredients: "); for (int i = 0; i recipeIngredients = new ArrayList(); ArrayList recipeDetails = 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; break; } else { recipeIngredients.add(ingredientName); addMoreIngredients = true; String unitMeasurement = ""; while (true){ System.out.println("Please enter the unit of measurement for this ingredient, only one of the following is valid: "); System.out.println("Cups, Tbsp, Tsp"); unitMeasurement = scnr.next(); if ((unitMeasurement.equalsIgnoreCase("Cups")) || (unitMeasurement.equalsIgnoreCase("Tbsp")) || (unitMeasurement.equalsIgnoreCase("Tbs"))){ System.out.println("You have selected " + unitMeasurement); break; } else{ System.out.println("Please enter one of the preceding options."); } } System.out.println("Please enter the ingredient amount: "); float ingredientAmount = scnr.nextFloat(); recipeDetails.add(ingredientAmount + " " + unitMeasurement); System.out.println("Please enter the ingredient Calories: "); int ingredientCalories = scnr.nextInt(); totalRecipeCalories = (int) (ingredientCalories * ingredientAmount); } } while (addMoreIngredients == true) ; RecipeItems recipe1; recipe1 = new RecipeItems(recipeName, servings, recipeDetails, recipeIngredients, totalRecipeCalories); recipe1.printRecipe(); recipe1.printAdjustedAmt(); } }