Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, this is for class intro to object oriented java programming. This project is a recipe collection manager with 3 classes (Ingredient, Recipe & Recipe_Box)

Hello, this is for class intro to object oriented java programming. This project is a recipe collection manager with 3 classes (Ingredient, Recipe & Recipe_Box) and a main driver class (UnitTest). Im showing some errors in the program and I cant figure out why. We've been working on this project all semester and I think I have some code in the wrong spot. Im not sure. My question is if you can overlook this and tell me what I am doing wrong. Next, I need change the nameOfIngredient from an ArrayList to an object so that all the steps will playout when a user adds an ingredient to the list. Last, I need to add an ArrayList of instructions. Only problem is we covered Arrays like 7 weeks ago and I cant remember where to put it and how to execute it. The following is a pseudo-code I code for the ArrayList. Feel free to change it. Please let me know what I did wrong for this too. After the psuedo-code I will add the class files and the main last so you can take a look at the code I have so far. Thank you in advance.

Pseudo-code Instructions

public static addInstructions() {

String steps = (""); // string type

ArrayList Instructions = new ArrayList(); // accepts new list for instructions

boolean addMoreSteps = true; //Boolean value closes or continues list

int i = 0; // declares use of the int i printing method.

System.out.println("Please enter the instruction: "):

String Instructions = scnr.nextLine();

do {

System.out.println("would you like to continue to add steps (y or n)? ");

String reply = scnr.next().toLowerCase();

if (reply.equals("y")){

addMoreSteps = true;

Instructions.add(steps);

}

else{

addMoreSteps = false;

break;

}

while (addMoreSteps == true);

for (i = 0; i < Instructions.size(); i++){

System.out.println(Instructions.get(i));

}

}

}

FIRST CLASS (Ingredient)

import java.util.Scanner;

public class Ingredient {

private String nameOfIngredient;

private String unitMeasurement;

private double ingredientAmount;

private int numberOfCaloriesPerCup;

private double totalCalories;

/**

* @return the nameOfIngredient

*/

public String getNameOfIngredient() {

return nameOfIngredient;

}

/**

* @paramsets the nameOfIngredient

*/

public void setNameOfIngredient(String nameOfIngredient) {

this.nameOfIngredient = nameOfIngredient;

}

/**

* @return the unitMeasurement

*/

public String getUnitMeasurement() {

return unitMeasurement;

}

/**

*

* @param unitMeasurement sets the unitMeasurement

*/

public void setUnitMeasurement(String unitMeasurement) {

this.unitMeasurement = unitMeasurement;

}

/**

* @return the ingredientAmount

*/

public double getIngredientAmount() {

return ingredientAmount;

}

/**

* @param ingredientAmount the ingredientAmount to set

*/

public void setIngredientAmount(float ingredientAmount) {

this.ingredientAmount = ingredientAmount;

}

/**

* @return the numberCaloriesPerCup

*/

public int getNumberOfCaloriesPerCup() {

return numberOfCaloriesPerCup;

}

/**

* @param numberCaloriesPerCup the numberCaloriesPerCup to set

*/

public void setNumberOfCaloriesPerCup(int numberOfCaloriesPerCup) {

this.numberOfCaloriesPerCup = numberOfCaloriesPerCup;

}

/**

* @return the totalCalories

*/

public double getTotalCalories() {

return totalCalories;

}

/**

* @param totalCalories the totalCalories to set

*/

public void setTotalCalories(double totalCalories) {

this.totalCalories = totalCalories;

}

public Ingredient() {

this.nameOfIngredient = ("");

this.unitMeasurement = ("");

this.ingredientAmount = 0;

this.numberOfCaloriesPerCup = 0;

this.totalCalories = 0.0;

}

public Ingredient(String nameOfIngredient, String unitMeasurement, double ingredientAmount,

int numberCaloriesPerCup, double totalCalories) {

this.nameOfIngredient = nameOfIngredient;

this.unitMeasurement = unitMeasurement;

this.ingredientAmount = ingredientAmount;

this.numberOfCaloriesPerCup = numberOfCaloriesPerCup;

this.totalCalories = totalCalories;

}

public Ingredient addIngredient(String tempNameOfIngredient) {

tempNameOfIngredient = tempNameOfIngredient;

double tempIngredientAmount = -1;

String tempUnitMeasurement = null;

int tempNumberOfCaloriesPerCup;

double tempTotalCalories;

final int MAX_AMT = 100;

Scanner scnr = new Scanner(System.in);

System.out.println("Please enter the name of the ingredient: ");

tempNameOfIngredient = scnr.next();

System.out.println("Please enter the ingredient amount of " + nameOfIngredient + " we'll need: ");

tempIngredientAmount = scnr.nextDouble();

if (scnr.hasNextInt()) {

ingredientAmount = scnr.nextDouble();

if (ingredientAmount >= 1 && ingredientAmount <= MAX_AMT) {

System.out.println(ingredientAmount + " is a valid amount!");

} else {

System.out.println(ingredientAmount + " is a not valid ingredient amount!");

System.out.println("Please enter another ingredient amount between 1 and 100: ");

ingredientAmount = scnr.nextDouble();

if (ingredientAmount >= 1 && ingredientAmount <= MAX_AMT) {

System.out.println(ingredientAmount + " is a valid amount!");

} else if (ingredientAmount < 1) {

System.out.println(ingredientAmount + " is less than 1. Sorry you are out of tries.");

} else {

System.out.println(ingredientAmount + " is larger than 100. Sorry you are out of tries.");

}

}

} else {

System.out.println("Error: That is not a numer. Try again.");

}

System.out.println("Please enter unit of measurement (example: oz, tablespoon)");

tempUnitMeasurement = scnr.next();

System.out.println("Please enter the name of calories per cup: ");

tempNumberOfCaloriesPerCup = scnr.nextInt();

tempTotalCalories = ingredientAmount * numberOfCaloriesPerCup;

Ingredient tempNewIngredient =

new Ingredient(tempNameOfIngredient, tempUnitMeasurement,

tempIngredientAmount, tempNumberOfCaloriesPerCup,

tempTotalCalories);

return tempNewIngredient;

}

}

SECOND CLASS (Recipe)

import java.util.Scanner;

import java.util.ArrayList;

public class Recipe {

private String recipeName;

private int servings;

private ArrayList recipeIngredients;

private double totalRecipeCalories;

/**

* @return the recipeName

*/

public String getRecipeName() {

return recipeName;

}

/**

* @param recipeName the recipeName to set

*/

public void setRecipeName(String recipeName) {

this.recipeName = recipeName;

}

/**

* @return the servings

*/

public int getServings() {

return servings;

}

/**

* @param servings the servings to set

*/

public void setServings(int servings) {

this.servings = servings;

}

/**

* @return the recipeIngredients

*/

public ArrayList getRecipeIngredients() {

return recipeIngredients;

}

/**

* @param recipeIngredients the recipeIngredients to set

*/

public void setRecipeIngredients(ArrayList recipeIngredients) {

this.recipeIngredients = recipeIngredients;

}

/**

* @return the totalRecipeCalories

*/

public double getTotalRecipeCalories() {

return totalRecipeCalories;

}

/**

* @param totalRecipeCalories the totalRecipeCalories to set

*/

public void setTotalRecipeCalories(double totalRecipeCalories) {

this.totalRecipeCalories = totalRecipeCalories;

}

public Recipe() {

this.recipeName = "";

this.servings = 0;

this.recipeIngredients = new ArrayList<>();

this.totalRecipeCalories = 0;

}

public Recipe(String recipeName, int servings, ArrayList recipeIngredients, double totalRecipeCalories) {

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = totalRecipeCalories;

}

public void printRecipe() {

double singleServingCalories = totalRecipeCalories / servings;

System.out.println("Recipe: " + getRecipeName());

System.out.println("Yield: " + getServings() + " servings");

System.out.println("Ingredients:");

/*

for (Ingredient currentIngredient: recipeIngredients)

System.out.println(currentIngredient.getIngredientName());

*/

for (int i = 0; i < recipeIngredients.size(); i++) {

Ingredient currentIngredient = recipeIngredients.get(i);

String currentIngredientName = currentIngredient.getIngredientName();

System.out.println(currentIngredientName);

}

System.out.println("Total Calories per serving: " + singleServingCalories);

}

public Recipe addNewRecipe() { //Comment out for Stepping Stone 6

//public SteppingStone5_Recipe createNewRecipe() {//uncomment for Stepping Stone 6

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("How many servings: ");

int servings = scnr.nextInt();

do {

System.out.println("Please enter the ingredient name or type 'e' if you are done: ");

String ingredientName = scnr.next();

if (ingredientName.toLowerCase().equals("e")) {

addMoreIngredients = false;

}

else {

Ingredient tempIngredient = new Ingredient().enterNewIngredient(ingredientName);

recipeIngredients.add(tempIngredient);

}

} while (addMoreIngredients);

for (int i = 0; i < recipeIngredients.size(); i++) {

Ingredient currentIngredient = recipeIngredients.get(i);

float ingredientAmount = currentIngredient.getIngredientAmount();

int ingredientCalories = currentIngredient.getIngredientCalories();

double ingredientTotalCalories = ingredientAmount * ingredientCalories;

totalRecipeCalories += ingredientTotalCalories;

}

Recipe newRecipe = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);

//newRecipe.printRecipe();//comment out for Stepping Stone 6

return newRecipe; //uncomment for Stepping Stone 6

}

}

THIRD CLASS (Recipe_Box)

import java.util.Scanner;

import java.util.ArrayList;

public class Recipe_Box {

private ArrayList listOfRecipes;

/**

* @param args the command line arguments

*/

/**

* @return the listOfRecipes

*/

public ArrayList getListOfRecipes() {

return listOfRecipes;

}

/**

* @param listOfRecipes the listOfRecipes to set

*/

public void setListOfRecipes(ArrayList listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

public Recipe_Box() {

this.listOfRecipes = new ArrayList();

}

public Recipe_Box(ArrayList listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

public void printAllRecipeDetails(String selectedRecipeName) {

for (int i = 0; i < listOfRecipes.size(); i++) {

if (listOfRecipes.get(i).getRecipeName().equals(selectedRecipeName)) {

listOfRecipes.get(i).printRecipe();

}

}

}

public void printAllRecipeNames() {

for (Recipe currentRecipe : listOfRecipes)) {

System.out.println(currentRecipe.getRecipeName());

};

}

public void addNewRecipe() {

listOfRecipes.add(new Recipe().createNewRecipe());

}

public static void main(String[] args) {

Recipe_Box myRecipeBox = new 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();

if (input == 1) {

myRecipeBox.addNewRecipe();

} else if (input == 2) {

System.out.println("Which recipe? ");

String selectedRecipeName = menuScnr.next();

myRecipeBox.printAllRecipeDetails(selectedRecipeName);

} else if (input == 3) {

for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {

System.out.println((j + 1) + ": " + myRecipeBox.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:");

}

}

}

MAIN CLASS (UnitTest)

import java.util.ArrayList;

import java.util.Scanner;

public class UnitTest {

/**

* @param args the command line arguments

*/

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 name of the recipe: ");

String recipeName = scnr.nextLine();

System.out.println("Number of servings? ");

int servings = scnr.nextInt();

do {

Ingredient ingredient = new Ingredient();

System.out.println("Please enter the ingredient name ");

System.out.print("or type 'end' if you are finished entering ingredients: ");

String ingredientName = scnr.next();

if (ingredientName.toLowerCase().equals("end")) {

addMoreIngredients = false;

} else {

ingredient.setNameOfIngredient(ingredientName);

System.out.println("Please enter the type of unit for the ingredient: ");

String measurment = scnr.next(); //validates there is an entry for unit measurement

ingredient.setUnitMeasurement(measurment);

recipeIngredients.add(ingredient);

System.out.println("Please enter the ingredient amount: ");

float ingredientAmount = scnr.nextFloat();

ingredient.setIngredientAmount(ingredientAmount);

System.out.println("Please enter the ingredient Calories: ");

int ingredientCalories = scnr.nextInt();

ingredient.setNumberOfCaloriesPerCup(ingredientCalories);

//Add the total Calories from this ingredient

totalRecipeCalories = totalRecipeCalories + (ingredientCalories * ingredientAmount);

ingredient.setTotalCalories(totalRecipeCalories);

}

} while (addMoreIngredients); //looping to taking multiple ingredients from user until typed as "end"

Recipe recipe = new Recipe(recipeName,

servings, recipeIngredients, totalRecipeCalories);

Recipe_Box recipeBox = new Recipe_Box();

recipeBox.addNewRecipe(Recipe);

recipeBox.printAllRecipeDetails(Recipe);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions