Class TheCookbookDB

java.lang.Object
edu.ntnu.stud.idatt1005.team3.database.TheCookbookDB

public class TheCookbookDB extends Object
A class that provides methods for interacting with the database.
  • Constructor Details

    • TheCookbookDB

      public TheCookbookDB()
  • Method Details

    • insertFoodItem

      public void insertFoodItem(String name, double quantity, String unit)
      Inserting a new FoodItem to the database.
      Parameters:
      name - the name of the item
      quantity - the current quantity of the item
      unit - the unit used to measure the item
    • insertRecipe

      public void insertRecipe(String name, int servings)
      Inserting a new recipe to the database.
      Parameters:
      name - the name of the recipe
      servings - the amount of serving the recipe will make
    • insertIngredient

      public void insertIngredient(int food_item_id, int recipe_id, double quantity, String unit)
      Inserts a new ingredient to the database, where we can connect it to a food item and a recipe.
      Parameters:
      food_item_id - a reference to the ingredient in the FoodItem table
      recipe_id - a reference to the recipe the ingredient will be used for
      quantity - the quantity used in the recipe
      unit - the unit used for measurement in the recipe
    • insertRecipeCategory

      public void insertRecipeCategory(int recipe_id, String name)
      Inserts a new category to the database, where we can connect it to a recipe.
      Parameters:
      recipe_id - a reference to the recipe the category will be used for
      name - the name of the category
    • insertStep

      public void insertStep(int step_number, String description, int recipe_id)
      Inserting a step in the recipe.
      Parameters:
      step_number - the number of the step in the recipe
      description - what to do in the current step
      recipe_id - the recipe the step is used for
    • getRecipe

      public Recipe getRecipe(String recipeName)
      Retrieves a recipe from the database.
      Parameters:
      recipeName - the name of the recipe you want to retrieve
      Returns:
      returning the chosen recipe id, name and servings, and then gets the steps and ingredients with their own method using the id
    • getAllRecipeNames

      public List<String> getAllRecipeNames()
      Retrieves all recipes from the database and puts them in a list.
      Returns:
      a list that contains the names of all recipes
    • getAllRecipes

      public ArrayList<Recipe> getAllRecipes()
      Retrieves all food items from the database and puts them in a list.
      Returns:
      Returns a list of all food items.
    • getStepsForRecipe

      public ArrayList<String> getStepsForRecipe(int recipe_id)
      Retrieves all steps from the database og a chosen recipe and puts them in a list.
      Parameters:
      recipe_id - the id of the recipe you want to view
      Returns:
      a list of the steps for the recipe
    • getFoodItem

      public FoodItem getFoodItem(String name)
      Retrieves a food item from the database.
      Parameters:
      name - The name of the food item you want to retrieve
      Returns:
      The chosen food item
    • getIngredientsForRecipe

      public ArrayList<FoodItem> getIngredientsForRecipe(int recipe_id)
      Retrieves all the ingredients from the database og a chosen recipe and puts them in a list.
      Parameters:
      recipe_id - The id of the recipe you want to view
      Returns:
      Returns a list of the ingredients for the recipe
    • getFoodItemQuantity

      public double getFoodItemQuantity(FoodItem foodItem)
      Retrieves all the categories from the database og a chosen recipe and puts them in a list.
      Parameters:
      foodItem - The id of the recipe you want to view.
      Returns:
      Returns a list of the categories for the recipe.
    • updateFoodItemQuantity

      public void updateFoodItemQuantity(int foodItemId, double newQuantity)
      Updates the quantity of a food item in the database.
      Parameters:
      foodItemId - The ID of the food item to update.
      newQuantity - The new quantity of the food item.
    • getNonEmptyFoodItems

      public ArrayList<FoodItem> getNonEmptyFoodItems()
      Retrieves all food items from the database that have a quantity greater than 0.
      Returns:
      A list of all food items with quantity greater than 0.
    • clearPlaceholder

      public void clearPlaceholder()
      Deletes the placeholder recipe from the database once if new recipe hasn't been made.
    • removeRecipe

      public void removeRecipe(int id)
      Deletes a recipe from the database.
      Parameters:
      id - The ID of the recipe to delete.
    • addObserver

      public void addObserver(ObserverDB observer)
      Deletes a food item from the database.
      Parameters:
      observer - The food item to delete.
    • insertPlaceholderRecipe

      public void insertPlaceholderRecipe()
      Inserts a placeholder recipe to the database.
    • getRecipeId

      public int getRecipeId(String name)
      Retrieves the ID of a recipe from the database.
      Parameters:
      name - The name of the recipe whose ID you want to retrieve.
    • getFoodItemId

      public int getFoodItemId(String name)
      Retrieves the ID of a food item from the database.
      Parameters:
      name - The name of the food item whose ID you want to retrieve.
      Returns:
      Returns the ID of the food item.
    • removeFoodItem

      public void removeFoodItem(String name)
      Removes a food item from the database.
      Parameters:
      name - The name of the food item to remove.
    • getIngredientId

      public int getIngredientId(String name, int recipeId)
      Retrieves the ID of an ingredient from the database.
      Parameters:
      name - The name of the ingredient whose ID you want to retrieve.
      recipeId - The ID of the recipe the ingredient is used in.
      Returns:
      Returns the ID of the ingredient.
    • getIngredientAsFoodItem

      public FoodItem getIngredientAsFoodItem(int recipeId, String foodItemName)
      Retrieves the ID of a category from the database.
      Parameters:
      recipeId - The ID of the recipe the category is used in.
      foodItemName - The name of the category whose ID you want to retrieve.
      Returns:
      Returns the ID of the category.
    • getIngredientsAsFoodItemList

      public ArrayList<FoodItem> getIngredientsAsFoodItemList(int recipeId)
      Retrieves the ingredients for a recipe as a list of FoodItem objects.
      Parameters:
      recipeId - The ID of the recipe the ingredient is used in.
      Returns:
      Returns the list of ingredients.
    • getRecipeIdFromIngredientId

      public int getRecipeIdFromIngredientId(int ingredientId)
      Gets the recipe id for an ingredient.
      Parameters:
      ingredientId - The id of the ingredient.
      Returns:
      Returns the recipe id.
    • updateFoodItem

      public void updateFoodItem(FoodItem foodItem, double quantity)
      Updates the quantity of a food item in the database.
      Parameters:
      foodItem - The food item to update.
      quantity - The new quantity of the food item.
    • removeIngredient

      public void removeIngredient(String name, int recipeId)
      Removes an ingredient from the database.
      Parameters:
      name - The name of the ingredient to remove.
      recipeId - The ID of the recipe the ingredient is used in.
    • updatePlaceholderRecipe

      public void updatePlaceholderRecipe(int id, String name, int servings)
      Updates the placeholder recipe to a proper new recipe in the NewRecipePage.
      Parameters:
      id - The ID of the recipe to update.
      name - The name of the recipe to update.
      servings - The amount of servings for the recipe.
    • removeObserver

      public void removeObserver(ObserverDB observer)
      Deletes a food item from the database.
      Parameters:
      observer - The observer to remove.
    • notifyObservers

      public void notifyObservers()
      Notifies all observers that the database has been updated.