Class ShoppingListItemRegister

java.lang.Object
no.ntnu.idatt1002.demo.repo.ShoppingListItemRegister

public class ShoppingListItemRegister extends Object
This class represents a register for shopping list items. Allowing for communication between the database and the user interface.
  • Field Details

  • Constructor Details

    • ShoppingListItemRegister

      public ShoppingListItemRegister(DAO dao)
      Constructor for the ShoppingListItemRegister class.
      Parameters:
      dao - the data access object
  • Method Details

    • getItems

      public Map<Integer,ShoppingListItem> getItems()
      Returns the items in the register in the form of lists.
      Returns:
      the items in the register as lists of strings
    • getAllItems

      public void getAllItems()
      Gets all shopping list items from the database and packages them into a list of shopping list items.
    • packageToShoppingListItem

      private void packageToShoppingListItem(List<List<String>> items)
      Helper method to package the shopping list items into a list of shopping list items.
      Parameters:
      items - the items to package
    • addToShoppingList

      public void addToShoppingList(int itemId, int quantity, String unit)
      Method for adding a shopping list item to the database.
      Parameters:
      itemId - the id of the item
      quantity - the quantity of the item
      unit - the unit of the item
    • deleteFromShoppingList

      public void deleteFromShoppingList(int id)
      Method for deleting a shopping list item from the database.
      Parameters:
      id - the id of the shopping list item to delete
      Throws:
      IllegalArgumentException - if the shopping list item with the given id does not exist
    • getIndexFromId

      private int getIndexFromId(int id)
      Helper method to get the index of the shopping list item with the given id.
      Parameters:
      id - the id of the shopping list item
      Returns:
      the index of the shopping list item with the given id
      Throws:
      IllegalArgumentException - if the id is less than 1
    • getShoppingListItemById

      public ShoppingListItem getShoppingListItemById(int id)
    • updateShoppingListItem

      public void updateShoppingListItem(int shoppingListItemId, int itemId, int quantity, String unit)
      Method for updating a shopping list item in the database.
      Parameters:
      shoppingListItemId - the id of the shopping list item to update
      quantity - the quantity of the item
      unit - the unit of the item
      Throws:
      IllegalArgumentException - if the shopping list item with the given id does not exist
    • searchItemsByName

      public void searchItemsByName(String name)
      Method to search for items in the shopping list by name.

      Uses the packageToShoppingListItem to create ShoppingListItem instances from the returned list of data from the DAO.searchFromTable

      Parameters:
      name - the name of the item to search for
      Throws:
      IllegalArgumentException - if the name is empty
    • clearShoppingList

      public void clearShoppingList()