Final Project Details: For your final project submission, you should add a menu
ID: 3909755 • Letter: F
Question
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. Here is the Recipe class code:
package recipe.collection;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author
*/
public class Recipe {
private String recipeName;
private int servings;
private ArrayListrecipeIngredients;
private double totalRecipeCalories;
/**
*
*/
public Recipe() {
this.recipeName = "";
this.servings = 0; //<--- assignment value with appropriate data type
this.recipeIngredients = new ArrayList(); //<-- assignment value for empty ArrayList
this.totalRecipeCalories = 0;
}
/**
*This is the recipe class and in this class the user can print the recipe and create a new recipe as well.
*When Printing the recipe the user will see the Recipe name, the serving size, and the ingredients
*When creating a new recipe the user is asked to name the recipe, how many servings the recipe yields, and then they are asked to add
*the ingredients one at a time. It is during this process that the ingredient class is called.
* @param args
*/
public static void main(String[] args){
Recipe r = createNewRecipe();//constructor to create new recipe
r.printRecipe();//Accessor for the printRecipe() method
}
//
/**
*constructor that retrieves the recipe name
* @return recipeName
*/
public String getRecipeName() {
return recipeName;
}
//
/**
*mutator that sets the recipe name
* @param recipeName
*/
public void setRecipeName(String recipeName){
this.recipeName = recipeName;
}
//
/**
*constructor to retrieve the serving size
* @return serving
*/
public int getServing() {
return servings;
}
//
/**
*mutator to set the serving size
* @param serving size
*/
public void setServing(int serving) {
this.servings = serving;
}
/**
* calls the Array that holds the recipe ingredients
* @return recipe ingredients
*/
public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
//
/**
*sets the ingredients held by the array
* @param recipeIngredients
*/
public void setRecipeIngredients(ArrayList recipeIngredients){
this.recipeIngredients = recipeIngredients;
}
//accessor that calls the total recipe calories
/**
*
* @return Total calories in the recipe
*/
public double getTotalRecipeCalories(){
return totalRecipeCalories;
}
//mutator that sets the total recipe calories
/**
*
* @param totalRecipeCalories
*/
public void setTotalRecipeCalories(double totalRecipeCalories){
this.totalRecipeCalories = totalRecipeCalories;
}
//Recipe class
/**
*
* @param recipeName
* @param servings
* @param recipeIngredients
* @param totalRecipeCalories
*/
public Recipe(String recipeName, int servings,
ArrayList 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);
double singleServingCalories = totalRecipeCalories / servings;
System.out.println("Recipe: " + getRecipeName());
System.out.println("Yield: " + getServing() + " servings");
System.out.println("Ingredients:");
for (int i = 0; i < recipeIngredients.size(); i++) {
Ingredient currentIngredient = recipeIngredients.get(i);
String currentIngredientName = currentIngredient.getNameOfIngredient();
System.out.println(currentIngredientName);
}
System.out.print(".Each serving has " + singleServingCalories + " calories");
}
/**
*
* @return recipe1
*/
public static Recipe createNewRecipe(){
double totalRecipeCalories = 0;
ArrayList recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
String reply="Y";
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 {
Ingredient tempIngredient = new Ingredient().addIngredient();
recipeIngredients.add(tempIngredient);
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;
}
}
Recipe Collection Box Code:
package recipe.collection;
import java.util.ArrayList;
import java.util.Scanner;
/**
*This is a collection spot for all the recipes currently being maintain by the user.
* @author
*/
public class RecipeBox {
private static ArrayList listOfRecipes;
//Accessor Recipe Array
/**
*Gathers the list of recipes and stores them in an array
* @return
*/
public ArrayList getListOfRecipes(){
return listOfRecipes;
}
//Mutator for Array List
/**
*All of the recipes that are currently being stored by the user are set with this mutator
* @param listOfRecipes
*/
public void setListOfRecipes(ArrayList listOfRecipes){
this.listOfRecipes = listOfRecipes;
}
//Constructor for recipe box
void RecipeBox(){
listOfRecipes = new ArrayList();
}
void RecipeBox(ArrayList listOfRecipes)
{
this.listOfRecipes=listOfRecipes;
}
static 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
}
}
}
public 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
}
}
static void addRecipe()
{
Recipe r= new Recipe();
Recipe tmpRecipe = r.createNewRecipe(); // creates new Recipe
listOfRecipes.add(tmpRecipe); // adds it to the ArrayList
}
/**
*If the user enters a 1 they will proceed to add a recipe, if they enter a 2 the list of recipes will be printed, and if they enter a 3 they will be prompted to enter a new recipe
* @param args
*/
public static void main(String[] args ){
// Create a Recipe Box
Scanner menuScnr = new Scanner(System.in);
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();
String myRecipeBox="";
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:");
}
}
}
}
Explanation / Answer
Hello Sir/Madam
It seems like the above code represents the Selection of the new receipies by the customer and addition of new receipies to the menu following the customization of the Customer
Here the menu contains
1. Add Recipe
2. Print All Recipe Details
3. Print All Recipe Names
Where the customer have to select one option among them If the user enters a 1 they will proceed to add a recipe, if they enter a 2 the list of recipes will be printed, and if they enter a 3 they will be prompted to enter a new recipe
The recipe class and in this class the user can print the recipe and create a new recipe as well.
When Printing the recipe the user will see the Recipe name, the serving size, and the ingredients
When creating a new recipe the user is asked to name the recipe, how many servings the recipe yields, and then they are asked to add,the ingredients one at a time. It is during this process that the ingredient class is called.
Hope It works and looking forward to help if have any doubts..
Thank You Sir/Madam.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.