Prompt: In Stepping Stone Lab Five, you created a Recipe Class. Now, in Stepping
ID: 3905820 • 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.
*
*/
Explanation / Answer
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> list){
listOfRecipes = list;
}
/**
* Add constructors for the SteppingStone6_RecipeBox()
*
*/
SteppingStone6_RecipeBox(){
ArrayList<SteppingStone5_Recipe> listOfRecipes = new ArrayList<Recipe>();
}
/**
* 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 addRecipe(SteppingStone5_Recipe a){
listOfRecipes.add(a);
}
publid void printAllRecipeDetails(SteppingStone5_Recipe selectedRecipe){
System.out.println(selectedRecipe);
}
public void printAllRecipeNames() {
for (int i = 0; i< listOfRecipes.size(); i++){
System.out.println(listOfRecipes.get(i).getName());
}
}
/**
* 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) {
printAllRecipeNames();
/*
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.
*
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.