Question

In: Computer Science

For this assignment you will create a set of simple classes that model a cookbook. The...

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:

  • Ingredient
    • name - the name of the ingredient
    • quantity - the amount of the ingredient
    • units - the unit of the quantity (e.g, tablespoon, or T, or Tbl)
    • require the creator to supply the name of the ingredient in order to create one
    • enable the user to retrieve and update each of the attributes appropriately
  • Recipe
    • name - the name of the recipe
    • category - the category of the recipe
    • ingredients - a list of ingredients (you may assume no more than 20)
    • instructions - this would be a paragraph of text, no need to worry about steps
    • require the creator to supply the name of the recipe in order to create one
    • enable the user to retrieve and update each of the attributes appropriately
  • Cookbook
    • name - the name of the cookbook
    • recipes - a list of recipes (you may assume no more than 20)
    • require the creator to supply the name of the cookbook in order to create one
    • enable the user to find recipes by name, returning only those recipes
    • Extra Credit (+1/3 letter grade): enable the user to find recipes by ingredient and return only those recipes

Solutions

Expert Solution

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:


Related Solutions

For this assignment you will design a set of classes that work together to simulate a...
For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. You should design the following classes: - The ParkedCar Class: This class should simulate a parked car. The class's responsibilities are as follows: - To know the car's make, model, color, license number, and the number of minutes that the car has been parked. - The ParkingMeter Class: This class should simulate a parking meter. The class's only...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
For this assignment, you will create a hierarchy of five classes to describe various elements of...
For this assignment, you will create a hierarchy of five classes to describe various elements of a school setting. The classes you will write are: Person, Student,Teacher, HighSchoolStudent, and School. Detailed below are the requirements for the variables and methods of each class. You may need to add a few additional variables and/or methods; figuring out what is needed is part of your task with this assignment. Person Variables: String firstName - Holds the person's first name String lastName -...
Parking Ticket simulator For this assignment you will design a set of classes that work together...
Parking Ticket simulator For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. You should design the following classes: • The ParkedCar Class: This class should simulate a parked car. The class’s responsibili-ties are as follows: – To know the car’s make, model, color, license number, and the number of minutes that the car has been parked. • The ParkingMeter Class: This class should simulate a parking meter....
parking Ticket simulator For this assignment you will design a set of classes that work together...
parking Ticket simulator For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. You should design the following classes: • The ParkedCar Class: This class should simulate a parked car. The class’s responsibili-ties are as follows: – To know the car’s make, model, color, license number, and the number of minutes that the car has been parked. • The ParkingMeter Class: This class should simulate a parking meter....
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple HR application using the C++ programming language and Oracle server. This assignment helps students learn a basic understanding of application development using C++ programming and an Oracle database Submission: This Milestone is a new project that simply uses what was learned in the SETUP. Your submission will be a single text-based .cpp file including your C++ program for the Database Application project/assignment. The file...
For this assignment, you are to use the business model canvas to create four (4) canvases...
For this assignment, you are to use the business model canvas to create four (4) canvases on different business concepts Make sure to include the following in the margins as per the video. 1)Key Trends 2) Industry Trends 3) Market Forces 4) Marco-economic forces
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT