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...

  • 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 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...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • 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,...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

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
ADVERTISEMENT