Class ShoppingListController

java.lang.Object
stud.ntnu.idatt1005.pantrypal.controllers.Controller
stud.ntnu.idatt1005.pantrypal.controllers.ShoppingListController
All Implemented Interfaces:
Observer

public class ShoppingListController extends Controller implements 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 Details

    • view

      private final stud.ntnu.idatt1005.pantrypal.views.ShoppingListView view
      The view associated with this controller.
    • register

      private final GroceryRegister register
      The register holding the groceries.
    • pantryController

      private final PantryController pantryController
      The controller related to the pantry. Used to update the pantry view when groceries are added to the shopping list.
  • Constructor Details

    • ShoppingListController

      public ShoppingListController(ViewManager viewManager, PantryController pantryController)
      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

      public GroceryRegister getRegister()
      Returns the grocery register.
      Returns:
      the grocery register
    • update

      public void update(ButtonEnum buttonEnum, Object object)
      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 interface Observer
      Parameters:
      buttonEnum - the button that was pressed
      object - the grocery item associated with the action
      Throws:
      IllegalArgumentException - if the object is not of type Grocery
    • update

      public void update(ButtonEnum buttonEnum)
      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.
      Specified by:
      update in interface Observer
      Parameters:
      buttonEnum - the button that was pressed
    • 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

      public void addGrocery(Grocery grocery)
      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

      private void removeGrocery(Grocery grocery)
      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.