*Please Add Inline Comments Directed toward software engineers about design deci
ID: 3910401 • Letter: #
Question
*Please Add Inline Comments Directed toward software engineers about design decisions to facilitate the programs ongoing maintenance*
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Eli Mishkit
*/
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) {
this.listOfRecipes = listOfRecipes;
}
public SteppingStone6_RecipeBox() {
this.listOfRecipes = new ArrayList<>();
}
/**
* 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(SteppingStone5_Recipe recipe: listOfRecipes) {
if(recipe.getRecipeName().equalsIgnoreCase(selectedRecipe)) {
recipe.printRecipe();
return;
}
}
System.out.println("No Recipe found with name: " + selectedRecipe);
}
void printAllRecipeNames(){
for(SteppingStone5_Recipe selectedRecipe: listOfRecipes) {
System.out.println(selectedRecipe.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() {
//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 code should remain uncommented when using the
* SteppingStone6_RecipeBox_tester.
*
* Comment out this section when using the
* public static main() method
*/
if (input == 1) {
//new Recipe();
} 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:");
}
}
}
Explanation / Answer
import java.util.ArrayList; import java.util.Scanner; public class SteppingStone6_RecipeBox { // as there can be multiple recipes in a box, we are taking it as arraylist // because arraylist gives us option of random access. private ArrayList listOfRecipes; // Parameterized Constructors public SteppingStone6_RecipeBox( ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; } // Default constructor public SteppingStone6_RecipeBox() { // create new arraylist, as none is passed from user side this.listOfRecipes = new ArrayList(); } // Getter methods public ArrayList getListOfRecipes() { return listOfRecipes; } // Setter methods public void setListOfRecipes(ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; } // This method prints a selected recipe details // If recipe is not found in the box, we return a error message. void printAllRecipeDetails(String selectedRecipe){ // iterate on all recipes for(SteppingStone5_Recipe recipe: listOfRecipes) { // if found matching one if(recipe.getRecipeName().equalsIgnoreCase(selectedRecipe)) { recipe.printRecipe(); return; } } System.out.println("No Recipe found with name: " + selectedRecipe); } // this method iterates on list of recipes and print their details void printAllRecipeNames(){ for(SteppingStone5_Recipe selectedRecipe: listOfRecipes) { System.out.println(selectedRecipe.getRecipeName()); } } // this method is used to add a new recipe into the box public void addRecipe(SteppingStone5_Recipe tmpRecipe) { listOfRecipes.add(tmpRecipe); } public void menu() { // Scanner to take user choices as input Scanner menuScnr = new Scanner(System.in); // show menu choices to user System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:"); // Till the time user input is available, keep working while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) { // take user choice int input = menuScnr.nextInt(); // If user wants to create a new Recipe if (input == 1) { //new Recipe(); } else if (input == 2) { // if user wants to see the details of some recipe System.out.println("Which recipe? "); String selectedRecipeName = menuScnr.next(); printAllRecipeDetails(selectedRecipeName); } else if (input == 3) { // if user wants to see the names of all recipes in the box for (int j = 0; jRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.