Class PantryController
java.lang.Object
stud.ntnu.idatt1005.pantrypal.controllers.Controller
stud.ntnu.idatt1005.pantrypal.controllers.PantryController
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final ShelfRegisterprivate intprivate final stud.ntnu.idatt1005.pantrypal.views.PantryViewThe view for the PantryController.Fields inherited from class stud.ntnu.idatt1005.pantrypal.controllers.Controller
viewManager -
Constructor Summary
ConstructorsConstructorDescriptionPantryController(ViewManager viewManager) Constructor for the PantryController. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddGrocery(String shelfName, String name, int amount) Adds a grocery item to the shelf.voidaddGrocery(Shelf shelf, String name, int amount) Adds a grocery item to the shelf.voidaddShelf()Adds a shelf to the register.Adds a shelf to the register.voiddeleteGrocery(Shelf shelf, Grocery grocery) Removes the grocery item from the shelf.voiddeleteShelf(Shelf shelf) Deletes the specified shelf.voideditShelfName(Shelf shelf, String name) Edits the name of the specified shelf.Grocery[]getGroceries(Shelf shelf) Returns all groceries from the specified shelf.Returns the register in the controller.Shelf[]Returns all shelves in the register.private voidLoads all shelves and groceries from the database for the specified user.voidrerender()Renders the view with the updated data.voidupdate(ButtonEnum buttonEnum) Updates the observer based on the button pressed.voidupdate(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.PantryView viewThe view for the PantryController. -
register
-
shelfCount
private int shelfCount
-
-
Constructor Details
-
PantryController
Constructor for the PantryController.- Parameters:
viewManager- The view manager for the application.
-
-
Method Details
-
load
Loads all shelves and groceries from the database for the specified user.- Parameters:
username- the username of the user to load shelves for.
-
getRegister
Returns the register in the controller. In this case, the register is a ShelfRegister.- Returns:
- the register in the controller
-
getShelves
Returns all shelves in the register.- Returns:
- an array of Shelf objects
-
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 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:
updatein 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 view is re-rendered. -
addShelf
public void addShelf()Adds a shelf to the register. -
addShelf
Adds a shelf to the register.- Parameters:
name- the name of the shelf
-
deleteShelf
Deletes the specified shelf.- Parameters:
shelf- the shelf to delete
-
editShelfName
Edits the name of the specified shelf.- Parameters:
shelf- the shelf to editname- the new name of the shelf
-
getGroceries
Returns all groceries from the specified shelf.- Parameters:
shelf- the shelf to get the groceries from- Returns:
- an array of Grocery objects
-
addGrocery
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 toname- the name of the grocery itemamount- the amount of the grocery item
-
addGrocery
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 shelfname- the name of the grocery itemamount- the amount of the grocery item
-
deleteGrocery
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 removedgrocery- the grocery item to be removed
-
rerender
public void rerender()Renders the view with the updated data.
-