In: Computer Science
For this assignment you will create a set of simple classes that model a cookbook. The cookbook will consist of set or recipes. Each recipe will consist of a set of ingredients and instructions. Your submission will consist of the below three classes and a test driver (the only class with a main()) that exercises the functionality of your system.
You will need to implement the following classes based on the description of their attributes and operations:
Done. Please go through the code and if there is any thing you want differently let me know. WIll look into it. Screenshot of the output is included as well
////////////////////////////////////////Ingredient.java///////////////////////////////////////
package com.java.cookbook;
public class Ingredient {
// the name of the ingredient
private String name;
// the amount of the ingredient
private double quantity;
// the unit of the quantity (e.g, tablespoon, or T, or
Tbl)
private String units;
public Ingredient(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity2) {
this.quantity = quantity2;
}
public String getUnits() {
return units;
}
public void setUnits(String units) {
this.units = units;
}
@Override
public String toString() {
return "[name=" + name + ",
quantity=" + quantity + ", units=" + units + "]";
}
}
///////////////////////////////////////////////End
Ingredient.java///////////////////////////////////////
/////////////////////////////////////////////Recipe.java//////////////////////////////////////////////
package com.java.cookbook;
import java.util.ArrayList;
public class Recipe {
// a list of ingredients
private ArrayList<Ingredient> ingredients;
// the category of the recipe
private String category;
// the name of the recipe
private String name;
// Constructor with one parameter-creator to supply
the name of the recipe in
// order to create one
public Recipe(String name) {
this.name = name;
this.ingredients = new
ArrayList<>();
}
public ArrayList<Ingredient> getIngredients()
{
return ingredients;
}
public void addIngredient(Ingredient ingredient)
{
this.ingredients.add(ingredient);
}
public void removeIngredient(Ingredient ingredient)
{
if
(ingredients.contains(ingredient)) {
ingredients.remove(ingredient);
}
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// This add new ingredient to the list
public void addIngredient(String name, double
quantity, String units) {
Ingredient in = new
Ingredient(name);
in.setUnits(units);
in.setQuantity(quantity);
ingredients.add(in);
}
@Override
public String toString() {
String res = "";
for (int i = 0; i <
ingredients.size(); i++) {
res += "
"+ingredients.get(i).toString() ;
}
res +="]";
return "category=" + category + ",
name=" + name + " Ingredients = " + res ;
}
}
//////////////////////////////////////////ENd
Recipe.java////////////////////////////////////////
/////////////////////////////////////////////CookBook.java///////////////////////////////////////
package com.java.cookbook;
import java.util.ArrayList;
public class CookBook {
// a list of recipes
private ArrayList<Recipe> reciepes;
// the name of the cookbook
private String name;
// Constructor-creator to supply the name of the
cookbook in order to create one
public CookBook(String name) {
this.name = name;
this.reciepes = new
ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Recipe> getReciepes()
{
return reciepes;
}
public void addReciepe(Recipe reciepe) {
this.reciepes.add(reciepe);
}
@Override
public String toString() {
String res = " ";
for (int i = 0; i <
reciepes.size(); i++) {
res += " " +
"Recipe " + (i + 1) + "[" + reciepes.get(i).toString() +" ";
}
res += "]";
return "CookBook name = " + name +
" reciepes=" + res;
}
// find recipes by name. If there is no recipe with
this name the method returns
// null
public ArrayList<Recipe> findReciepe(String
name) {
ArrayList<Recipe> res =
new ArrayList<>();
for (int i = 0; i <
reciepes.size(); i++) {
if
(reciepes.get(i).getName().equals(name)) {
res.add(reciepes.get(i));
}
}
return res;
}
// enable the user to find recipes by ingredient
and return only those recipes
public ArrayList<Recipe> findRecipe(Ingredient
in) {
ArrayList<Recipe> resultList
= new ArrayList<>();
for (int i = 0; i <
reciepes.size(); i++) {
ArrayList<Ingredient> ingredients =
reciepes.get(i).getIngredients();
if
(ingredients.contains(in)) {
resultList.add(reciepes.get(i));
}
}
return resultList;
}
}
///////////////////////////////////////////ENd
Cookbook.java//////////////////////////////////////////////
///////////////////////////////////////////Driver.java///////////////////
package com.java.cookbook;
import java.util.ArrayList;
//Class that tests the functionality of all the classes.
public class Driver {
public static void main(String args[]) {
// Create a new Cook Book
CookBook book = new
CookBook("chinese");
// create new recipe
Recipe recipe1 = new
Recipe("Noodles");
recipe1.setCategory("Fried
Food");
// create Ingredient1
Ingredient ingredient1 = new
Ingredient("capsicum");
ingredient1.setQuantity(1);
ingredient1.setUnits("1 cup");
// create Ingredient2
Ingredient ingredient2 = new
Ingredient("cabbage");
ingredient2.setQuantity(1);
ingredient2.setUnits("1 cup");
// create Ingredient3
Ingredient ingredient3 = new
Ingredient("oil");
ingredient3.setQuantity(0.5);
ingredient3.setUnits("tsp");
// create Ingredient4
Ingredient ingredient4 = new
Ingredient("salt");
ingredient4.setQuantity(0.5);
ingredient4.setUnits("tsp");
// Add ingredient to
Recipe
recipe1.addIngredient(ingredient1);
recipe1.addIngredient(ingredient2);
recipe1.addIngredient(ingredient3);
recipe1.addIngredient(ingredient4);
Recipe recipe2 = new
Recipe("Chicken Wings");
recipe2.setCategory("Fried
Food");
recipe2.addIngredient("chicken",
0.5, "kg");
recipe2.addIngredient("Soya Sauce",
1, "tsp");
recipe2.addIngredient("Chilli
Sauce", 1, "tsp");
recipe2.addIngredient("Oil", 0.5,
"lt");
Recipe recipe3 = new
Recipe("Schezwan Fried Rice");
recipe3.setCategory("Fried
Food");
recipe3.addIngredient("Rice", 1,
"cup");
recipe3.addIngredient("Soya Sauce",
1, "tsp");
recipe3.addIngredient("Chilli
Sauce", 1, "tsp");
recipe3.addIngredient("Garlic", 4,
"Cloves");
// Add recipes to the book
book.addReciepe(recipe1);
book.addReciepe(recipe2);
book.addReciepe(recipe3);
// print how many recipes are in
the book. WE have added 3 recipes
System.out.println("CookBook
contains:" + book.getReciepes().size() + " recipes");
System.out.println("****Print
CookBook**** ");
System.out.println(book.toString());
System.out.println(" ****End
Printing CookBook**** ");
String rName = "Chicken
Wings";
// FInd recipe with the given
name
ArrayList<Recipe> res =
book.findReciepe(rName);
System.out.println("Total Recipes
found: " + res.size());
for (int i = 0; i < res.size();
i++) {
System.out.println("Recipes with Name: " + rName +
res.get(i));
}
// Find recipe with the given
Ingrident
res =
book.findRecipe(ingredient1);
System.out.println(" Total Recipes
found with Ingredient: " + ingredient1.getName() + "=" +
res.size());
for (int i = 0; i < res.size();
i++) {
System.out.println("Recipes " + res.get(i));
}
res =
book.findRecipe(ingredient2);
System.out.println(" Total Recipes
found with Ingredient: " + ingredient2.getName() + "=" +
res.size());
for (int i = 0; i < res.size();
i++) {
System.out.println("Recipes " + res.get(i));
}
}
}
//////////////////////////////////////////////ENd
Driver.java/////////////////////////////
Screenshot output: