Specifically, you will need to create the following: The instance variables for the class (recipeName, serving
Question:
Specifically, you will need to create the following:
The instance variables for the class (recipeName, serving size, and recipeIngredients)
The methods (the accessors/mutators, the constructors, and the extra print details method) for the class
A custom method to print the recipe out to the console
This part I believe I have correct. Code below.
package SteppingStones;
import java.util.Scanner;
import java.util.ArrayList;
public class SteppingStone5_Recipe {
private String recipeName;
private int servings;
private ArrayList
private double totalRecipeCalories;
// Accessors and mutators for instance variables
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public ArrayList
return recipeIngredients;
}
public void setRecipeIngredients(ArrayList
this.recipeIngredients = recipeIngredients;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
// Constructors
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList
this.totalRecipeCalories = 0.0;
}
public SteppingStone5_Recipe(String recipeName, int servings, ArrayList
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
// Method to print the recipe
public void printRecipe() {
int singleServingCalories = (int) Math.round(this.totalRecipeCalories / this.servings);
System.out.println("Recipe: " + this.recipeName);
System.out.println("Serves: " + this.servings);
System.out.println("Ingredients:");
for (String ingredient : this.recipeIngredients) {
System.out.println(ingredient);
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
public static void main(String[] args) {
double totalRecipeCalories = 0.0;
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("Please enter the number of servings: ");
int servings = scnr.nextInt();
do {
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 {
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();
totalRecipeCalories += (ingredientCalories * ingredientAmount);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
recipe1.printRecipe();
}
}
This is where I don't know what to do??? Do I create a new Java class called SteppingStone5_RecipeTest?
*To test the functionality of your finished code, use the SteppingStone5_RecipeTest.java file *Replace the public static void main(String[] args) with public SteppingStone5_Recipe createNewRecipe()
Here is the test code given. I need to know exactly what to put in the SteppingStone5_Recipe Test...and do I need to change anything in SteppingStone5_Recipe for the test to work?
package SteppingStones;
import java.util.ArrayList;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class SteppingStone5_RecipeTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create two recipe objects first
SteppingStone5_Recipe myFirstRecipe = new SteppingStone5_Recipe();
ArrayList
ArrayList
String ingredientName = "Anchovies";
Ingredient tempIngredient = new Ingredient().addNewIngredient(ingredientName);
recipeIngredients.add(tempIngredient);
SteppingStone5_Recipe mySecondRecipe = new SteppingStone5_Recipe("Pizza", 2, recipeIngredients, 300);
// Initialize first recipe
String ingredientNameTwo = "Noodles";
Ingredient tempIngredientTwo = new Ingredient().addNewIngredient(ingredientNameTwo);
recipeIngredientsTwo.add(tempIngredientTwo);
myFirstRecipe.setRecipeName("Ramen");
myFirstRecipe.setServings(2);
myFirstRecipe.setRecipeIngredients(recipeIngredientsTwo);
myFirstRecipe.setTotalRecipeCalories(150);
// Print details of both recipes
myFirstRecipe.printRecipe();
mySecondRecipe.printRecipe();
}
}
Operations and Supply Chain Management
ISBN: 978-0078024023
14th edition
Authors: F. Robert Jacobs, Richard Chase