Class ShoppingListController
java.lang.Object
stud.ntnu.idatt1005.pantrypal.controllers.Controller
stud.ntnu.idatt1005.pantrypal.controllers.ShoppingListController
- All Implemented Interfaces:
Observer
Controller class for the ShoppingListView.
Handles the logic for the ShoppingListView,
including managing the grocery register and updating the view.
Implements the Observer interface to listen for changes in the view.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final PantryController
The controller related to the pantry.private final GroceryRegister
The register holding the groceries.private final stud.ntnu.idatt1005.pantrypal.views.ShoppingListView
The view associated with this controller.Fields inherited from class stud.ntnu.idatt1005.pantrypal.controllers.Controller
viewManager
-
Constructor Summary
ConstructorsConstructorDescriptionShoppingListController
(ViewManager viewManager, PantryController pantryController) Constructs a new ShoppingListController with a given view manager and pantry controller. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds groceries to the pantry.void
addGrocery
(Grocery grocery) Adds a grocery to the register.Returns the grocery register.private void
load()
Retrieve the user's shopping list from the database and adds it to the register.private void
removeGrocery
(Grocery grocery) Removes a grocery from the register.void
rerender()
Re-renders the view.void
update
(ButtonEnum buttonEnum) Updates the observer based on the button pressed.void
update
(ButtonEnum buttonEnum, Object object) Updates the observer based on the button pressed and the grocery item associated with the action.Methods inherited from class stud.ntnu.idatt1005.pantrypal.controllers.Controller
isLoggedIn, logOut, onNavLinkPress
-
Field Details
-
view
private final stud.ntnu.idatt1005.pantrypal.views.ShoppingListView viewThe view associated with this controller. -
register
The register holding the groceries. -
pantryController
The controller related to the pantry. Used to update the pantry view when groceries are added to the shopping list.
-
-
Constructor Details
-
ShoppingListController
Constructs a new ShoppingListController with a given view manager and pantry controller. Initializes the grocery register and the shopping list view.- Parameters:
viewManager
- The view manager for the application.pantryController
- The controller for the pantry.
-
-
Method Details
-
load
private void load()Retrieve the user's shopping list from the database and adds it to the register. If the user is not logged in, the shopping list is not loaded. -
getRegister
Returns the grocery register.- Returns:
- the grocery register
-
update
Updates the observer based on the button pressed and the grocery item associated with the action. If the button pressed is ADD, the grocery item is added to the register. If the button pressed is REMOVE, the grocery item is removed from the register. The view is re-rendered after the grocery item is added or removed.- Specified by:
update
in interfaceObserver
- Parameters:
buttonEnum
- the button that was pressedobject
- the grocery item associated with the action- Throws:
IllegalArgumentException
- if the object is not of type Grocery
-
update
Updates the observer based on the button pressed. If the button pressed is ADD_TO_PANTRY, the groceries that are checked are added to the pantry and removed from the shopping list. The view is re-rendered after the groceries are added to the pantry. -
addGroceriesToPantry
public void addGroceriesToPantry()Adds groceries to the pantry. The groceries in the grocery register that are checked are added to the pantry and removed from the shopping list. -
addGrocery
Adds a grocery to the register. If the user is logged in, the grocery is also added to the database.- Parameters:
grocery
- the grocery to be added to the register
-
removeGrocery
Removes a grocery from the register. If the user is logged in, the grocery is also removed from the database.- Parameters:
grocery
- the grocery to be removed from the register
-
rerender
public void rerender()Re-renders the view. Used to update the view with the current grocery register.
-