java.lang.Object
stud.ntnu.idatt1005.pantrypal.controllers.Controller
stud.ntnu.idatt1005.pantrypal.controllers.CookbookController
All Implemented Interfaces:
Observer

public class CookbookController extends Controller implements Observer
The controller for the CookBookView and RecipeView. This class is responsible for handling the logic for the CookBookView and RecipeView.
  • Field Details

    • recipeRegister

      private final RecipeRegister recipeRegister
    • currentSearch

      private List<Recipe> currentSearch
    • shelfRegister

      private final ShelfRegister shelfRegister
    • shoppingListRegister

      private final GroceryRegister shoppingListRegister
    • shoppingListController

      private final ShoppingListController 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 application
      shoppingListController - the ShoppingListController for the application
      pantryController - 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

      public Map<String,Recipe> getRecipes()
      Returns the register with recipes in the recipeRegister.
      Returns:
      the register with Recipes in the recipeRegister.
    • getCurrentSearch

      public List<Recipe> getCurrentSearch()
      Returns the current search list.
      Returns:
      the current search list.
    • update

      public void update(ButtonEnum buttonEnum, Object object)
      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.
      Specified by:
      update in interface Observer
      Parameters:
      buttonEnum - the button that was pressed
      object - the object that is related to the button
    • update

      public void update(ButtonEnum buttonEnum)
      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.
      Specified by:
      update in interface Observer
      Parameters:
      buttonEnum - the button that was pressed
    • 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

      public void searchRecipes(String search)
      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

      private void openRecipe(Recipe recipe)
      Opens a recipe in the RecipeView, and sets the view to RecipeView.
      Parameters:
      recipe - the recipe to be opened in the RecipeView.
    • addGroceriesToShoppingList

      private void addGroceriesToShoppingList(Recipe recipe)
      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

      private void addRecipe(Recipe recipe)
      Adds a recipe to the recipeRegister and the database.
      Parameters:
      recipe - the recipe to add to the recipeRegister and the database.
    • toggleIsFavorite

      private void toggleIsFavorite(Recipe recipe)
      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.