Prompt: In Stepping Stone Lab Five, you created a Recipe Class. Now, in Stepping
ID: 3905919 • Letter: P
Question
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
Code:
/*
* 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.
*/
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 " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:");
while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {
System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please 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? ");
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(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please 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? ");
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(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:");
}
*
*/
System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please 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.
*
*/
Stepping Stone 5 (Recipe Class):
package recipe.collection;
import java.util.ArrayList;
import java.util.Scanner;
public class Recipe {
private String recipeName;
private int servings;
private ArrayList<String>recipeIngredients;
private double totalRecipeCalories;
public void main(String[] args){
Recipe r = createNewRecipe();//constructor to create new recipe
//r.printRecipe();//Accessor for the printRecipe() method
//r.createNewRecipe();//Invokes the createNewRecipe() method
r.printRecipeWithDifferentServings(servings);
}
public Recipe() {
this.recipeName = "";
this.servings = 0; //<--- assignment value with appropriate data type
this.recipeIngredients = new ArrayList<String>(); //<-- assignment value for empty ArrayList
this.totalRecipeCalories = 0;
}
//accessor that retrieves the recipe name
public String getRecipeName() {
return recipeName;
}
//mutator that sets the recipe name
public void setRecipeName(String recipeName){
this.recipeName = recipeName;
}
//accessor to retrieve the serving size
public int getServing() {
return servings;
}
//mutator to set the serving size
public void setServing(int serving) {
this.servings = serving;
}
// calls the Array that holds the recipe ingredients
public ArrayList<String> getRecipeIngredients() {
return recipeIngredients;
}
//sets the ingredients held by the array
public void setRecipeIngredients(ArrayList<String> recipeIngredients){
this.recipeIngredients = recipeIngredients;
}
//accessor that calls the total recipe calories
public double getTotalRecipeCalories(){
return totalRecipeCalories;
}
//mutator that sets the total recipe calories
public void setTotalRecipeCalories(double totalRecipeCalories){
this.totalRecipeCalories = totalRecipeCalories;
}
//Recipe class
public Recipe(String recipeName, int servings,
ArrayList<String> recipeIngredients, double totalRecipeCalories)
//<-- use appropriate data type for the ArrayList and the servings arguments
{
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() {
//printRecipeWithDifferentServings(servings);
int singleServingCalories = (int) totalRecipeCalories / servings;
System.out.println("Recipe:" + recipeName + " Serves: " + servings + " Ingredients: ");
for (int i = 0; i < recipeIngredients.size(); i++) {
System.out.print(recipeIngredients.get(i) + getRecipeName() + " ");
System.out.print(recipeIngredients.get(i) + getServing() + " ");
System.out.print(recipeIngredients.get(i) + getTotalRecipeCalories() + " ");
}
System.out.print(".Each serving has " + singleServingCalories + " calories");
}
Recipe createNewRecipe(){
double totalRecipeCalories = 0;
ArrayList <String> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
String reply="";
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: ");
@SuppressWarnings("LocalVariableHidesMemberVariable")
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;
System.out.println("Do you want to continue. Y/N");
reply = scnr.nextLine();
}
} while (!reply.equals("n")) ;
scnr.close();
Recipe recipe1 = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
return recipe1;
}
public void printRecipeWithDifferentServings(int n){
int singleServingCalories = (int) totalRecipeCalories / n;
for (int i = 0; i < recipeIngredients.size(); i++) {
System.out.print(recipeIngredients.get(i) + getRecipeName() + " ");
System.out.print(recipeIngredients.get(i) + getServing() + " ");
System.out.print(recipeIngredients.get(i) + getTotalRecipeCalories() + " ");
}
System.out.println(". Each serving has " + singleServingCalories + " calories");
}
}
/**
* Final Project
*
* For your Final Project:
*
* 1. Modify this code to develop a Recipe class:
* a. change the void main method createNewRecipe() that returns a Recipe
*
* 2. FOR FINAL SUBMISSION ONLY:Change the ArrayList type to an
* Ingredient object. When a user adds an ingredient to the recipe,
* instead of adding just the ingredient name, you will be adding the
* actual ingredient including name, amount, measurement type, calories.
* For the Milestone Two submission, the recipeIngredients ArrayList can
* remain as a String type.
*
* 3. Adapt the printRecipe() method to print the amount and measurement
* type as well as the ingredient name.
*
* 4. Create a custom method in the Recipe class.
* Choose one of the following options:
*
* a. print out a recipe with amounts adjusted for a different
* number of servings
*
* b. create an additional list or ArrayList that allow users to
* insert step-by-step recipe instructions
*
* c. conversion of ingredient amounts from
* English to metric (or vice versa)
*
* d. calculate select nutritional information
*
* e. calculate recipe cost
*
* f. propose a suitable alternative to your instructor
*
*/
Explanation / Answer
package SteppingStones;
import java.util.ArrayList;
import java.util.Scanner;
public class RecipeBox {
/**
* Declare instance variables:
* a private ArrayList of the type SteppingStone5_Recipe named listOfRecipes
*
*/
private ArrayList<Recipe> listOfRecipes;
/**
* Add accessor and mutator for listOfRecipes
*
*/
public ArrayList<Recipe> getListOfRecipes() {
return listOfRecipes;
}
public void setListOfRecipes(ArrayList<Recipe> listOfRecipes){
this.listOfRecipes = listOfRecipes;
}
/**
* Add constructors for the SteppingStone6_RecipeBox()
*
*/
public RecipeBox()
{
listOfRecipes = new ArrayList<Recipe>();
}
public RecipeBox(ArrayList<Recipe> listOfRecipes)
{
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
*
*/
void printAllRecipeDetails(String selectedRecipe)
{
for (int i = 0; i < listOfRecipes.size(); i++) // traverse ArrayList
{// fetches the element at i position and compare its name with the aargument string
if(listOfRecipes.get(i).getRecipeName().equals(selectedRecipe))
listOfRecipes.get(i).printRecipe(); // if matches then prints Recipes detail
}
}
void printAllRecipeNames()
{
System.out.println("List of Recipes are : ");
for (int i = 0; i < listOfRecipes.size(); i++)
System.out.println(listOfRecipes.get(i).getRecipeName()); // just prints Recipe names
}
void addRecipe()
{
Recipe r= new Recipe();
Recipe tmpRecipe = r.createNewRecipe(); // creates new Recipe
listOfRecipes.add(tmpRecipe); // adds it to the ArrayList
}
/**
* 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 " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:");
while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {
System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please 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) {
addRecipe();
} else if (input == 2) {
System.out.println("Which recipe? ");
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(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please 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? ");
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(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:");
}
*
*/
System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.