My objective for this Java program was to help manage a collection manager. I ne
ID: 3910378 • Letter: M
Question
My objective for this Java program was to help manage a collection manager.
I need to implement three classes:
1. Main Recipe items
Your Recipe class will start off similar to your Ingredient class, but you will increase its functionality by modifying it to accept the Ingredient objects, containing
all the details stored in an Ingredient class object. You will also expand the Recipe class by adding recipe-specific methods to your Recipe class.
2. Ingredients part of the recipe
Your Ingredient class will model the items that will be stored in each recipe in your collection. You will give it some basic attributes (of numeric or string types)
and some basic methods (accessors/mutators, printItemDetails(), etc.).
3. Entire colletion of Recipes
The collection should use a list data structure to store the individual items. Your collection class should include methods like addItem(), printItem(), and
deleteItem() that allow you to add, print, or delete items from your collection of items
*Please verify all of objectives have been met*
* I have added Inline Comments for my Ingredient program but have not for the others. Please Add Inline Comments Directed toward software engineers about design decisions to facilitate the programs ongoing maintenance*
/*
* Author : Eli Mishkit
* Created on : 6/28/18
* File Name : Ingredient.java
* Description :
*
* */
import java.util.Scanner;
public class Ingredient {
// Declaring Ingredient class memebers/fields
private String ingredientName;
private float ingredientAmount;
private String unitMeasurement;
private double numberofCalories;
// Creating scanner object to take the input form the keyboard
private Scanner sc = new Scanner(System.in);
// Default COnstructor
public Ingredient() {
// Asking for the ingredient name and storing into the respective field/variable
System.out.println("Please enter the name of the ingredient: ");
ingredientName = sc.nextLine();
// Asking for the number of cups of the specified ingredient and storing into the respective field/variable
System.out.println("Please enter the number of cups of " + ingredientName + " we'll need: ");
boolean valid = false;
// Validating the input
while(!valid) {
try {
ingredientAmount = Float.parseFloat(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
// Asking for the unit of the measurement
System.out.println("Please enter the unit of measure: ");
unitMeasurement = sc.nextLine();
// Asking for the number of calories per cup
System.out.println("Please enter the number of calories per cup: ");
valid = false;
// Validating the input
while(!valid) {
try {
numberofCalories = Integer.parseInt(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
}
// Getters
public String getIngredientName() {
return ingredientName;
}
public float getIngredientAmount(){
return ingredientAmount;
}
public double getNumberofCalories() {
return numberofCalories;
}
public String getUnitMeasurement() {
return unitMeasurement;
}
// Overriding toString method to display useful information about the object
@Override
public String toString() {
return "Ingredient Name = " + ingredientName + " Ingredient Amount = " + ingredientAmount + " Unit Measurement = " + unitMeasurement + " Number of Calories = "
+ numberofCalories;
}
// Driver Method to test the methods
public static void main(String args[]) {
// Creating instance of(object) Ingredient
Ingredient i = new Ingredient();
// Displaying the object's information
System.out.println(" " + i);
}
}
///////////////////////////////////////////////////////////Recipe.java//////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Eli Mishkit
*/
public class Recipe {
private String recipeName;
private double totalRecipeCalories = 0.0;
private ArrayList<RecipeIngredient> recipeIngredients = new ArrayList<>();
private boolean addMoreIngredients = true;
private int servings;
private static double totalCost; // total cost of the recipe
public String getRecipeName() {
return recipeName;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public ArrayList<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients;
}
public boolean isAddMoreIngredients() {
return addMoreIngredients;
}
public int getServings() {
return servings;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
public void setRecipeIngredients(ArrayList<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public void setAddMoreIngredients(boolean addMoreIngredients) {
this.addMoreIngredients = addMoreIngredients;
}
public void setServings(int servings) {
this.servings = servings;
}
public Recipe() {
this.recipeName = "";
this.servings = 0; // <--- assignment value with appropriate data type
this.totalRecipeCalories = 0;
}
public Recipe(String recipeName, int servings, ArrayList<RecipeIngredient> 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() {
double singleServingCalories;
singleServingCalories = this.totalRecipeCalories / this.servings;
/**
* Print the following recipe information: Recipe: <<recipeName>>
* system.out.println("Recipe Name" + this.recipeName); Serves: <<servings>>
* Ingredients: <<Ingredient1>> <<Ingredient2>> ... <<Last Ingredient>>
*
* Each serving has <<singleServingCalories>> Calories.
*
* HINT --> Use a for loop to iterate through the ingredients
*/
System.out.println("Recipe Name" + this.recipeName);
System.out.println("Serves" + this.servings);
System.out.println("Ingredients");
for (RecipeIngredient m : recipeIngredients) {
System.out.println("Name= " + m.getName() + ", Amount= " + m.getAmount() + m.getType().getUnit());
}
System.out.println("Each serving has " + singleServingCalories + " Calories");
System.out.println("Total Cost= " + totalCost + "$");
}
public static void main(String[] args) {
Recipe recipe1 = createNewRecipe();
recipe1.printRecipe();
}
private static Recipe createNewRecipe() {
double totalRecipeCalories = 0.0;
ArrayList<RecipeIngredient> 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: ");
// correct data type & Scanner assignment method for servings variable
int servings = scnr.nextInt();
String ingredientName;
do {
System.out
.println("Please enter the ingredient name or type end if you are finished entering ingredients: ");
ingredientName = scnr.next();
if (ingredientName.toLowerCase().equals("end")) {
addMoreIngredients = false;
break;
} else {
/**
* Add the ingredient name to recipeIngredients
*
*/
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
scnr.nextLine();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
scnr.nextLine();
/**
* Add the total Calories from this ingredient (ingredientCalories *
* ingredientAmount) to the totalRecipeCalories
*
*/
totalRecipeCalories = ingredientCalories * ingredientAmount;
System.out.println("Please enter the unit of measurement");
String unit = scnr.nextLine();
System.out.println("Please enter the cost of Ingredient");
double cost = scnr.nextDouble();
scnr.nextLine();
Measurement type = new Measurement(ingredientAmount, unit);// Create new Measurement unit type
RecipeIngredient r = new RecipeIngredient(ingredientName, ingredientAmount, type, ingredientCalories,
cost);// Add a new object of REcipeIngredient and add it to the list
recipeIngredients.add(r);
totalCost += cost;// Update the cost
System.out.println("Do you want to continue. Y/N");
ingredientName = scnr.nextLine();
}
} while (!ingredientName.equalsIgnoreCase("n"));
scnr.close();
Recipe recipe1 = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
return recipe1;
}
}
///////////////////////////////////////////////////End Recipe.java/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////RecipeIngredient.java////////////////////////////////////////////////////////////////////
public class RecipeIngredient {
private String name;
private float amount;
private Measurement type;
private double calories;
private double cost;
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
@Override
public String toString() {
return "RecipeIngredient [name=" + name + ", amount=" + amount + ", type=" + type + ", calories=" + calories
+ "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getAmount() {
return amount;
}
public RecipeIngredient(String name, float amount, Measurement type, double calories, double cost) {
super();
this.name = name;
this.amount = amount;
this.type = type;
this.calories = calories;
this.cost = cost;
}
public void setAmount(float amount) {
this.amount = amount;
}
public Measurement getType() {
return type;
}
public void setType(Measurement type) {
this.type = type;
}
public double getCalories() {
return calories;
}
public void setCalories(double calories) {
this.calories = calories;
}
}
//////////////////////////////////////////////////////////End RecipeIngredient.java/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////Measurement.java/////////////////////////////////////////////////////////////////////////////
public class Measurement {
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
private double quantity = 0.0;
private String unit = "";// The unit of the Measurement.
/**
* Create Measurement object with Quantity and its unit
*
* @param quantity
* to be measured
* @param unit
* of measurement like grams
*/
public Measurement(double quantity, String unit) {
this.quantity = quantity;
this.unit = unit;
}
@Override
public String toString() {
return "Measurement [quantity=" + quantity + ", unit=" + unit + "]";
}
}
////////////////////////////////////////ENd Measurement.java//////////////////////////////////////////////////////////////////////////////////////////////////
Explanation / Answer
SOlution:
/*
* Author : Eli Mishkit
* Created on : 6/28/18
* File Name : Ingredient.java
* Description :
*
* */
import java.util.Scanner;
public class Ingredient {
// Declaring Ingredient class memebers/fields
private String ingredientName;
private float ingredientAmount;
private String unitMeasurement;
private double numberofCalories;
// Creating scanner object to take the input form the keyboard
private Scanner sc = new Scanner(System.in);
// Default COnstructor
public Ingredient() {
// Asking for the ingredient name and storing into the respective field/variable
System.out.println("Please enter the name of the ingredient: ");
ingredientName = sc.nextLine();
// Asking for the number of cups of the specified ingredient and storing into the respective field/variable
System.out.println("Please enter the number of cups of " + ingredientName + " we'll need: ");
boolean valid = false;
// Validating the input
while(!valid) {
try {
ingredientAmount = Float.parseFloat(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
// Asking for the unit of the measurement
System.out.println("Please enter the unit of measure: ");
unitMeasurement = sc.nextLine();
// Asking for the number of calories per cup
System.out.println("Please enter the number of calories per cup: ");
valid = false;
// Validating the input
while(!valid) {
try {
numberofCalories = Integer.parseInt(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
}
// Getters
public String getIngredientName() {
return ingredientName;
}
public float getIngredientAmount(){
return ingredientAmount;
}
public double getNumberofCalories() {
return numberofCalories;
}
public String getUnitMeasurement() {
return unitMeasurement;
}
// Overriding toString method to display useful information about the object
@Override
public String toString() {
return "Ingredient Name = " + ingredientName + " Ingredient Amount = " + ingredientAmount + " Unit Measurement = " + unitMeasurement + " Number of Calories = "
+ numberofCalories;
}
// Driver Method to test the methods
public static void main(String args[]) {
// Creating instance of(object) Ingredient
Ingredient i = new Ingredient();
// Displaying the object's information
System.out.println(" " + i);
}
}
///////////////////////////////////////////////////////////Recipe.java//////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Eli Mishkit
*/
// Recipe class
public class Recipe {
// Recipe Memebers/fields
private String recipeName;
private double totalRecipeCalories = 0.0;
private ArrayList<RecipeIngredient> recipeIngredients = new ArrayList<>();
private boolean addMoreIngredients = true;
private int servings;
private static double totalCost; // total cost of the recipe
// Getters and Setters
public String getRecipeName() {
return recipeName;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public ArrayList<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients;
}
public boolean isAddMoreIngredients() {
return addMoreIngredients;
}
public int getServings() {
return servings;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
public void setRecipeIngredients(ArrayList<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public void setAddMoreIngredients(boolean addMoreIngredients) {
this.addMoreIngredients = addMoreIngredients;
}
public void setServings(int servings) {
this.servings = servings;
}
// Default COnstructor used to set default values to the fields
public Recipe() {
this.recipeName = "";
this.servings = 0; // <--- assignment value with appropriate data type
this.totalRecipeCalories = 0;
}
// Parameterized constructor to set the object fields during the creation of the object
public Recipe(String recipeName, int servings, ArrayList<RecipeIngredient> 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;
}
// Print Recipe tmethod to print the recipe
public void printRecipe() {
double singleServingCalories;
singleServingCalories = this.totalRecipeCalories / this.servings;
/**
* Print the following recipe information: Recipe: <<recipeName>>
* system.out.println("Recipe Name" + this.recipeName); Serves: <<servings>>
* Ingredients: <<Ingredient1>> <<Ingredient2>> ... <<Last Ingredient>>
*
* Each serving has <<singleServingCalories>> Calories.
*
* HINT --> Use a for loop to iterate through the ingredients
*/
// DIsplaying the whole recipe
System.out.println("Recipe Name" + this.recipeName);
System.out.println("Serves" + this.servings);
System.out.println("Ingredients");
for (RecipeIngredient m : recipeIngredients) {
System.out.println("Name= " + m.getName() + ", Amount= " + m.getAmount() + m.getType().getUnit());
}
System.out.println("Each serving has " + singleServingCalories + " Calories");
System.out.println("Total Cost= " + totalCost + "$");
}
// Driver Prohgram to test our methods
public static void main(String[] args) {
Recipe recipe1 = createNewRecipe();
recipe1.printRecipe();
}
// To create new recipe
private static Recipe createNewRecipe() {
double totalRecipeCalories = 0.0;
ArrayList<RecipeIngredient> 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: ");
// correct data type & Scanner assignment method for servings variable
int servings = scnr.nextInt();
String ingredientName;
do {
System.out
.println("Please enter the ingredient name or type end if you are finished entering ingredients: ");
ingredientName = scnr.next();
if (ingredientName.toLowerCase().equals("end")) {
addMoreIngredients = false;
break;
} else {
/**
* Add the ingredient name to recipeIngredients
*
*/
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
scnr.nextLine();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
scnr.nextLine();
/**
* Add the total Calories from this ingredient (ingredientCalories *
* ingredientAmount) to the totalRecipeCalories
*
*/
totalRecipeCalories = ingredientCalories * ingredientAmount;
System.out.println("Please enter the unit of measurement");
String unit = scnr.nextLine();
System.out.println("Please enter the cost of Ingredient");
double cost = scnr.nextDouble();
scnr.nextLine();
Measurement type = new Measurement(ingredientAmount, unit);// Create new Measurement unit type
RecipeIngredient r = new RecipeIngredient(ingredientName, ingredientAmount, type, ingredientCalories,
cost);// Add a new object of REcipeIngredient and add it to the list
recipeIngredients.add(r);
totalCost += cost;// Update the cost
System.out.println("Do you want to continue. Y/N");
ingredientName = scnr.nextLine();
}
} while (!ingredientName.equalsIgnoreCase("n"));
scnr.close();
Recipe recipe1 = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories);
return recipe1;
}
}
///////////////////////////////////////////////////End Recipe.java/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////RecipeIngredient.java////////////////////////////////////////////////////////////////////
// Recipe Ingredient class
public class RecipeIngredient {
// Recipe fields
private String name;
private float amount;
private Measurement type;
private double calories;
private double cost;
// Parameterized constructor to set the values of the memebrs of the class during the creation of object
public RecipeIngredient(String name, float amount, Measurement type, double calories, double cost) {
super();
this.name = name;
this.amount = amount;
this.type = type;
this.calories = calories;
this.cost = cost;
}
// Geteters and Setters
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
public Measurement getType() {
return type;
}
public void setType(Measurement type) {
this.type = type;
}
public double getCalories() {
return calories;
}
public void setCalories(double calories) {
this.calories = calories;
}
// Overriding toString method to display object information
@Override
public String toString() {
return "RecipeIngredient [name=" + name + ", amount=" + amount + ", type=" + type + ", calories=" + calories
+ "]";
}
}
//////////////////////////////////////////////////////////End RecipeIngredient.java/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////Measurement.java/////////////////////////////////////////////////////////////////////////////
// Measurement class
public class Measurement {
// Measurement class attributes
private double quantity = 0.0;
private String unit = "";// The unit of the Measurement.
// Getters and Setters
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
/**
* Create Measurement object with Quantity and its unit
*
* @param quantity
* to be measured
* @param unit
* of measurement like grams
*/
// Parameterized construtor to set the values to the field during the creation
public Measurement(double quantity, String unit) {
this.quantity = quantity;
this.unit = unit;
}
// Ovverriding toString method to display meaning ful information about the object
@Override
public String toString() {
return "Measurement [quantity=" + quantity + ", unit=" + unit + "]";
}
}
////////////////////////////////////////ENd Measurement.java//////////////////////////////////////////////////////////////////////////////////////////////////
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.