Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have code where i am trying to create a recipe list based on other classes i have built, so far the code works but

I have code where i am trying to create a recipe list based on other classes i have built, so far the code works but the order in which the questions are asked and values are taken in are incorrect, for example i ask ask: Please enter recipe name: Please enter the number of servings for this recipe:

*this is where the issue is*

Please enter the ingredient name or type 'end' if you are finished: Please enter the number of units of the ingredient:

* these questions are asked at the same time instead of individually*

it is stuck in my while loop and i cant figure out why

these are the classes i have used along with the test .java code to test my code

public class Ingredient { private static String nameOfIngredient; private static String unitMeasurement; private static float ingredientAmount; private static int numberOfCaloriesPerUnit; private static int numberOfCups; private static double totalCalories; //Operations: Public Methods //Mutators and Accessors: Set Values at the this. Index and Return the Value //Set and Return Name of Ingredient public void setNameOfIngredient(String nameOfIngredient) { this.nameOfIngredient = nameOfIngredient; } public String getNameOfIngredient() { return nameOfIngredient; } public void setNumberOfCups(int numberOfcups) { this.numberOfCups = numberOfCups; } public int getNumberOfCups() { return numberOfCups; } //Set and Return Unit Measurement public void setUnitMeasurement (String unitMeasurement) { this.unitMeasurement = unitMeasurement; } public String getUnitMeasurement () { return unitMeasurement; } //Set and Return Ingredient Amount public void setIngredientAmount(float ingredientAmount) { this.ingredientAmount = ingredientAmount; } public float getIngredientAmount() { return ingredientAmount; } //Set and Return Number of Calories per Unit public void setNumberCaloriesPerUnit(int numberCaloriesPerUnit) { this.numberOfCaloriesPerUnit = numberCaloriesPerUnit; } public int getNumberCaloriesPerUnit() { return numberOfCaloriesPerUnit; } //Set and Return Total Calories public void setTotalCalories(double totalCalories) { this.totalCalories = totalCalories; } public double getTotalCalories() { return totalCalories; } }

i wanted to include logic in here to ask for measurement type and i have the code it works but do i include it in the main class or can i include this in my ingredient class:

System.out.println("Please Enter the Unit of measurement for this Ingredient, Please only select from the following options: "); System.out.println("Cups, Tbsp, Tsp, Grams"); if (scnr.hasNextLine()) { unitMeasurement = scnr.next(); //use equals() to compare strings instead of == if ((unitMeasurement.equalsIgnoreCase("Cups")) || (unitMeasurement.equalsIgnoreCase("Tbsp")) || (unitMeasurement.equalsIgnoreCase("Tsp")) || (unitMeasurement.equalsIgnoreCase("Grams"))) { System.out.println("You have selected " + unitMeasurement); } else { System.out.println("Please enter one of the preceding options."); } } else { System.out.println("Please enter one of the preceding options."); } //Prompt user for number of units System.out.println("Please enter the number of units of " + nameOfIngredient + " required (between 1 and 100): "); //replaced hasNextInt() here with hasNextFloat() as ingredientAmount is of float type if (scnr.hasNextFloat()) { ingredientAmount = scnr.nextFloat(); if ((ingredientAmount >= 1) && (ingredientAmount <= 100)) { System.out.println(ingredientAmount + " is a valid amount!"); } else if (ingredientAmount < 1) { System.out.println(ingredientAmount + " is less than 1 unit. Sorry, you are out of attempts."); } else{ System.out.println(ingredientAmount + " is greater than 100 units. Sorry, you are out of attempts."); } } else { System.out.println("Error: That is not a number. Try again."); } //Prompt user for calories per unit System.out.println("Please enter the number of calories per unit of " + nameOfIngredient + " (between 1 and 2000): "); if (scnr.hasNextInt()){ numberOfCaloriesPerUnit = scnr.nextInt(); if ((numberOfCaloriesPerUnit >= 1) && (numberOfCaloriesPerUnit <= 2000)) { System.out.println(numberOfCaloriesPerUnit + " is a valid number of calories!" ); } else if (numberOfCaloriesPerUnit < 1) { System.out.println(numberOfCaloriesPerUnit + " is less than 1. Sorry, you are out of attempts."); } else { System.out.println(numberOfCaloriesPerUnit + " is greater than 2000. Sorry, you are out of attempts."); } } else{ System.out.println("Error: That is not a number. Try again."); } //Calculate total calories totalCalories = ingredientAmount * numberOfCaloriesPerUnit; System.out.println(nameOfIngredient + " uses " + ingredientAmount + " number of " + unitMeasurement + "'s" + " which contains " + totalCalories + " total calories."); System.out.println("Ingredient has been successfully added."); }

**************************************

This class is my recipe Class if i were to include methods in my ingredient class like this recipe class would that account for my measurement check?

public class Recipe_M2 { private String recipeName; private int servings; ArrayList recipeIngredients = new ArrayList(); private int totalRecipeCalories; // Creating accessors and mutators for the instance variables 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 setTotalRecipeCalories(int totalRecipeCalories){ this.totalRecipeCalories = totalRecipeCalories; } public int getTotalRecipeCalories(){ return totalRecipeCalories; } // Now we build the constructors one to create a new objectt and one to overload the constructor public Recipe_M2(){ this.recipeName = ""; this.servings = 0; this.recipeIngredients = new ArrayList(); this.totalRecipeCalories = 0; } public Recipe_M2(String recipeName, int servings, ArrayList recipeIngredients, int totalRecipeCalories){ this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = totalRecipeCalories; } // Print recipe method public void printRecipe(){ int singleServingCalories = (totalRecipeCalories / servings); System.out.println("Here is the information associated with you recipe: "); System.out.println("Recipe: "+ recipeName); System.out.println("This recipe serves: "+ servings); System.out.println("These are the ingredients you entered: "+ recipeIngredients); for (int i = 0; i < recipeIngredients.size(); i++) { //For loop to print each array member String ingredient = recipeIngredients.get(i); System.out.println(ingredient); } System.out.println("Each Serving Has " + singleServingCalories + " Calories."); } public Recipe_M2 addNewRecipe(){ int totalRecipeCalories = 0; ArrayList recipeIngredients = new ArrayList(); boolean addMoreIngredients = true; Scanner scnr = new Scanner(System.in); System.out.println("Please enter the name of the recipe: "); String recipeName = scnr.nextLine(); System.out.println("Please enter the number of servings for this recipe: "); int servings = scnr.nextInt(); // Using a do,While loop to add more ingreadients like in Stepping stone 4 do{ System.out.println("Please enter the ingredient name or type 'end' if you are finished: "); String ingredientName = scnr.nextLine(); if(ingredientName.toLowerCase().equals("end")){ //addMoreIngredients = false; break; } else { recipeIngredients.add(ingredientName); addMoreIngredients = true; System.out.println("Please enter the number of units of the ingredient: "); int ingredientAmount = scnr.nextInt(); System.out.println("Please enter the calories per unit of the ingredient: "); int ingredientCalories = scnr.nextInt(); totalRecipeCalories = (ingredientCalories * ingredientAmount); } }while(addMoreIngredients == true); Recipe_M2 recipe1 = new Recipe_M2(recipeName, servings, recipeIngredients, totalRecipeCalories); recipe1.printRecipe(); return recipe1; } } // I chose to create an additional ArrayList where the user // can submit step by step instructions and then print them using printRecipe() /* Public method AddInstructions new String ArrayList "Instructions" create a do,While loop to ask thee user to enter the instruction(step) after the first step i will ask if they want to add more steps if yes, set boolean addMoreIngredients = true if no, set boolean addMoreIngredients = false print all instructions after the user enters no end method */

************************************

This is what i am using to test my recipe class

public class RecipeTest { /** * @param args the command line arguments */ public static void main(String[] args) { final Recipe_M2 newRecipe = new Recipe_M2(); Recipe_M2 addNewRecipe = newRecipe.addNewRecipe(); newRecipe.printRecipe(); }

Please let me know if you have any questions regarding my code i will answer the best i can.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Answer To resolve this issue you need to ensure consistent handling of input types and properly cons... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

How would you define supply chain risk?

Answered: 1 week ago

Question

What fundamental changes has the Internet brought to marketing?

Answered: 1 week ago