Package no.ntnu.idatt1002.demo.repo
Class ShoppingListItemRegister
java.lang.Object
no.ntnu.idatt1002.demo.repo.ShoppingListItemRegister
This class represents a register for shopping list items.
Allowing for communication between the database and the user interface.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for the ShoppingListItemRegister class. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addToShoppingList
(int itemId, int quantity, String unit) Method for adding a shopping list item to the database.void
void
deleteFromShoppingList
(int id) Method for deleting a shopping list item from the database.void
Gets all shopping list items from the database and packages them into a list of shopping list items.private int
getIndexFromId
(int id) Helper method to get the index of the shopping list item with the given id.getItems()
Returns the items in the register in the form of lists.getShoppingListItemById
(int id) private void
packageToShoppingListItem
(List<List<String>> items) Helper method to package the shopping list items into a list of shopping list items.void
searchItemsByName
(String name) Method to search for items in the shopping list by name.void
updateShoppingListItem
(int shoppingListItemId, int itemId, int quantity, String unit) Method for updating a shopping list item in the database.
-
Field Details
-
shoppingListItems
-
dao
-
-
Constructor Details
-
ShoppingListItemRegister
Constructor for the ShoppingListItemRegister class.- Parameters:
dao
- the data access object
-
-
Method Details
-
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
Helper method to package the shopping list items into a list of shopping list items.- Parameters:
items
- the items to package
-
addToShoppingList
Method for adding a shopping list item to the database.- Parameters:
itemId
- the id of the itemquantity
- the quantity of the itemunit
- 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
-
updateShoppingListItem
Method for updating a shopping list item in the database.- Parameters:
shoppingListItemId
- the id of the shopping list item to updatequantity
- the quantity of the itemunit
- the unit of the item- Throws:
IllegalArgumentException
- if the shopping list item with the given id does not exist
-
searchItemsByName
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 theDAO.searchFromTable
- Parameters:
name
- the name of the item to search for- Throws:
IllegalArgumentException
- if the name is empty
-
clearShoppingList
public void clearShoppingList()
-