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

public class PantryController extends Controller implements Observer
Controller for the PantryView. This class is responsible for handling the logic for the PantryView. It is responsible for adding, removing and editing shelves and groceries.
  • Field Details

    • view

      private final stud.ntnu.idatt1005.pantrypal.views.PantryView view
      The view for the PantryController.
    • register

      private final ShelfRegister register
    • shelfCount

      private int shelfCount
  • Constructor Details

    • PantryController

      public PantryController(ViewManager viewManager)
      Constructor for the PantryController.
      Parameters:
      viewManager - The view manager for the application.
  • Method Details

    • load

      private void load(String username)
      Loads all shelves and groceries from the database for the specified user.
      Parameters:
      username - the username of the user to load shelves for.
    • getRegister

      public ShelfRegister getRegister()
      Returns the register in the controller. In this case, the register is a ShelfRegister.
      Returns:
      the register in the controller
    • getShelves

      public Shelf[] getShelves()
      Returns all shelves in the register.
      Returns:
      an array of Shelf objects
    • 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 and the view is re-rendered. If the button pressed is REMOVE, the grocery item is removed from the register and the view is re-rendered. If the object is not of type Grocery, an IllegalArgumentException is thrown.
      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 view is re-rendered.
      Specified by:
      update in interface Observer
      Parameters:
      buttonEnum - the button that was pressed
    • addShelf

      public void addShelf()
      Adds a shelf to the register.
    • addShelf

      public Shelf addShelf(String name)
      Adds a shelf to the register.
      Parameters:
      name - the name of the shelf
    • deleteShelf

      public void deleteShelf(Shelf shelf)
      Deletes the specified shelf.
      Parameters:
      shelf - the shelf to delete
    • editShelfName

      public void editShelfName(Shelf shelf, String name)
      Edits the name of the specified shelf.
      Parameters:
      shelf - the shelf to edit
      name - the new name of the shelf
    • getGroceries

      public Grocery[] getGroceries(Shelf shelf)
      Returns all groceries from the specified shelf.
      Parameters:
      shelf - the shelf to get the groceries from
      Returns:
      an array of Grocery objects
    • addGrocery

      public void addGrocery(Shelf shelf, String name, int amount)
      Adds a grocery item to the shelf. If the grocery item already exists in the shelf, the quantity of the grocery item is updated.
      Parameters:
      shelf - the shelf to add the grocery item to
      name - the name of the grocery item
      amount - the amount of the grocery item
    • addGrocery

      public void addGrocery(String shelfName, String name, int amount)
      Adds a grocery item to the shelf. If the shelf does not exist in the register, a new shelf is created and the grocery item is added to the shelf.
      Parameters:
      shelfName - the name of the shelf
      name - the name of the grocery item
      amount - the amount of the grocery item
    • deleteGrocery

      public void deleteGrocery(Shelf shelf, Grocery grocery)
      Removes the grocery item from the shelf. If the shelf does not exist in the register, an IllegalArgumentException is thrown.
      Parameters:
      shelf - the shelf from the grocery is to be removed
      grocery - the grocery item to be removed
    • rerender

      public void rerender()
      Renders the view with the updated data.