Question

Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditiona...

Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output should show the ingredients with the unit of measurement and it should show the cost per serving. Please help. Everything I've done just breaks the code and gives me errors.

///////////////////////////////////

import java.util.ArrayList;

import java.util.Scanner;

public class Recipe {

   // Declare variables and assign default values
   private String recipeName;

   double servings; // Using double to accommodate partial servings.

   ArrayList<String> recipeIngredients;// ArrayList stores the text of the ingredient added

   private double totalRecipeCalories;;

   private double totalCost;

   public Recipe() {

       this.recipeName = ""; // initialized to a default value

       this.servings = 0.0; // variable is initialized to a default value

       this.recipeIngredients = (null); // ArrayList is written as null- ready to be populated by strings

       this.totalCost = 0.0;
       this.totalRecipeCalories = 0;
       recipeIngredients = new ArrayList<>();

   }

   // Overloaded Constructor
   public Recipe(String recipeName, double servings, ArrayList<String> recipeIngredients, double totalRecipeCalories,
           double totalCost) {

       this.recipeName = recipeName;

       this.servings = servings;

       this.totalRecipeCalories = totalRecipeCalories;

       this.recipeIngredients = recipeIngredients;

       this.totalCost = totalCost;

   }

   // Accessors and mutators {setters and getters)
   // @param setRecipeName value
   public void setRecipeName(String recipeName) {
       this.recipeName = recipeName;
   }

   // @return value
   public String getrecipeName() {
       return recipeName;
   }

   // @param getservings value
   public void setServings(int servings) {
       this.servings = servings;
   }

   // @return getservings value
   public double getservings() {
       return servings;
   }

   // sets the ingredients held by the array
   public void setrecipeIngredients(ArrayList<String> recipeIngredients) {
       this.recipeIngredients = recipeIngredients;
   }

   // @return returns the information in the list
   public ArrayList<String> getrecipeIngredients() {
       return recipeIngredients;
   }

   // @return the value for the
   public double getTotalRecipeCalories() {
       return totalRecipeCalories;

   }

   public void setTotalRecipeCalories(double totalRecipeCalories) {
       this.totalRecipeCalories = totalRecipeCalories;
   }

   public double getTotalCost() {
       return totalCost;
   }

   public void setTotalCost(double totalCost) {
       this.totalCost = totalCost;
   }

   // constructor
   public Recipe(String recipeName, int servings, ArrayList<String> recipeIngredients, double totalRecipeCalories, double totalCost) {
       this.recipeName = recipeName;
       this.servings = servings;
       this.recipeIngredients = recipeIngredients;
       this.totalRecipeCalories = totalRecipeCalories;
       this.totalCost = totalCost;
   }

   // print recipe method will help print recipe
   public void printRecipe() {
       double singleServingCalories;
       singleServingCalories = this.totalRecipeCalories / this.servings;
       System.out.println("Recipe: " + recipeName);// Prints recipeName on standard output.
       System.out.println("Serves: " + servings);// Prints servings on standard output.
       System.out.println("Ingredients:");
       for (String ingredient : recipeIngredients) {
           System.out.println(ingredient);
       }

       System.out.println("Each serving has " + singleServingCalories + " Calories.");
       System.out.println("Total cost of recipe is: $"+totalCost*servings);
       // This expression outputs the calories per serving
   }
   // method that represents a recipe to be called on

   public static Recipe createNewRecipe() {
       double totalRecipeCalories = 0.0; // For decimal values, this data type is generally the default choice
       // this variable will hold an array of the specified type
       ArrayList<String> recipeIngredients = new ArrayList<String>();
       boolean addMoreIngredients = true;// this data type for simple flags that track true/false conditions.

       Scanner scnr = new Scanner(System.in);
       System.out.println("Please enter the recipe name: ");
       // Message prompts user to enter recipe name
       String recipeName = scnr.nextLine();// needs user input
       System.out.println("Please enter the number of servings: ");
       // Asks user to enter number of servings
       int servings = scnr.nextInt();// needs user input
       scnr.nextLine();
       // the statements inside the do block are always executed at least once.
       // The do loop will evaluate the statements inside the loop and then the boolean
       // expression.
       do {
           // prompts the user to enter the ingredient or type "end" to end the program
           System.out.println("Enter the ingredient name or type end if you are finished entering ingredients: ");

           String ingredientName = scnr.nextLine();// needs user input

           if (ingredientName.toLowerCase().equals("end")) { // user enters word "end" addMoreIngredients will result
                                                               // to false

               addMoreIngredients = false;// looping will end if user types "end"

           } else {
               recipeIngredients.add(ingredientName); // ArrayList of ingredient accessed and added

               System.out.println("Please enter the ingredient amount: ");
               // Asks user to enter the amount used
               float ingredientAmount = scnr.nextFloat(); // needs user input

               System.out.println("Please enter the ingredient Calories: ");
               // Asks user to enter the amount used
               int ingredientCalories = scnr.nextInt();// needs user input
               totalRecipeCalories += ingredientCalories * ingredientAmount;
               // Calculates the total Calories
               scnr.nextLine();
           }

       } while (addMoreIngredients);
       // Do-while-loop continues if condition is not false and will continue to run
       // the code until user types "end"
       System.out.print("Pleaae enter the total cost of recipe: ");
       double cost = scnr.nextDouble();
       Recipe recipe1 = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories,cost);
       scnr.close();

       return recipe1;

   }

}

/////////////////////

import java.util.ArrayList;

public class RecipeTest {

   /**
   * @param args
   * the command line arguments
   */
   public static void main(String[] args) {
       // Create first recipe using constructor and setter methods
       ArrayList<String> recipeIngredients = new ArrayList<>();
       recipeIngredients.add("Peanut butter");
       recipeIngredients.add("Jelly");
       recipeIngredients.add("Bread");
       Recipe recipe1 = new Recipe("Peanut butter & jelly sandwich", 2, recipeIngredients, 300, 10.0);

       System.out.println("RECIPE 1");
       recipe1.printRecipe();
       System.out.println();

       // Now change some of the values using mutator methods
       recipe1.setRecipeName("Turkey sandwich");
       recipe1.setServings(5);

       recipe1.setTotalRecipeCalories(500);
       System.out.println(); // blank line
       System.out.println("RECIPE 1 (Modified)");

       // printRecipe() will show these values have changed but
       // recipeIngredients was not modified
       recipe1.printRecipe();

       System.out.println(); // blank line
       System.out.println("RECIPE 2");
       // Create second recipe using our static createNewRecipe() method
       // This is similar to a well-known design pattern called Factory
       Recipe recipe2 = Recipe.createNewRecipe();
       // No need to set hard-coded values here as createNewRecipe() will
       // prompt the user for ingredient info
       recipe2.printRecipe();

       // Create second recipe using our static createNewRecipe() method
       // This is similar to a well-known design pattern called Factory
   }

}

///////////

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

There is no error / issue in your code. It executes fine. Please find below the snapshot.

Also coming to your question if you can add unit measurements; no it will be very complicated and depends on your requirement. As units of measurements can differ from one kind of ingredient to another (i.e. if it is solid or liquid).

D Recipe.java RecipeTestjava 3 ArrayList<string> recipeIngredients new ArrayList()(); recipeIngredients.add( Peanut butter)

Add a comment
Know the answer?
Add Answer to:
Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditiona...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • I'm getting a few errors still under RecipeBox. Would you be able to see what's wrong?...

    I'm getting a few errors still under RecipeBox. Would you be able to see what's wrong? Thanks ------------ package recipecollection; import java.util.ArrayList; public class SteppingStone5_RecipeTest { public static void main(String[] args) { ArrayList<String> recipeIngredients = new ArrayList<>(); recipeIngredients.add("Peanut butter"); recipeIngredients.add("Jelly"); recipeIngredients.add("Bread"); SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe("Peanut butter & jelly sandwich", 2, recipeIngredients, 300, 10.0); System.out.println("RECIPE 1"); recipe1.printRecipe(); System.out.println(); recipe1.setRecipeName("Turkey sandwich"); recipe1.setServings(5); recipe1.setTotalRecipeCalories(500); System.out.println(); System.out.println("RECIPE 1 (Modified)"); recipe1.printRecipe(); System.out.println(); System.out.println("RECIPE 2"); SteppingStone5_Recipe recipe2 = SteppingStone5_Recipe.createNewRecipe(); recipe2.printRecipe(); } } ///////////////// package recipecollection;...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • I would like someone to check my code and help with my for loop to print...

    I would like someone to check my code and help with my for loop to print the recipe. It is incorrect. package SteppingStones; import java.util.Scanner; //Scanner class// import java.util.ArrayList; //ArrayList// import ingredients.Ingredient; /**Gets from package ingredients and class Ingredient * * @author kimbe */ public class SteppingStone5_Recipe {    //Instance Variables// private ArrayList recipeIngredients; private String recipeName; private int servings; private double totalRecipeCalories;                   //Setter and Getters//    public ArrayList getrecipeIngredients() { return recipeIngredients; }...

  • I cannot submit my entire project due to the word count limit. I think my project...

    I cannot submit my entire project due to the word count limit. I think my project looks ok, but my recipe box class and recipe test class is running errors and it may have something to do with the menu portion of the recipe box. Please take a look: "You will create a program that will help you manage a collection of recipes. You will implement three classes: one for the main recipe items, one for the ingredients that are...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT