0

After making a new recipe using the addNewRecipe method located in SteppingStone5_Recipe, the recipe won't store in the recipe list to display when using the print functions in SteppingStone6_RecipeBox.

SteppingStone5_Recipe Class

import java.util.Scanner; import java.util.ArrayList;

public class SteppingStone5_Recipe{

private String recipeName;
private int servings;
private ArrayList<String> recipeIngredients;
private double totalRecipeCalories;

//accessor/getter for recipe name
public String getRecipeName() {
    return recipeName;
}
//mutator/setter for recipe name
public void setRecipeName(String recipeName){
    this.recipeName = recipeName;
}
//accessor for servings
public int getServings(){
    return servings;
}
//mutator for servings 
public void setServings(int servings){
    this.servings = servings;
}
//accessor for recipe ingredients
public ArrayList<String> getRecipeIngredients(){
    return recipeIngredients;
//mutator for recipe ingredients
}
public void setRecipeIngredients(ArrayList<String> recipeIngredients){
    this.recipeIngredients = recipeIngredients;
}
//accessor for the total of recipe calories
public double getTotalRecipeCalories(){
    return totalRecipeCalories;
}
//mutator for Total of recipe calories
public void setTotalRecipeCalories(double totalRecipeCalories){
    this.totalRecipeCalories = totalRecipeCalories;
}

public SteppingStone5_Recipe() {
    this.recipeName = "";
    this.servings = 0;
    this.recipeIngredients = new ArrayList<>();
    this.totalRecipeCalories = 0;
}

public SteppingStone5_Recipe(String recipeName, int servings, 
    ArrayList<String> recipeIngredients, 
    double totalRecipeCalories) {
    this.recipeName = recipeName;
    this.servings = servings;
    this. recipeIngredients = recipeIngredients;
    this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() { //method to print all instances of the recipe 
    double singleServingCalories = totalRecipeCalories / servings;
    System.out.println("Recipe: " + getRecipeName());
    System.out.println("Serves: " + getServings());
    System.out.println("Ingredients:");

    for(int i = 0; i < recipeIngredients.size(); ++i) {
        System.out.println(recipeIngredients.get(i));
    }
    System.out.println("Total Calories per serving: " + 
    singleServingCalories);
}
public SteppingStone5_Recipe addNewRecipe() {
    double totalRecipeCalories = 0;
    ArrayList<String> 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("How many servings: ");
    int servings = scnr.nextInt();

    do {//Loop to add new ingredients until user inputs end
        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 {
                //Add the ingredient name to recipeIngredients
                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();

            //Add the total Calories from this ingredient
            totalRecipeCalories = ingredientCalories * ingredientAmount;
        }

   } while (addMoreIngredients == true) ;
    //create new object recipe1 and use method printRecipe to display all properties of recipe
    SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName, 
        servings, recipeIngredients, totalRecipeCalories);


    return recipe1;
}

}

SteppingStone6_RecipeBox Class

public class SteppingStone6_RecipeBox extends SteppingStone5_Recipe{

private ArrayList<SteppingStone5_Recipe> listOfRecipes;

/**
 * @param args the command line arguments
 */
/**
 * @return the listOfRecipes
 */
public ArrayList<SteppingStone5_Recipe> getListOfRecipes() {
    return listOfRecipes;
}

/**
 * @param listOfRecipes the listOfRecipes to set
 */
public void setListOfRecipes(ArrayList<SteppingStone5_Recipe> listOfRecipes) {
    this.listOfRecipes = listOfRecipes;
}

public SteppingStone6_RecipeBox() {
    listOfRecipes = new ArrayList();
}

public SteppingStone6_RecipeBox(ArrayList<SteppingStone5_Recipe> 
    listOfRecipes) {
    this.listOfRecipes = listOfRecipes;
}

public void printAllRecipeDetails(String selectedRecipeName) {
    for (int i = 0; i < listOfRecipes.size(); i++) {
        if (listOfRecipes.get(i).getRecipeName().equals(selectedRecipeName)) {
            listOfRecipes.get(i).printRecipe();
        }
    }
}

public void printAllRecipeNames() {
    for (SteppingStone5_Recipe currentRecipe : listOfRecipes) {
        System.out.println(currentRecipe.getRecipeName());
    }
}

public void createNewRecipe() {
    listOfRecipes.add(new SteppingStone5_Recipe().addNewRecipe());
}

public static void main(String[] args) {
    SteppingStone6_RecipeBox myRecipeBox = new SteppingStone6_RecipeBox();
    Scanner menuScnr = new Scanner(System.in);
    System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All Recipe 
    Details\n" + "3. Print All Recipe Names\n" + "\nPlease select a menu 
    item:");

while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {
        System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All 
        Recipe Details\n" + "3. Print All Recipe Names\n" + "\nPlease select 
       a menu item:");
        int input = menuScnr.nextInt();
        switch (input) {
            case 1:
                myRecipeBox.addNewRecipe();
                break;
            case 2:
                System.out.println("Which recipe?\n");
                String selectedRecipeName = menuScnr.next();
                myRecipeBox.printAllRecipeDetails(selectedRecipeName);
                break;
            case 3:
                for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {
                    System.out.println((j + 1) + ": " + 
                    myRecipeBox.listOfRecipes.get(j).getRecipeName());
                }   break;
            default:
                System.out.println("\nMenu\n" + "1. Add Recipe\n" + "2. Print 
                Recipe\n" + "3. Adjust Recipe Servings\n" + "\nPlease select 
                a menu item:");
                break;
        }
        System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All 
        Recipe Details\n" + "3. Print All Recipe Names\n" + "\nPlease select 
        a menu item:");    
    }

  }

}

I also can't get the program to accept two-word inputs for the name of the ingredient. The data type is set to String but it doesn't seem to allow ingredients such as "Pasta Sauce" or "Green Beans".

Tony_J
  • 3
  • 2
  • You're shadowing the recipeIngredients ArrayList variable in the addNewRecipe method. Your code also could use some cleaning up and refactoring, including separating your code logic from your user input/output. – Hovercraft Full Of Eels Mar 20 '20 at 22:51
  • So, don't re-declare the variable within that method. Instead, use the variable that has been declared at the class-level. – Hovercraft Full Of Eels Mar 20 '20 at 23:00
  • For the ingredients, use nextLine() to get the entire string. – NomadMaker Mar 20 '20 at 23:30
  • After removing the declaration there seems to be an issue. Now the scanner tool won't work when requesting the user to input the ingredient name. – Tony_J Mar 21 '20 at 22:10

0 Answers0