diff --git a/src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java b/src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java index 3ee7c24e81fb4b68ec7df44330b735a2b828f287..9a3324d1edfbe37c13653976c6af6a78fb5bdd65 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java +++ b/src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java @@ -1,6 +1,10 @@ package no.ntnu.idatt1002.demo.dao; -import java.sql.*; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import no.ntnu.idatt1002.demo.data.Storable; @@ -13,6 +17,7 @@ public class DAO { /** * Constructor for the DAO class. + * * @param connectionProvider the connection provider */ public DAO(DBConnectionProvider connectionProvider) { @@ -21,6 +26,7 @@ public class DAO { /** * This method adds an object to the database. + * * @param obj the object to add */ public void addToDatabase(Storable obj) { @@ -43,10 +49,12 @@ public class DAO { /** * This method deletes an item from the database. + * * @param obj the object to be deleted */ public void deleteFromDatabase(Storable obj) { - String sql = String.format("DELETE FROM %s WHERE %s = ?", obj.getClass().getSimpleName(), obj.getIdName()); + String sql = String.format("DELETE FROM %s WHERE %s = ?", + obj.getClass().getSimpleName(), obj.getIdName()); try (Connection connection = connectionProvider.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setInt(1, obj.getId()); @@ -59,12 +67,13 @@ public class DAO { /** * This method updates an object in the database. + * * @param obj the object to be updated */ public void updateDatabase(Storable obj) { String sql = String.format("UPDATE %s SET %s = ? WHERE %s = ?", obj.getClass().getSimpleName(), - String.join(" = ?, ", obj.getAttributeNames()),obj.getIdName()); + String.join(" = ?, ", obj.getAttributeNames()), obj.getIdName()); List<String> attributes = obj.getAttributes(); try (Connection connection = connectionProvider.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -82,6 +91,7 @@ public class DAO { /** * This method returns all attributes from a table. + * * @param table the table to get attributes from * @return a list of lists containing all attributes from a table */ @@ -91,8 +101,9 @@ public class DAO { if (joinTable == null) { sql = "SELECT * FROM " + table; } else { - sql = "SELECT * FROM " + table + - " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + joinTable + "." + joinColumn; + sql = "SELECT * FROM " + table + + " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + + joinTable + "." + joinColumn; } try (Connection connection = connectionProvider.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); @@ -116,21 +127,24 @@ public class DAO { /** * This method searches for objects by name in the database. + * * @param table the table to search in * @param name the name to search for (can also be only part of the name) * @param joinTable the table to join with if needed * @param joinColumn the column to join on if needed * @return a list of lists containing the attributes of the objects with the given name */ - public List<List<String>> searchFromTable(String table, String name, String joinTable, String joinColumn) { + public List<List<String>> searchFromTable( + String table, String name, String joinTable, String joinColumn) { List<List<String>> items = new ArrayList<>(); String sql; if (joinTable == null) { - sql = "SELECT * FROM "+ table + " WHERE name LIKE ?"; + sql = "SELECT * FROM " + table + " WHERE name LIKE ?"; } else { - sql = "SELECT * FROM " + table + - " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + joinTable + "." + joinColumn + - " WHERE name LIKE ?"; + sql = "SELECT * FROM " + table + + " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + + joinTable + "." + joinColumn + + " WHERE name LIKE ?"; } try (Connection connection = connectionProvider.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -155,6 +169,7 @@ public class DAO { /** * This method filters objects by a given column in the database. + * * @param table the table to filter from * @param filterColumn the column to filter by * @param filter the filter to use @@ -162,15 +177,18 @@ public class DAO { * @param joinColumn the column to join on if needed * @return a list of items with the given category */ - public List<List<String>> filterFromTable(String table, String filterColumn, String filter, String joinTable, String joinColumn) { + public List<List<String>> filterFromTable( + String table, String filterColumn, String filter, + String joinTable, String joinColumn) { List<List<String>> items = new ArrayList<>(); String sql; if (joinTable == null) { sql = "SELECT * FROM " + table + " WHERE " + filterColumn + " LIKE ?"; } else { - sql = "SELECT * FROM " + table + - " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + joinTable + "." + joinColumn + - " WHERE " + filterColumn + " LIKE ?"; + sql = "SELECT * FROM " + table + + " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + + joinTable + "." + joinColumn + + " WHERE " + filterColumn + " LIKE ?"; } try (Connection connection = connectionProvider.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -195,6 +213,7 @@ public class DAO { /** * This method returns all columns from a table. + * * @param table the table to get columns from * @return a list of all columns from the table */ diff --git a/src/main/java/no/ntnu/idatt1002/demo/dao/DBConnectionProvider.java b/src/main/java/no/ntnu/idatt1002/demo/dao/DBConnectionProvider.java index 828a248b78683470588dfeefed222219a67c07a9..2c556d1a81c2e05346b0a2efa8adb192864078f4 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/dao/DBConnectionProvider.java +++ b/src/main/java/no/ntnu/idatt1002/demo/dao/DBConnectionProvider.java @@ -1,6 +1,11 @@ package no.ntnu.idatt1002.demo.dao; -import java.sql.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; /** * This class provides a connection to the database. @@ -9,7 +14,8 @@ public class DBConnectionProvider { private final String url; private static DBConnectionProvider databaseConnectionProvider; - private static final String DB_PATH = "src/main/resources/no/ntnu/idatt1002/database/database.sqlite"; + private static final String DB_PATH = + "src/main/resources/no/ntnu/idatt1002/database/database.sqlite"; /** * Constructor for the DBConnectionProvider class. @@ -20,6 +26,7 @@ public class DBConnectionProvider { /** * This method returns a connection to the database. + * * @return a connection to the database */ Connection getConnection() { @@ -32,6 +39,7 @@ public class DBConnectionProvider { /** * This method returns an instance of the DBConnectionProvider. + * * @return an instance of the DBConnectionProvider */ public static DBConnectionProvider getInstance() { @@ -43,6 +51,7 @@ public class DBConnectionProvider { /** * This method closes the connection to the database. + * * @param connection the connection to the database */ public void closeConnection(Connection connection) { @@ -55,6 +64,7 @@ public class DBConnectionProvider { /** * This method closes the PreparedStatement. + * * @param preparedStatement the PreparedStatement */ public void closePreparedStatement(PreparedStatement preparedStatement) { @@ -69,6 +79,7 @@ public class DBConnectionProvider { /** * This method closes the ResultSet. + * * @param resultSet the ResultSet */ public void closeResultSet(ResultSet resultSet) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Event.java b/src/main/java/no/ntnu/idatt1002/demo/data/Event.java index f791e97dd277a98851480673d4c6e7cf47eda1df..47f861355d157336a97cb0e9f4247326fcccea73 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Event.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Event.java @@ -10,14 +10,15 @@ import java.util.List; */ public class Event implements Storable { private int event_id; - private int recipe_id; + private final int recipe_id; private String name; - private int date; + private final int date; private String category; private int cooking_time; /** * Constructor for the Event class. + * * @param recipe_id the id of the recipe * @param date the date of the event */ @@ -31,6 +32,7 @@ public class Event implements Storable { /** * Constructor for the Event class. + * * @param event_id the id of the event * @param recipe_id the id of the recipe * @param date the date of the event @@ -42,6 +44,7 @@ public class Event implements Storable { /** * Constructor for the Event class. + * * @param event_id the id of the event * @param recipe_id the id of the recipe * @param name the name of the event @@ -49,7 +52,8 @@ public class Event implements Storable { * @param category the category of the event * @param cooking_time the cooking time of the event */ - public Event(int event_id, int recipe_id, String name, int date, String category, int cooking_time) { + public Event( + int event_id, int recipe_id, String name, int date, String category, int cooking_time) { this(event_id, recipe_id, date); this.name = name; this.category = category; @@ -59,6 +63,7 @@ public class Event implements Storable { /** * Returns the attributes of the event. + * * @return the attributes of the event */ @Override @@ -71,6 +76,7 @@ public class Event implements Storable { /** * Returns the attribute names of the event. + * * @return the attribute names of the event */ @Override @@ -83,15 +89,17 @@ public class Event implements Storable { /** * Returns the id of the event. + * * @return the id of the event */ @Override public int getId() { - return event_id; + return getEvent_id(); } /** * Returns the name of the id. + * * @return the name of the id */ @Override @@ -101,6 +109,7 @@ public class Event implements Storable { /** * Getter method for the event id. + * * @return the event id */ public int getEvent_id() { @@ -109,6 +118,7 @@ public class Event implements Storable { /** * Getter method for the recipe id of the event. + * * @return the recipe id of the event */ public int getRecipe_id() { @@ -117,12 +127,22 @@ public class Event implements Storable { /** * Getter method for the date of the event. + * * @return the date of the event */ public int getDate() { return date; } + /** + * Getter method for the name of the event. + * + * @return the name of the event + */ + public String getName() { + return name; + } + @Override public String toString() { return "Event ID: " + event_id + ", Recipe ID: " + recipe_id + ", Date: " + date; diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/InventoryItem.java b/src/main/java/no/ntnu/idatt1002/demo/data/InventoryItem.java index 13689f79fd608545374fe3bce826821fc1c1815b..c5e298d09043413f8d8cd077e784879d5326f541 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/InventoryItem.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/InventoryItem.java @@ -4,14 +4,16 @@ import java.util.ArrayList; import java.util.List; /** - * This class is a simple bean for an inventory item. This is an item with a quantity, a unit and an expiration date. + * This class is a simple bean for an inventory item. + * This is an item with a quantity, a unit and an expiration date. */ -public class InventoryItem extends ShoppingListItem{ +public class InventoryItem extends ShoppingListItem { private int inventory_id; private final int expirationDate; /** - * Constructor for the InventoryItem class + * Constructor for the InventoryItem class. + * * @param name the name of the item * @param category the category of the item * @param allergy the allergy of the item @@ -19,13 +21,16 @@ public class InventoryItem extends ShoppingListItem{ * @param unit the unit of the item * @param expirationDate the expiration date of the item */ - public InventoryItem(int item_id, String name, String category, String allergy, int quantity, String unit, int expirationDate) { + public InventoryItem( + int item_id, String name, String category, String allergy, + int quantity, String unit, int expirationDate) { super(item_id, name, category, allergy, quantity, unit); this.expirationDate = expirationDate; } /** - * Constructor for the InventoryItem class + * Constructor for the InventoryItem class. + * * @param inventory_id the id of the inventory item * @param item_id the id of the item * @param name the name of the item @@ -35,13 +40,16 @@ public class InventoryItem extends ShoppingListItem{ * @param unit the unit of the item * @param expirationDate the expiration date of the item */ - public InventoryItem(int inventory_id, int item_id, String name, String category, String allergy, int quantity, String unit, int expirationDate) { + public InventoryItem( + int inventory_id, int item_id, String name, String category, + String allergy, int quantity, String unit, int expirationDate) { this(item_id, name, category, allergy, quantity, unit, expirationDate); this.inventory_id = inventory_id; } /** * This method returns the attributes of the inventory item. + * * @return the attributes of the inventory item */ @Override @@ -56,6 +64,7 @@ public class InventoryItem extends ShoppingListItem{ /** * This method returns the attribute names of the inventory item. + * * @return the attribute names of the inventory item */ @Override @@ -70,19 +79,39 @@ public class InventoryItem extends ShoppingListItem{ /** * This method returns the id of the inventory item. + * * @return the id of the inventory item */ @Override public int getId() { - return inventory_id; + return getInventory_id(); } /** * This method returns the name of the id. + * * @return the name of the id */ @Override public String getIdName() { return "inventory_id"; } + + /** + * Getter method for the expiration date. + * + * @return the expiration date + */ + public int getExpirationDate() { + return expirationDate; + } + + /** + * Getter method for the inventory id. + * + * @return the inventory id + */ + public int getInventory_id() { + return inventory_id; + } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java index 09efcbd691c67463436cf59f22c6e389daf5d35a..7539e4b5b299afde01481f926f705da7e1ccf1ce 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java @@ -1,14 +1,13 @@ package no.ntnu.idatt1002.demo.data; -import no.ntnu.idatt1002.demo.util.VerifyInput; - import java.util.ArrayList; import java.util.List; +import no.ntnu.idatt1002.demo.util.VerifyInput; /** * This class is a simple bean for an item. */ -public class Item implements Storable{ +public class Item implements Storable { private int item_id; private final String name; private final String category; @@ -16,6 +15,7 @@ public class Item implements Storable{ /** * Constructor for the Item class. When creating a new item, the id is not known. + * * @param name the name of the item * @param category the category of the item * @param allergy the allergy of the item @@ -30,6 +30,7 @@ public class Item implements Storable{ /** * Constructor for the Item class. When creating an item from the database, the id is known. + * * @param item_id the id of the item * @param name the name of the item * @param category the category of the item @@ -42,6 +43,7 @@ public class Item implements Storable{ /** * This method returns the attributes of the item. + * * @return the attribute names of the item */ @Override @@ -55,6 +57,7 @@ public class Item implements Storable{ /** * This method returns the attribute names of the item. + * * @return the attribute names of the item */ @Override @@ -68,6 +71,7 @@ public class Item implements Storable{ /** * This method returns the id of the item. + * * @return the id of the item */ @Override @@ -77,6 +81,7 @@ public class Item implements Storable{ /** * This method returns the name of the id. + * * @return the name of the id */ @Override @@ -86,6 +91,7 @@ public class Item implements Storable{ /** * This method returns the id of the item. + * * @return the id of the item */ public int getItem_id() { @@ -94,6 +100,7 @@ public class Item implements Storable{ /** * This method returns the name of the item. + * * @return the name of the item */ public String getName() { @@ -102,6 +109,7 @@ public class Item implements Storable{ /** * This method returns the category of the item. + * * @return the category of the item */ public String getCategory() { @@ -110,6 +118,7 @@ public class Item implements Storable{ /** * This method returns the allergy of the item. + * * @return the allergy of the item */ public String getAllergy() { @@ -118,10 +127,12 @@ public class Item implements Storable{ /** * This method returns a string representation of the item. + * * @return a string representation of the item */ @Override public String toString() { - return "Item ID: " + item_id + ", Name: " + name + ", Category: " + category + ", Allergy: " + allergy; + return "Item ID: " + item_id + ", Name: " + name + ", Category: " + category + + ", Allergy: " + allergy; } } \ No newline at end of file diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Recipe.java b/src/main/java/no/ntnu/idatt1002/demo/data/Recipe.java index 34deb3552d42e77d5d3ed13fa4fb7a4c7f11c79e..4e7f344c5e41b676cc30a5da3e21d1cadf90faea 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Recipe.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Recipe.java @@ -10,13 +10,14 @@ import java.util.List; */ public class Recipe implements Storable { private int recipe_id; - private String name; - private int cooking_time; - private String category; - private List<RecipeIngredient> ingredients; + private final String name; + private final int cooking_time; + private final String category; + private final List<RecipeIngredient> ingredients; /** * Constructor for the Recipe class. + * * @param name the name of the recipe * @param category the category of the recipe * @param cooking_time the cooking time of the recipe @@ -33,95 +34,104 @@ public class Recipe implements Storable { /** * Constructor for the Recipe class. - * @param recipe_id - * @param name - * @param cooking_time - * @param category * + * @param recipe_id the id of the recipe + * @param name the name of the recipe + * @param cooking_time the cooking time of the recipe + * @param category the category of the recipe */ public Recipe(int recipe_id, String name, int cooking_time, String category) { - this(name, cooking_time, category); - this.recipe_id = recipe_id; + this(name, cooking_time, category); + this.recipe_id = recipe_id; } /** * Returns the attributes of the recipe. + * * @return the attributes of the recipe */ @Override public List<String> getAttributes() { - List<String> attributes = new ArrayList<>(); - attributes.add(name); - attributes.add(Integer.toString(cooking_time)); - attributes.add(category); - return attributes; + List<String> attributes = new ArrayList<>(); + attributes.add(name); + attributes.add(Integer.toString(cooking_time)); + attributes.add(category); + return attributes; } /** * Returns the attribute names of the recipe. + * * @return the attribute names of the recipe */ @Override public List<String> getAttributeNames() { - List<String> attributes = new ArrayList<>(); - attributes.add("name"); - attributes.add("cooking_time"); - attributes.add("category"); - return attributes; + List<String> attributes = new ArrayList<>(); + attributes.add("name"); + attributes.add("cooking_time"); + attributes.add("category"); + return attributes; } /** * Returns the id of the recipe. + * * @return the id of the recipe */ @Override public int getId() { - return recipe_id; + return getRecipe_id(); } /** * Returns the name of the id. + * * @return the name of the id */ @Override public String getIdName() { - return "recipe_id"; + return "recipe_id"; } /** * Returns the recipe id. + * * @return the recipe id */ public int getRecipe_id() { - return recipe_id; + return recipe_id; } /** * Returns the name of the recipe. + * * @return the name of the recipe */ public String getName() { - return name; + return name; } /** * Returns the category of the recipe. + * * @return the category of the recipe */ public String getCategory() { - return category; + return category; } /** * Returns the cooking time of the recipe. + * * @return the cooking time of the recipe */ public int getCooking_time() { - return cooking_time; + return cooking_time; } /** * Adds an ingredient to the recipe. + * * @param recipeIngredient_id the id of the recipe ingredient * @param item_id the id of the item * @param name the name of the item @@ -131,12 +141,17 @@ public class Recipe implements Storable { * @param unit the unit of the item * @param recipe_id the id of the recipe */ - public void addIngredient(int recipeIngredient_id, int item_id, String name, String category, String allergy, int quantity, String unit, int recipe_id) { - ingredients.add(new RecipeIngredient(recipeIngredient_id, item_id, name, category, allergy, quantity, unit, recipe_id)); + public void addIngredient( + int recipeIngredient_id, int item_id, String name, String category, + String allergy, int quantity, String unit, int recipe_id) { + ingredients.add( + new RecipeIngredient( + recipeIngredient_id, item_id, name, category, allergy, quantity, unit, recipe_id)); } /** * Returns the ingredients of the recipe. + * * @return the ingredients of the recipe */ public List<RecipeIngredient> getIngredients() { @@ -145,6 +160,7 @@ public class Recipe implements Storable { /** * Returns the ingredient with the given id. + * * @param id the id of the ingredient * @return the ingredient with the given id */ @@ -159,15 +175,16 @@ public class Recipe implements Storable { /** * Returns the recipe as a string representation. - * @return the string representation of the recipe + * + * @return the string representation of the recipe */ @Override public String toString() { return - "Recipe ID: " + recipe_id + - ", Name: " + name + - ", Category: " + category + - ", Cooking Time: " + cooking_time; + "Recipe ID: " + recipe_id + + ", Name: " + name + + ", Category: " + category + + ", Cooking Time: " + cooking_time; } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/RecipeIngredient.java b/src/main/java/no/ntnu/idatt1002/demo/data/RecipeIngredient.java index 11409196c2df86a56bf0ea09bd394dee56d59471..18f5409b13ecd8f54639497176c43748aa9320b2 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/RecipeIngredient.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/RecipeIngredient.java @@ -6,13 +6,14 @@ import java.util.List; /** * This class represents a recipe ingredient connected to a recipe. */ -public class RecipeIngredient extends ShoppingListItem{ +public class RecipeIngredient extends ShoppingListItem { private int recipeIngredient_id; - private int recipe_id; + private final int recipe_id; /** * Constructor for the RecipeIngredient class. + * * @param item_id the id of the item * @param name the name of the item * @param category the category of the item @@ -21,13 +22,16 @@ public class RecipeIngredient extends ShoppingListItem{ * @param unit the unit of the item * @param recipe_id the id of the recipe */ - public RecipeIngredient(int item_id, String name, String category, String allergy, int quantity, String unit, int recipe_id) { + public RecipeIngredient( + int item_id, String name, String category, String allergy, + int quantity, String unit, int recipe_id) { super(item_id, name, category, allergy, quantity, unit); this.recipe_id = recipe_id; } /** * Constructor for the RecipeIngredient class with recipe ingredient id. + * * @param recipeIngredient_id the id of the recipe ingredient * @param item_id the id of the item * @param name the name of the item @@ -37,13 +41,16 @@ public class RecipeIngredient extends ShoppingListItem{ * @param unit the unit of the item * @param recipe_id the id of the recipe */ - public RecipeIngredient(int recipeIngredient_id, int item_id, String name, String category, String allergy, int quantity, String unit, int recipe_id) { + public RecipeIngredient( + int recipeIngredient_id, int item_id, String name, String category, + String allergy, int quantity, String unit, int recipe_id) { this(item_id, name, category, allergy, quantity, unit, recipe_id); this.recipeIngredient_id = recipeIngredient_id; } /** * Returns the attributes of the recipe ingredient. + * * @return the attributes of the recipe ingredient */ @Override @@ -58,6 +65,7 @@ public class RecipeIngredient extends ShoppingListItem{ /** * Returns the attribute names of the recipe ingredient. + * * @return the attribute names of the recipe ingredient */ @Override @@ -72,19 +80,39 @@ public class RecipeIngredient extends ShoppingListItem{ /** * Returns the id of the recipe ingredient. + * * @return the id of the recipe ingredient */ @Override public int getId() { - return recipeIngredient_id; + return getRecipeIngredient_id(); } /** * Returns the id name of the recipe ingredient. + * * @return the id name of the recipe ingredient */ @Override public String getIdName() { return "recipeIngredient_id"; } + + /** + * Returns the recipe id of the recipe ingredient. + * + * @return the recipe id of the recipe ingredient + */ + public int getRecipe_id() { + return recipe_id; + } + + /** + * Returns the recipe ingredient id. + * + * @return the recipe ingredient id + */ + public int getRecipeIngredient_id() { + return recipeIngredient_id; + } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java b/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java index 07c95253f62abcdf45e133071ccccf7f27e80929..233417f1ef04dae19c0603d7640fd7964ea4c705 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java @@ -96,11 +96,11 @@ public class ShoppingListItem extends Item { */ @Override public int getId() { - return ShoppingListItem_id; + return getShoppingListItem_id(); } /** - * Returns the id of the item + * Returns the id of the item. * * @return the id of the item */ @@ -118,6 +118,10 @@ public class ShoppingListItem extends Item { return "shoppinglistitem_id"; } + public int getShoppingListItem_id() { + return ShoppingListItem_id; + } + /** * Getter method for the quantity of the shopping list item. * diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Storable.java b/src/main/java/no/ntnu/idatt1002/demo/data/Storable.java index 3545fb0f313710674af16746191381637e4f6425..587ad7babf6b7929057d9ad8a8ccbb1f8a249292 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Storable.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Storable.java @@ -10,24 +10,32 @@ public interface Storable { /** * This method returns the attribute names of the object. + * These should be the same as the column names in the database. + * * @return the attribute names of the object */ List<String> getAttributeNames(); /** * This method returns the attributes of the object. + * These should be the values to be stored in the columns of the database. + * * @return the attributes of the object */ List<String> getAttributes(); /** * This method returns the name of the id. + * This should be the same as the column name in the database. + * * @return the name of the id */ String getIdName(); /** * This method returns the id of the object. + * This should be the value to be stored in the id column of the database. + * * @return the id of the object */ int getId(); diff --git a/src/main/java/no/ntnu/idatt1002/demo/repo/EventRegister.java b/src/main/java/no/ntnu/idatt1002/demo/repo/EventRegister.java index 101e90cf15d4df900cf2efd80557d930a00f4047..463ecceea3822a110111dabd6e510a327e41924d 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/repo/EventRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/repo/EventRegister.java @@ -1,15 +1,16 @@ package no.ntnu.idatt1002.demo.repo; -import no.ntnu.idatt1002.demo.dao.DAO; -import no.ntnu.idatt1002.demo.data.Event; - import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import no.ntnu.idatt1002.demo.dao.DAO; +import no.ntnu.idatt1002.demo.data.Event; + /** - * This class represents a register for events. Allowing for communication between the database and the user interface. + * This class represents a register for events. + * Allowing for communication between the database and the user interface. */ public class EventRegister { private List<Event> events; @@ -17,6 +18,7 @@ public class EventRegister { /** * Constructor for the EventRegister class. + * * @param dao the data access object */ public EventRegister(DAO dao) { @@ -26,6 +28,7 @@ public class EventRegister { /** * This method returns the events in the register in the form of lists. + * * @return the events in the register as lists of strings */ public Map<Integer, List<String>> getEvents() { @@ -44,6 +47,7 @@ public class EventRegister { /** * This method adds an event to the database. + * * @param recipe_id the id of the recipe * @param date the date of the event */ @@ -54,6 +58,7 @@ public class EventRegister { /** * This method returns the index of the event with the given id. + * * @param id the id of the event * @return the index of the event */ @@ -68,6 +73,7 @@ public class EventRegister { /** * This method deletes an event from the database. + * * @param id the id of the event to be deleted */ public void deleteEvent(int id) { @@ -77,6 +83,7 @@ public class EventRegister { /** * This method updates an event in the database. + * * @param event_id the id of the event * @param recipe_id the id of the recipe * @param date the date of the event @@ -88,16 +95,19 @@ public class EventRegister { /** * This method gets the events by the date. + * * @param date the date of the event */ - public void getEventsByDate(int date) { + public void getEventsByDate(int date) { events = new ArrayList<>(); - List<List<String>> events = dao.filterFromTable("Event", "date", Integer.toString(date), "recipe", "recipe_id"); + List<List<String>> events = dao.filterFromTable( + "Event", "date", Integer.toString(date), "recipe", "recipe_id"); packageToEvent(events); } /** * Helper method to package the events into the Event class. + * * @param eventList the list of events */ private void packageToEvent(List<List<String>> eventList) { @@ -117,6 +127,7 @@ public class EventRegister { /** * This method returns a string representation of the events. + * * @return a string representation of the events */ @Override diff --git a/src/main/java/no/ntnu/idatt1002/demo/repo/Inventory.java b/src/main/java/no/ntnu/idatt1002/demo/repo/Inventory.java index db47a29a97c651811e1615baf0a6fe13db9ae112..20fe8a846e360acfbe9722c3022a555616a4d062 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/repo/Inventory.java +++ b/src/main/java/no/ntnu/idatt1002/demo/repo/Inventory.java @@ -6,7 +6,8 @@ import no.ntnu.idatt1002.demo.dao.DAO; import no.ntnu.idatt1002.demo.data.InventoryItem; /** - * This class represents a register for inventory items. Allowing for communication between the database and the user interface. + * This class represents a register for inventory items. + * Allowing for communication between the database and the user interface. */ public class Inventory { private List<InventoryItem> inventoryItems; @@ -14,6 +15,7 @@ public class Inventory { /** * Constructor for the Inventory class. + * * @param dao the database access object */ public Inventory(DAO dao) { @@ -23,6 +25,7 @@ public class Inventory { /** * This method returns the inventory items in the register in the form of lists. + * * @return the inventory items in the register as lists of strings */ public List<InventoryItem> getInventoryItems() { @@ -40,16 +43,19 @@ public class Inventory { /** * This method retrieves filtered inventory items by category from the database. + * * @param category the category to filter by */ public void filterInventoryByCategory(String category) { inventoryItems = new ArrayList<>(); - List<List<String>> inventoryItems = dao.filterFromTable("InventoryItem", "category", category, "item", "item_id"); + List<List<String>> inventoryItems = dao.filterFromTable( + "InventoryItem", "category", category, "item", "item_id"); packageToInventoryItems(inventoryItems); } /** * This method searches for inventory items by name and retrieves them from the database. + * * @param name the name to search by */ public void searchInventoryByName(String name) { @@ -60,6 +66,7 @@ public class Inventory { /** * This method packages lists of lists representing inventory items into InventoryItem objects. + * * @param inventoryItems the list of lists representing inventory items */ private void packageToInventoryItems(List<List<String>> inventoryItems) { @@ -79,6 +86,7 @@ public class Inventory { /** * This method adds a new inventory item to the database. + * * @param item_id the id of the item * @param quantity the quantity of the item * @param unit the unit of the item @@ -90,6 +98,7 @@ public class Inventory { /** * This method retrieves the index of an inventory item by its id. + * * @param id the id of the inventory item * @return the index of the inventory item */ @@ -104,6 +113,7 @@ public class Inventory { /** * This method deletes an inventory item from the database. + * * @param id the id of the inventory item to delete */ public void deleteInventoryItem(int id) { @@ -111,16 +121,19 @@ public class Inventory { dao.deleteFromDatabase(inventoryItems.get(index)); } - /** + /** * This method updates an inventory item in the database. + * * @param inventory_id the id of the inventory item * @param item_id the id of the item * @param quantity the quantity of the item * @param unit the unit of the item * @param expirationDate the expiration date of the item */ - public void updateInventoryItem(int inventory_id, int item_id, int quantity, String unit, int expirationDate) { - InventoryItem inventoryItem = new InventoryItem(inventory_id, item_id, null, null, null, quantity, unit, expirationDate); + public void updateInventoryItem( + int inventory_id, int item_id, int quantity, String unit, int expirationDate) { + InventoryItem inventoryItem = new InventoryItem( + inventory_id, item_id, null, null, null, quantity, unit, expirationDate); dao.updateDatabase(inventoryItem); } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/repo/ItemRegister.java b/src/main/java/no/ntnu/idatt1002/demo/repo/ItemRegister.java index 17687de6ffb0bf845574e19445fd8197dfc7901c..0903db8633fb388ac8a8f8d6ce8295260d5982f4 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/repo/ItemRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/repo/ItemRegister.java @@ -18,7 +18,7 @@ public class ItemRegister { /** * Constructor for the ItemRegister class. - * + * * @param dao the data access object */ public ItemRegister(DAO dao) { @@ -28,7 +28,7 @@ public class ItemRegister { /** * This method returns the items in the register in the form of lists. - * + * * @return the items in the register as lists of strings */ public Map<Integer, Item> getItems() { @@ -37,7 +37,7 @@ public class ItemRegister { /** * This method retrieves filtered items by category from the database. - * + * * @param category the category to filter by */ public void filterItemsByCategory(String category) { @@ -48,7 +48,7 @@ public class ItemRegister { /** * This method searches for items by name and retrieves them from the database. - * + * * @param name the name to search by */ public void searchItemsByName(String name) { @@ -71,13 +71,14 @@ public class ItemRegister { */ private void packageToItem(List<List<String>> items) { for (List<String> item : items) { - this.items.add(new Item(Integer.parseInt(item.get(0)), item.get(1), item.get(2), item.get(3))); + this.items.add( + new Item(Integer.parseInt(item.get(0)), item.get(1), item.get(2), item.get(3))); } } /** * This method adds a new item to the database. - * + * * @param name the name of the item * @param category the category of the item * @param allergy the allergy of the item @@ -88,7 +89,7 @@ public class ItemRegister { /** * This method deletes an item from the database. - * + * * @param id the id of the item to delete */ public void deleteItem(int id) { @@ -98,7 +99,7 @@ public class ItemRegister { /** * This method returns the index of the item with the given id. - * + * * @param id the id of the item * @return the index of the item with the given id */ @@ -113,7 +114,7 @@ public class ItemRegister { /** * This method updates an item in the database. - * + * * @param id the id of the item to update * @param name the name of the item * @param category the category of the item @@ -126,7 +127,7 @@ public class ItemRegister { /** * This method returns a string representation of the item register. - * + * * @return a string representation of the item register */ @Override diff --git a/src/main/java/no/ntnu/idatt1002/demo/repo/RecipeRegister.java b/src/main/java/no/ntnu/idatt1002/demo/repo/RecipeRegister.java index 0070627395e147e1bd2bbedf002c4f4fa084e7f2..e976deaca4be3cc341eecc254577b6ab28155a40 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/repo/RecipeRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/repo/RecipeRegister.java @@ -1,16 +1,16 @@ package no.ntnu.idatt1002.demo.repo; -import no.ntnu.idatt1002.demo.dao.DAO; -import no.ntnu.idatt1002.demo.data.Recipe; - import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import no.ntnu.idatt1002.demo.dao.DAO; +import no.ntnu.idatt1002.demo.data.Recipe; import no.ntnu.idatt1002.demo.data.RecipeIngredient; /** - * This class represents a register for recipes. Allowing for communication between the database and the user interface. + * This class represents a register for recipes. + * Allowing for communication between the database and the user interface. */ public class RecipeRegister { @@ -19,6 +19,7 @@ public class RecipeRegister { /** * Constructor for the RecipeRegister class. + * * @param dao the data access object */ public RecipeRegister(DAO dao) { @@ -28,6 +29,7 @@ public class RecipeRegister { /** * This method returns the recipes in the register in the form of lists. + * * @return the recipes in the register as lists of strings */ public Map<Integer, List<String>> getRecipes() { @@ -37,12 +39,16 @@ public class RecipeRegister { /** * This method packages the recipes into a list of recipes. + * * @param recipes the recipes to package */ private void packagetoRecipe(List<List<String>> recipes) { for (List<String> recipe : recipes) { - Recipe newRecipe = new Recipe(Integer.parseInt(recipe.get(0)), recipe.get(1), Integer.parseInt(recipe.get(2)), recipe.get(3)); - List<List<String>> ingredients = dao.filterFromTable("RecipeIngredient", "recipe_id", Integer.toString(newRecipe.getId()), "Item", "item_id"); + Recipe newRecipe = new Recipe( + Integer.parseInt(recipe.get(0)), recipe.get(1), + Integer.parseInt(recipe.get(2)), recipe.get(3)); + List<List<String>> ingredients = dao.filterFromTable( + "RecipeIngredient", "recipe_id", Integer.toString(newRecipe.getId()), "Item", "item_id"); for (List<String> ingredient : ingredients) { newRecipe.addIngredient(Integer.parseInt(ingredient.get(0)), Integer.parseInt(ingredient.get(1)), ingredient.get(6), ingredient.get(7), @@ -55,6 +61,7 @@ public class RecipeRegister { /** * This method retrieves filtered recipes by category from the database. + * * @param category the category to filter by */ public void filterRecipesByCategory(String category) { @@ -65,6 +72,7 @@ public class RecipeRegister { /** * This method searches for recipes by name and retrieves them from the database. + * * @param name the name to search by */ public void searchRecipesByName(String name) { @@ -84,6 +92,7 @@ public class RecipeRegister { /** * This method adds a recipe to the database. + * * @param name the name of the recipe * @param category the category of the recipe * @param cooking_time the cooking time of the recipe @@ -94,6 +103,7 @@ public class RecipeRegister { /** * This method retrieves the index of a recipe from the register by id. + * * @param id the id of the recipe * @return the index of the recipe */ @@ -108,6 +118,7 @@ public class RecipeRegister { /** * This method deletes a recipe from the database. + * * @param id the id of the recipe to delete */ public void deleteRecipe(int id) { @@ -122,19 +133,25 @@ public class RecipeRegister { /** * This method updates a recipe in the database. + * * @param name the name of the recipe * @param cooking_time the cooking time of the recipe * @param category the category of the recipe */ - public void updateRecipe(int recipe_id, String name, int cooking_time, String category ) { + public void updateRecipe(int recipe_id, String name, int cooking_time, String category) { Recipe recipe = new Recipe(recipe_id, name, cooking_time, category); dao.updateDatabase(recipe); } - public void addIngredient(int item_id,int quantity, String unit, int recipe_id) { + public void addIngredient(int item_id, int quantity, String unit, int recipe_id) { dao.addToDatabase(new RecipeIngredient(item_id, null, null, null, quantity, unit, recipe_id)); } + /** + * This method deletes an ingredient from the database. + * + * @param id the id of the ingredient to delete + **/ public void deleteIngredient(int id) { for (Recipe recipe : recipes) { if (recipe.getIngredientById(id) != null) { @@ -144,6 +161,14 @@ public class RecipeRegister { } } + /** + * This method updates an ingredient in the database. + * + * @param recipeIngredient_id the id of the recipe ingredient + * @param item_id the id of the item + * @param quantity the quantity of the item + * @param unit the unit of the item + */ public void updateIngredient(int recipeIngredient_id, int item_id, int quantity, String unit) { int recipe_id = -1; for (Recipe recipe : recipes) { @@ -162,6 +187,7 @@ public class RecipeRegister { /** * This method returns a string representation of the recipe register. + * * @return a string representation of the recipe register */ public String toString() { diff --git a/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingListItemRegister.java b/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingListItemRegister.java index d298733de622d167fe3b665f6109a3b1d7923cf9..6d9e538fad709973da3d6172bb40757c7354d77e 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingListItemRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingListItemRegister.java @@ -4,12 +4,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; - import no.ntnu.idatt1002.demo.Logger; import no.ntnu.idatt1002.demo.dao.DAO; -import no.ntnu.idatt1002.demo.data.Item; import no.ntnu.idatt1002.demo.data.ShoppingListItem; -import no.ntnu.idatt1002.demo.view.scenes.ShoppingList; /** * This class represents a register for shopping list items. @@ -30,7 +27,8 @@ public class ShoppingListItemRegister { * @return the items in the register as lists of strings */ public Map<Integer, ShoppingListItem> getItems() { - return shoppingListItems.stream().collect(Collectors.toMap(ShoppingListItem::getId, item -> item)); + return shoppingListItems.stream() + .collect(Collectors.toMap(ShoppingListItem::getId, item -> item)); } @@ -54,8 +52,10 @@ public class ShoppingListItemRegister { */ private void packageToShoppingListItem(List<List<String>> items) { for (List<String> item : items) { - List<List<String>> matchingItem = dao.filterFromTable("Item", "item_id", item.get(1), null, null); - Logger.warning("Package Item: " + item.toString() + " Matching Item: " + matchingItem.toString()); + List<List<String>> matchingItem = dao.filterFromTable( + "Item", "item_id", item.get(1), null, null); + Logger.warning("Package Item: " + item.toString() + " Matching Item: " + + matchingItem.toString()); shoppingListItems.add(new ShoppingListItem( Integer.parseInt(item.get(0)), // shoppingListItem_id Integer.parseInt(item.get(1)), // item_id @@ -76,7 +76,8 @@ public class ShoppingListItemRegister { */ public void addToShoppingList(int item_id, int quantity, String unit) { dao.addToDatabase(new ShoppingListItem(item_id, null, null, null, quantity, unit)); - Logger.info("Added item to shopping list with id " + item_id + ", quantity " + quantity + " and unit " + unit + "."); //TODO remove + Logger.info("Added item to shopping list with id " + + item_id + ", quantity " + quantity + " and unit " + unit + "."); //TODO remove } /** @@ -122,7 +123,9 @@ public class ShoppingListItemRegister { /** * Method to search for items in the shopping list by name. - * <p>Uses the {@link #packageToShoppingListItem(List) packageToShoppingListItem} to create ShoppingListItem instances from the returned list of data from the {@link DAO#searchFromTable(String, String, String, String) DAO.searchFromTable}</p> + * <p>Uses the {@link #packageToShoppingListItem(List) packageToShoppingListItem} + * to create ShoppingListItem instances from the returned list of data from the + * {@link DAO#searchFromTable(String, String, String, String) DAO.searchFromTable}</p> * * @param name the name of the item to search for */