Class CookbookController
java.lang.Object
stud.ntnu.idatt1005.pantrypal.controllers.Controller
stud.ntnu.idatt1005.pantrypal.controllers.CookbookController
- All Implemented Interfaces:
Observer
The controller for the CookBookView and RecipeView.
This class is responsible for handling the logic for the CookBookView and RecipeView.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final RecipeRegister
private final ShelfRegister
private final ShoppingListController
private final GroceryRegister
private final stud.ntnu.idatt1005.pantrypal.views.CookbookView
Fields inherited from class stud.ntnu.idatt1005.pantrypal.controllers.Controller
viewManager
-
Constructor Summary
ConstructorsConstructorDescriptionCookbookController
(ViewManager viewManager, ShoppingListController shoppingListController, PantryController pantryController) Constructor for the CookbookController. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addGroceriesToShoppingList
(Recipe recipe) Adds the groceries from a recipe to the shopping list.private void
Adds a recipe to the recipeRegister and the database.Returns the current search list.Returns the register with recipes in the recipeRegister.private void
load()
Loads the recipes from the database.private void
Opens the AddRecipeView and sets the view to AddRecipeView.private void
openRecipe
(Recipe recipe) Opens a recipe in the RecipeView, and sets the view to RecipeView.void
rerender()
Re-renders the view with the currentSearch.void
searchRecipes
(String search) Searches for recipes in the recipeRegister based on the search string.private void
toggleIsFavorite
(Recipe recipe) Toggles the favorite status of a recipe.void
update
(ButtonEnum buttonEnum) Updates the observer with the button that was pressed.void
update
(ButtonEnum buttonEnum, Object object) Updates the observer with the button that was pressed and the object affected.Methods inherited from class stud.ntnu.idatt1005.pantrypal.controllers.Controller
isLoggedIn, logOut, onNavLinkPress
-
Field Details
-
recipeRegister
-
currentSearch
-
shelfRegister
-
shoppingListRegister
-
shoppingListController
-
view
private final stud.ntnu.idatt1005.pantrypal.views.CookbookView view
-
-
Constructor Details
-
CookbookController
public CookbookController(ViewManager viewManager, ShoppingListController shoppingListController, PantryController pantryController) Constructor for the CookbookController. The constructor takes in a ViewManager, a ShoppingListController, and a PantryController. The constructor creates a new RecipeRegister and sets the shelfRegister and shoppingListRegister to the registers in the PantryController and ShoppingListController. The constructor loads the recipes from the database if the user is logged in. The currentSearch is set to all the recipes in the recipeRegister. The view is created and added to the viewManager.- Parameters:
viewManager
- the ViewManager for the applicationshoppingListController
- the ShoppingListController for the applicationpantryController
- the PantryController for the application
-
-
Method Details
-
load
private void load()Loads the recipes from the database. This method gets all the recipes from the database and creates a new Recipe object for each recipe related to the user. The method also gets the groceries and steps for each recipe and adds them to the Recipe object. The method also checks if the recipe is a favorite for the user and sets the isFavorite boolean in the Recipe object. The Recipe object is then added to the recipeRegister. -
getRecipes
Returns the register with recipes in the recipeRegister.- Returns:
- the register with Recipes in the recipeRegister.
-
getCurrentSearch
Returns the current search list.- Returns:
- the current search list.
-
update
Updates the observer with the button that was pressed and the object affected. If the object is a Recipe, it performs the action related to the buttonEnum. If the object is not a Recipe, it throws an IllegalArgumentException. -
update
Updates the observer with the button that was pressed. If the button is 'buttonEnum.ADD', it opens the AddRecipeView for the user to add a new Recipe. -
openAddRecipe
private void openAddRecipe()Opens the AddRecipeView and sets the view to AddRecipeView. Creates a new AddRecipeController and AddRecipeView and set the view to AddRecipeView. -
searchRecipes
Searches for recipes in the recipeRegister based on the search string. The search string is passed to the recipeRegister, and the currentSearch is set to the result of the search. The view is then rendered with the currentSearch.- Parameters:
search
- the search string to search for in the recipeRegister
-
openRecipe
Opens a recipe in the RecipeView, and sets the view to RecipeView.- Parameters:
recipe
- the recipe to be opened in the RecipeView.
-
addGroceriesToShoppingList
Adds the groceries from a recipe to the shopping list. This method gets the grocery from the recipe and checks if the grocery is already in the shopping list or pantry. If the grocery is in the pantry, it checks if the quantity is enough. If not, it adds the difference to the shopping list. If the grocery is not in the pantry, it adds the grocery to the shopping list.- Parameters:
recipe
- the recipe to add groceries from.
-
addRecipe
Adds a recipe to the recipeRegister and the database.- Parameters:
recipe
- the recipe to add to the recipeRegister and the database.
-
toggleIsFavorite
Toggles the favorite status of a recipe.- Parameters:
recipe
- the recipe to toggle the favorite status of.
-
rerender
public void rerender()Re-renders the view with the currentSearch.
-