Class Recipe
java.lang.Object
no.ntnu.idatt1002.demo.data.recipes.Recipe
The recipe class represents a dinner recipe for the user. It has a name, a textual instruction
and holds a collection of recipe ingredient items. Each recipe ingredient has a food type,
amount, unit of measure and a status(true/false) of whether it is at hand or not. Apart for
methods to get and alter fields, the class provides the following specialized methods: -
updateIngredientStatus: takes in a list of food types at hand and updates the 'atHand' property
of each of its ingredients accordingly. - getMissingIngredients: returns the number of
ingredients in the recipe that are not at hand. - getMissingList: returns an ArrayList of the
FoodItem labels (String) that are not at hand. specific food type When the Recipe has not been
compared to a collection of ingredients at hand, the number of missing ingredients is set to 0
and the list of missing ingredients is set to an empty ArrayList.
- Author:
- hannesofie
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
addIngredient
(FoodItem ingredientType, double amount, MeasuringUnit unit) The method adds an ingredient to the recipe if it is not already in the recipe, in which case the existing ingredient of the same food type is updated with the parameters provided to this method.boolean
The method takes in another Object and checks whether it is equal to the current recipe object.getIngredient
(FoodItem ingredientType) The method takes in a constant of the FoodItem enum class and searches for it among the recipe's ingredients.The method returns the list of RecipeIngredients that the recipe consists of.The method returns the instructions of the recipe.int
The method returns the property 'missingIngredients' of the recipe.The method returns an ArrayList of Strings representing each of the food types that the recipe is missing relative to the collection of ingredients at hand last updated for.getName()
The method returns the name of the recipe.int
hashCode()
The method returns a standard hash-code based on the recipe's name, ingredient list and instructions.void
setInstructions
(String instructions) The method takes a String as a parameter to which the recipe instructions are set, provided that the String is not blank, in which case an IllegalArgumentException is thrown instead.void
The method takes in a String to which the name of the recipe is set.toString()
The method returns a String representation of the recipe, listing its name, ingredients and instructions.void
updateIngredientStatus
(IngredientsAtHand ingredientsAtHand) The method takes in an object of the IngredientsAtHand class which defines which foods the user has at hand.
-
Constructor Details
-
Recipe
The constructor method creates a new Recipe object by setting the name and instructions as Strings.- Parameters:
name
- Name of the recipe.instructions
- The instructions of the recipe.
-
-
Method Details
-
getName
The method returns the name of the recipe.- Returns:
- The name of the recipe as a String.
-
setName
The method takes in a String to which the name of the recipe is set. If a blank String is given, an IllegalArgumentException is thrown.- Parameters:
name
- New name for the recipe.
-
getIngredientList
The method returns the list of RecipeIngredients that the recipe consists of.- Returns:
- A list of RecipeIngredients belonging to the recipe.
-
getInstructions
The method returns the instructions of the recipe.- Returns:
- The instructions of the recipe as a String.
-
setInstructions
The method takes a String as a parameter to which the recipe instructions are set, provided that the String is not blank, in which case an IllegalArgumentException is thrown instead.- Parameters:
instructions
- The new instructions of the recipe as a String.
-
getIngredient
The method takes in a constant of the FoodItem enum class and searches for it among the recipe's ingredients. If it is found, the recipe ingredient object is returned, if it is not found, null is returned.- Parameters:
ingredientType
- A constant value defined by the FoodItem enum class.- Returns:
- The recipe ingredient if it is present in the recipe ingredient list, null otherwise.
-
addIngredient
The method adds an ingredient to the recipe if it is not already in the recipe, in which case the existing ingredient of the same food type is updated with the parameters provided to this method. The parameters are a constant of the FoodItem enum class to provide the food type, a double value representing the amount of the ingredient and the measuring unit given as a constant of the MeasuringUnit enum class. If the ingredient is neither added nor updated successfully, false is returned, otherwise true is returned.- Parameters:
ingredientType
- The type of food the ingredient belongs to.amount
- The amount of the ingredient when part of this recipe.unit
- The measuring unit of the ingredient part of this recipe.- Returns:
- True if the ingredient is successfully added or altered, otherwise false.
-
updateIngredientStatus
The method takes in an object of the IngredientsAtHand class which defines which foods the user has at hand. If the IngredientsAtHand object is null, and IllegalArgumentException is thrown. Otherwise, the recipe's ingredient list is iterated through, and the 'atHand' property of each ingredient is updated to true if it is present in the ingredients at hand collection, or false if it is not. In addition, the recipe's parameters "missingIngredients" and "missingList" are updated with the final number of ingredients in the recipe that are not at hand and the name of those recipes as Strings in an ArrayList, respectively.- Parameters:
ingredientsAtHand
- An IngredientsAtHand object holding a collection of the food types that the user has available.
-
getMissingIngredients
public int getMissingIngredients()The method returns the property 'missingIngredients' of the recipe. This is the number of ingredients as an int, contained in teh recipe, that is not part of the collection of ingredients at hand last updated for.- Returns:
- The number of ingredients in the recipe that are not currently at hand.
-
getMissingList
The method returns an ArrayList of Strings representing each of the food types that the recipe is missing relative to the collection of ingredients at hand last updated for.- Returns:
- The ingredients in this recipe that are not currently at hand as a list of Strings.
-
toString
The method returns a String representation of the recipe, listing its name, ingredients and instructions. -
equals
The method takes in another Object and checks whether it is equal to the current recipe object. It first checks if the object reference is the same, in which case true is returned. If not, it checks that the object is not null and whether its class is dissimilar, for which false is also returned. Finally, the object is cast as a Recipe and compared by name. If the two recipe's names are the same, they are considered the same. -
hashCode
public int hashCode()The method returns a standard hash-code based on the recipe's name, ingredient list and instructions.
-