Hello, The following JAVA Recipe Box class needs a method to access the custom method I created
Question:
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
/** * * @return */ public ArrayList
public void setListOfRecipes(ArrayList
public void Recipe() { this.listOfRecipes = new ArrayList<>(); }
public void RecipeBox(ArrayList
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
Intermediate Accounting IFRS
ISBN: 978-1119372936
3rd edition
Authors: Donald E. Kieso, Jerry J. Weygandt, Terry D. Warfield