Please help with this Java coding question. I particularly need help with the ps
ID: 3737421 • Letter: P
Question
Please help with this Java coding question. I particularly need help with the pseudocode and creating a custom method to add a new feature.
The guidelines for the Recipe class are as follows
In this milestone, you submit the final project version of your Recipe class. Your submission should include the Recipe.java file and a Recipe_Test.java file. Your Recipe class should include the following items:
Instance variables: recipeName, servings, recipeIngredients, and totalRecipeCalories
Accessors and mutators for the instance variables
Constructors
A printRecipe() method
A createNewRecipe() method to build a recipe from user input
Pseudocode for the custom method selected from the list in Stepping Stone Lab Five
The guidelines for the pseudocode are as follows:
Inn this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:
The instance variables for the class (recipeName, serving size, and recipeIngredients)
The methods (the accessors/mutators, the constructors, and the extra print details method) for the class
A custom method to print the recipe out to the console
Your Recipe_Test.java file containing a main() method that:
Uses a constructor to create a new recipe
Accesses the printRecipe() method to print the formatted recipe
Invokes the createNewRecipe() method to accept user input
This is the code I have for the Recipe Class
package Stepping.Stone;
import java.util.ArrayList;
import java.util.Scanner;
public class SteppingStone5_Recipe {
private String recipeName;
private int servings;
private ArrayList recipeIngredients;
private double totalRecipeCalories = 0;
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(ArrayList recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0; //<--- assignment value with appropriate data type
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
}
public SteppingStone5_Recipe(String recipeName, int servings,
ArrayList recipeIngredients, double totalRecipeCalories);
{
this.servings = 0;
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() {
int singleServingCalories = (int) (totalRecipeCalories/servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
recipeIngredients.stream().forEach((ingredient) -> {
System.out.println(ingredient);
});
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
public static void main(String[] args) {
double totalRecipeCalories = 0;
ArrayList recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
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: ");
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);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
recipe1.printRecipe();
}
}
Recipe Test file
Stepping.Stones;
import java.util.ArrayList;
public class SteppingStone5_RecipeTest {
SteppingStone5_Recipe myFirstRecipe = new SteppingStone5_Recipe();
ArrayList<Ingredient> recipeIngredients = new ArrayList();
ArrayList<Ingredient> recipeIngredientsTwo = new ArrayList();
String ingredientName = "Anchovies";
Ingredient tempIngredient = new Ingredient().addIngredient(ingredientName);
recipeIngredients.add(tempIngredient);
SteppingStone5_Recipe mySecondRecipe = new SteppingStone5_Recipe("Pizza", 2, recipeIngredients, 300);
String ingredientNameTwo = "Noodles";
Ingredient tempIngredientTwo = new Ingredient().addIngredient(ingredientNameTwo);
recipeIngredientsTwo.add(tempIngredientTwo);
myFirstRecipe.setRecipeName("Ramen");
myFirstRecipe.setServings(2);
myFirstRecipe.setRecipeIngredients(recipeIngredientsTwo);
myFirstRecipe.setTotalRecipeCalories(150);
myFirstRecipe.printRecipe();
mySecondRecipe.printRecipe();
}
}
Explanation / Answer
Please find my answer.
import java.util.Scanner;
import java.util.ArrayList;
/**
*
*
*
* @author
*
*/
public class SteppingStone5_Recipe {
private String recipeName;
private double totalRecipeCalories;
private ArrayList<String> recipeIngredients;
private int servings;
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
}
public SteppingStone5_Recipe(String recipeName, int servings,
ArrayList recipeIngredients, double totalRecipeCalories) {
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(ArrayList recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public void printRecipe() {
int singleServingCalories = (int) (totalRecipeCalories / servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
for (String ingredient : recipeIngredients) {
System.out.println(ingredient);
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
public static void main(String[] args) {
SteppingStone5_Recipe recipe1 = createNewRecipe();
recipe1.printRecipe();
}
public static SteppingStone5_Recipe createNewRecipe() {
double totalRecipeCalories = 0;
ArrayList<String> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
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: ");
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);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
scnr.close();
return recipe1;
}
}
I made 2 minor changes to the code. Every thing else is good acording to the specs. And please use params while declaring ArrayList as ArrayList<String> if the list is intended to store string data
Please rate my answer if it helped you!!