Class ItemRegister

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

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

    • items

      private List<Item> items
    • dao

      private final DAO dao
  • Constructor Details

    • ItemRegister

      public ItemRegister(DAO dao)
      Constructor for the ItemRegister class.
      Parameters:
      dao - the data access object
      Throws:
      IllegalArgumentException - if dao is null
  • Method Details

    • getItems

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

      public void filterItemsByCategory(String category)
      Retrieves filtered items by category from the database.
      Parameters:
      category - the category to filter by
      Throws:
      IllegalArgumentException - if category is null or empty
    • searchItemsByName

      public void searchItemsByName(String name)
      This method searches for items by name and retrieves them from the database.
      Parameters:
      name - the name to search by
      Throws:
      IllegalArgumentException - if name is null or empty
    • getAllItems

      public void getAllItems()
      This method retrieves all items from the database.
    • packageToItem

      private void packageToItem(List<List<String>> items)
      This method packages the lists of strings into items.
    • addItem

      public void addItem(String name, String category, String allergy)
      This method adds a new item to the database.
      Parameters:
      name - the name of the item
      category - the category of the item
      allergy - the allergy of the item
    • deleteItem

      public void deleteItem(int id)
      This method deletes an item from the database.
      Parameters:
      id - the id of the item to delete
      Throws:
      IllegalArgumentException - if the item does not exist
    • getItemById

      public Item getItemById(int id)
    • getIndexFromId

      private int getIndexFromId(int id)
      Returns the index of the item with the given id.
      Parameters:
      id - the id of the item*
      Returns:
      the index of the item with the given id
      Throws:
      IllegalArgumentException - if id is less than 1
    • updateItem

      public void updateItem(int id, String name, String category, String allergy)
      Updates an item in the database.
      Parameters:
      id - the id of the item to update
      name - the name of the item
      category - the category of the item
      allergy - the allergy of the item
      Throws:
      IllegalArgumentException - if the item does not exist
    • toString

      public String toString()
      Returns a string representation of the item register.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the item register