Package no.ntnu.idatt1002.demo.repo
Class ItemRegister
java.lang.Object
no.ntnu.idatt1002.demo.repo.ItemRegister
This class represents a register for items. Allowing for communication
between the database and the user interface.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
This method adds a new item to the database.void
deleteItem
(int id) This method deletes an item from the database.void
filterItemsByCategory
(String category) Retrieves filtered items by category from the database.void
This method retrieves all items from the database.private int
getIndexFromId
(int id) Returns the index of the item with the given id.getItemById
(int id) getItems()
Returns the items in the register in the form of lists.private void
packageToItem
(List<List<String>> items) This method packages the lists of strings into items.void
searchItemsByName
(String name) This method searches for items by name and retrieves them from the database.toString()
Returns a string representation of the item register.void
updateItem
(int id, String name, String category, String allergy) Updates an item in the database.
-
Field Details
-
items
-
dao
-
-
Constructor Details
-
ItemRegister
Constructor for the ItemRegister class.- Parameters:
dao
- the data access object- Throws:
IllegalArgumentException
- if dao is null
-
-
Method Details
-
getItems
Returns the items in the register in the form of lists.- Returns:
- the items in the register as lists of strings
-
filterItemsByCategory
Retrieves filtered items by category from the database.- Parameters:
category
- the category to filter by- Throws:
IllegalArgumentException
- if category is null or empty
-
searchItemsByName
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
This method packages the lists of strings into items. -
addItem
This method adds a new item to the database.- Parameters:
name
- the name of the itemcategory
- the category of the itemallergy
- 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
-
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
Updates an item in the database.- Parameters:
id
- the id of the item to updatename
- the name of the itemcategory
- the category of the itemallergy
- the allergy of the item- Throws:
IllegalArgumentException
- if the item does not exist
-
toString
Returns a string representation of the item register.
-