In: Computer Science
Looking for code to this stepping stone lab 6
Prompt:
In Stepping Stone Lab Five, you created a Recipe Class. Now, in
Stepping Stone Lab Six, you will focus your skills on developing
your own driver class including custom methods to access elements
of the Ingredient and Recipe classes.
Specifically, you will need to create the following:
The instance variables for the class (listOfRecipes)
Accessors and mutators for the instance variable
Constructors
Three custom methods: printAllRecipeDetails(),
printAllRecipeNames, and addNewRecipe
Guidelines for Submission: This assignment should be submitted as a
Java file.
Extending This Lab for Your Final Project For your final project
submission, you should add a menu item and a method to access the
custom method you developed for the Recipe class based on the
Steppcing Stone Lab Five.
Code given:
package SteppingStones;
import java.util.ArrayList;
public class SteppingStone6_RecipeBox {
/**
* Declare instance variables:
* a private ArrayList of the type
SteppingStone5_Recipe named listOfRecipes
*
*/
/**
* Add accessor and mutator for listOfRecipes
*
*/
/**
* Add constructors for the
SteppingStone6_RecipeBox()
*
*/
/**
* Add the following custom methods:
*
* //printAllRecipeDetails(SteppingStone5_Recipe
selectedRecipe)
* This method should
accept a recipe from the listOfRecipes ArrayList
* recipeName and use
the SteppingStone5_Recipe.printRecipe() method
* to print the
recipe
*
* //printAllRecipeNames() <-- This method should
print just the recipe
* names of each of the
recipes in the listOfRecipes ArrayList
*
* //addRecipe(SteppingStone5_Recipe tmpRecipe) <--
This method should use
* the
SteppingStone5_Recipe.addRecipe() method to add a new
*
SteppingStone5_Recipe to the listOfRecipes
*
*/
/**
* A variation of following menu method should be used
as the actual main
* once you are ready to
submit your final application. For this
* submission and for
using it to do stand-alone tests, replace the
* public void menu()
with the standard
*
public static main(String[] args)
* method
*
*/
public void menu() {
// Create a Recipe Box
//SteppingStone6_RecipeBox
myRecipeBox = new SteppingStone6_RecipeBox(); //Uncomment for main
method
Scanner menuScnr = new Scanner(System.in);
/**
* Print a menu for the user to
select one of the three options:
*
*/
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();
/**
* The code below
has two variations:
*
1. Code used with the SteppingStone6_RecipeBox_tester.
* 2.
Code used with the public static main() method
*
* One of the
sections should be commented out depending on the use.
*/
/**
* This could
should remain uncommented when using the
*
SteppingStone6_RecipeBox_tester.
*
* Comment out
this section when using the
*
public static main() method
*/
if (input == 1)
{
newRecipe();
} else if (input == 2) {
System.out.println("Which recipe?\n");
String selectedRecipeName = menuScnr.next();
printAllRecipeDetails(selectedRecipeName);
} else if (input == 3) {
for (int j = 0; j < listOfRecipes.size();
j++) {
System.out.println((j + 1) + ": " +
listOfRecipes.get(j).getRecipeName());
}
} else {
System.out.println("\nMenu\n" + "1. Add Recipe\n" + "2. Print
Recipe\n" + "3. Adjust Recipe Servings\n" + "\nPlease select a menu
item:");
}
/**
* This could
should be uncommented when using the
*
public static main() method
*
* Comment out
this section when using the
*
SteppingStone6_RecipeBox_tester.
*
if (input == 1)
{
myRecipeBox.newRecipe();
} else if (input == 2) {
System.out.println("Which recipe?\n");
String selectedRecipeName = menuScnr.next();
myRecipesBox.printAllRecipeDetails(selectedRecipeName);
} else if (input == 3) {
for (int j = 0; j <
myRecipesBox.listOfRecipes.size(); j++) {
System.out.println((j + 1) + ": " +
myRecipesBox.listOfRecipes.get(j).getRecipeName());
}
} else {
System.out.println("\nMenu\n" + "1. Add Recipe\n" + "2. Print
Recipe\n" + "3. Adjust Recipe Servings\n" + "\nPlease select a menu
item:");
}
*
*/
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:");
}
}
}
/**
*
* Final Project Details:
*
* For your final project submission, you should add a menu item and
a method
* to access the custom method you
developed for the Recipe class
* based on the Stepping Stone 5
Lab.
*
*/
/***************************************SteppingStone6_RecipeBox.java*************************/
import java.util.ArrayList;
import java.util.Scanner;
public class SteppingStone6_RecipeBox {
/**
* Declare instance variables: a private ArrayList of
the type
* SteppingStone5_Recipe named listOfRecipes
*
*/
private ArrayList<SteppingStone5_Recipe>
listOfRecipes;
/**
* Add accessor and mutator for listOfRecipes
*
*/
public ArrayList<SteppingStone5_Recipe>
getListOfRecipes() {
return listOfRecipes;
}
public void
setListOfRecipes(ArrayList<SteppingStone5_Recipe>
listOfRecipes) {
this.listOfRecipes =
listOfRecipes;
}
/**
* Add constructors for the
SteppingStone6_RecipeBox()
*
*/
public
SteppingStone6_RecipeBox(ArrayList<SteppingStone5_Recipe>
listOfRecipes) {
super();
this.listOfRecipes =
listOfRecipes;
}
/**
* Add the following custom methods:
*
* //printAllRecipeDetails(SteppingStone5_Recipe
selectedRecipe) This method
* should accept a recipe from the listOfRecipes
ArrayList recipeName and use
* the SteppingStone5_Recipe.printRecipe() method to
print the recipe
*
* //printAllRecipeNames() <-- This method should
print just the recipe names of
* each of the recipes in the listOfRecipes
ArrayList
*
* //addRecipe(SteppingStone5_Recipe tmpRecipe) <--
This method should use the
* SteppingStone5_Recipe.addRecipe() method to add a
new SteppingStone5_Recipe
* to the listOfRecipes
*
*/
public void printAllRecipeDetails(String
selectedRecipe) {
for (SteppingStone5_Recipe steppingStone5_Recipe : listOfRecipes) {
if (steppingStone5_Recipe.getRecipeName().equalsIgnoreCase(selectedRecipe)) {
steppingStone5_Recipe.printRecipe();
}
}
}
public void printAllRecipeNames() {
for (SteppingStone5_Recipe steppingStone5_Recipe : listOfRecipes) {
System.out.println(steppingStone5_Recipe.getRecipeName());
}
}
public void addRecipe(SteppingStone5_Recipe tmpRecipe) {
listOfRecipes.add(tmpRecipe);
}
/**
* A variation of following menu method should be used
as the actual main once
* you are ready to submit your final application. For
this submission and for
* using it to do stand-alone tests, replace the public
void menu() with the
* standard public static main(String[] args)
method
*
*/
public void menu() {
// Create a Recipe Box
// SteppingStone6_RecipeBox
myRecipeBox = new SteppingStone6_RecipeBox();
// //Uncomment for main
method
Scanner menuScnr = new
Scanner(System.in);
/**
* Print a menu for the user to
select one of the three options:
*
*/
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();
/**
* The code below
has two variations: 1. Code used with the
*
SteppingStone6_RecipeBox_tester. 2. Code used with the public
static main()
* method
*
* One of the
sections should be commented out depending on the use.
*/
/**
* This could
should remain uncommented when using the
*
SteppingStone6_RecipeBox_tester.
*
* Comment out
this section when using the public static main() method
*/
if (input ==
1) {
newRecipe();
} else if (input
== 2) {
System.out.println("Which recipe?\n");
String selectedRecipeName =
menuScnr.next();
printAllRecipeDetails(selectedRecipeName);
} else if (input
== 3) {
for (int j = 0; j < listOfRecipes.size();
j++) {
System.out.println((j + 1) +
": " + listOfRecipes.get(j).getRecipeName());
}
} else {
System.out.println("\nMenu\n" + "1. Add
Recipe\n" + "2. Print Recipe\n" + "3. Adjust Recipe
Servings\n"
+
"\nPlease select a menu item:");
}
/**
* This could
should be uncommented when using the public static main()
method
*
* Comment out
this section when using the SteppingStone6_RecipeBox_tester.
*
*
*
* if (input ==
1) { myRecipeBox.newRecipe(); } else if (input == 2) {
*
System.out.println("Which recipe?\n"); String selectedRecipeName
=
*
menuScnr.next();
myRecipesBox.printAllRecipeDetails(selectedRecipeName); }
* else if (input
== 3) { for (int j = 0; j <
myRecipesBox.listOfRecipes.size();
* j++) {
System.out.println((j + 1) + ": " +
*
myRecipesBox.listOfRecipes.get(j).getRecipeName()); } } else
{
*
System.out.println("\nMenu\n" + "1. Add Recipe\n" + "2. Print
Recipe\n" + "3.
* Adjust Recipe
Servings\n" + "\nPlease select a menu item:"); }
*
*
*/
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:");
}
}
public void newRecipe() {
double totalRecipeCalories =
0;
Scanner scnr = new
Scanner(System.in);
ArrayList<Ingredient>
recipeIngredients = new ArrayList<>();
System.out.print("Please enter the
recipe name: ");
String recipeName =
scnr.nextLine();
System.out.print("Please enter the
number of servings: ");
int servings =
scnr.nextInt();
System.out.print("How many
ingredients you want to add: ");
int numberOfIngredient =
scnr.nextInt();
scnr.nextLine();
for (int i = 0; i <
numberOfIngredient; i++) {
System.out.println("Enter details of ingredient " + (i + 1));
System.out.print("Enter Ingredient name: ");
String name =
scnr.nextLine();
System.out.print("Enter Ingredient Amount: ");
double amount =
scnr.nextDouble();
scnr.nextLine();
System.out.print("Enter UnitMeasurement: ");
String unit =
scnr.nextLine();
System.out.print("Enter total calories of Ingredient: ");
double
totalCalories = scnr.nextDouble();
scnr.nextLine();
totalRecipeCalories += totalCalories;
recipeIngredients.add(new Ingredient(name, amount, unit,
totalCalories));
}
SteppingStone5_Recipe myRecipe =
new SteppingStone5_Recipe(recipeName, servings,
recipeIngredients,
totalRecipeCalories);
addRecipe(myRecipe);
}
}
/*********************************SteppingStone5_Recipe.java*************************/
import java.util.ArrayList;
/**
*
* Recipe class :
*
* create one recipe
*
*
*
*/
public class SteppingStone5_Recipe {
// variables
private ArrayList<Ingredient> recipeIngredients;
private ArrayList<String> instructions;
private String recipeName;
private int servings;
private double totalRecipeCalories;
// constructor
public SteppingStone5_Recipe() {
this.recipeName = "";
this.recipeIngredients = new ArrayList<>();
this.instructions = new ArrayList<>();
this.servings = 0;
this.totalRecipeCalories =
0.0;
}
public SteppingStone5_Recipe(String recipeName, int
servings, ArrayList<Ingredient> ingredients,
double
totalRecipeCalories2) {
this.recipeName = recipeName;
this.recipeIngredients = ingredients;
this.instructions = new ArrayList<>();
this.servings = servings;
this.totalRecipeCalories = totalRecipeCalories2;
}
// get IngredientList
public ArrayList<Ingredient> getIngredientList() {
return recipeIngredients;
}
// set IngredientList
public void setIngredientList(ArrayList<Ingredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
/**
*
* @return the recipeName
*
*/
public String getRecipeName() {
return recipeName;
}
/**
*
* @param recipeName the recipeName to set
*
*/
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
/**
*
* @return the servings
*
*/
public int getServings() {
return servings;
}
/**
*
* @param servings the servings to set
*
*/
public void setServings(int servings) {
this.servings = servings;
}
/**
*
* @return the totalRecipeCalories
*
*/
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
/**
*
* @param totalRecipeCalories the totalRecipeCalories
to set
*
*/
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
// add Ingredient in recipe
public boolean addIngredient(Ingredient ingredient) {
this.totalRecipeCalories = this.totalRecipeCalories + ingredient.getTotalCalories();
return this.recipeIngredients.add(ingredient);
}
// remove Ingredient in recipe
public boolean removeIngredient(Ingredient ingredient) {
if (this.recipeIngredients.remove(ingredient)) {
this.totalRecipeCalories = this.totalRecipeCalories - ingredient.getTotalCalories();
return true;
}
else
return false;
}
// remove all Ingredient in recipe
public boolean removeAllIngredient() {
return this.recipeIngredients.removeAll(recipeIngredients);
}
// print Ingredients in recipe
public void printRecipe() {
int singleServingCalories = (int) (totalRecipeCalories / servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
for (Ingredient ingredient : recipeIngredients) {
ingredient.printItemDetails();
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
// create new recipe
public static Recipe createNewRecipe(String
recipeName, int servings, ArrayList<Ingredient>
ingredients,
double
totalRecipeCalories) {
return new Recipe(recipeName, servings, ingredients, totalRecipeCalories);
}
// insert instruction
public boolean insertInstruction(String instruction) {
return this.instructions.add(instruction);
}
public void printInstruction() {
instructions.toString();
}
}
/********************************************Ingredient.java*******************/
/**
* Ingredient class :
*
* items that will be stored in each recipe.
*/
public class Ingredient {
// variables
/** The ingredient name. */
private String ingredientName;
/** The ingredient amount. */
private double ingredientAmount;
/** The unit measurement. */
private String unitMeasurement;
/** The total calories. */
private double totalCalories;
/**
* Instantiates a new ingredient.
*/
public Ingredient() {
this.ingredientName = "";
this.ingredientAmount = 0;
this.unitMeasurement = "";
this.totalCalories = 0;
}
// constructor
/**
* Instantiates a new ingredient.
*
* @param ingredientName the ingredient name
* @param ingredientAmount the ingredient amount
* @param unitMeasurement the unit measurement
* @param totalCalories the total calories
*/
public Ingredient(String ingredientName, double
ingredientAmount, String unitMeasurement, double totalCalories)
{
super();
this.ingredientName = ingredientName;
this.ingredientAmount = ingredientAmount;
this.unitMeasurement = unitMeasurement;
this.totalCalories = totalCalories;
}
// getter and setter
/**
* Gets the ingredient name.
*
* @return the ingredient name
*/
public String getIngredientName() {
return ingredientName;
}
/**
* Sets the ingredient name.
*
* @param ingredientName the new ingredient name
*/
public void setIngredientName(String ingredientName)
{
this.ingredientName = ingredientName;
}
/**
* Gets the ingredient amount.
*
* @return the ingredient amount
*/
public double getIngredientAmount() {
return ingredientAmount;
}
/**
* Sets the ingredient amount.
*
* @param ingredientAmount the new ingredient
amount
*/
public void setIngredientAmount(double
ingredientAmount) {
this.ingredientAmount = ingredientAmount;
}
/**
* Gets the unit measurement.
*
* @return the unit measurement
*/
public String getUnitMeasurement() {
return unitMeasurement;
}
/**
* Sets the unit measurement.
*
* @param unitMeasurement the new unit
measurement
*/
public void setUnitMeasurement(String unitMeasurement)
{
this.unitMeasurement = unitMeasurement;
}
/**
* Gets the total calories.
*
* @return the total calories
*/
public double getTotalCalories() {
return totalCalories;
}
/**
* Sets the total calories.
*
* @param totalCalories the new total calories
*/
public void setTotalCalories(double totalCalories)
{
this.totalCalories = totalCalories;
}
/**
* Change measuremt.
*/
public void changeMeasuremt() {
if (this.unitMeasurement.equalsIgnoreCase("English")) {
ingredientAmount = 0.035 * ingredientAmount;
} else {
ingredientAmount = ingredientAmount / 0.035;
}
}
// print item detail
/**
* Prints the item details.
*/
public void printItemDetails() {
if(unitMeasurement.equalsIgnoreCase("English")) {
System.out.println("ingredientName
" + this.ingredientName);
System.out.println("ingredientAmount " + this.ingredientAmount+" Grams");
System.out.println("totalCalories " + this.totalCalories);
System.out.println("unitMeasurement " +
this.unitMeasurement);
}
else {
System.out.println("ingredientName " + this.ingredientName);
System.out.println("ingredientAmount " + this.ingredientAmount+" Ounces");
System.out.println("totalCalories " + this.totalCalories);
System.out.println("unitMeasurement " +
this.unitMeasurement);
}
}
}
Please let me know if you have any doubt or modify the answer, Thanks:)