From c39b245a5f0bc831662a3addfd835a22d475c68c Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Fri, 15 Mar 2024 14:18:21 +0100
Subject: [PATCH 1/7] added second constructor to ShoppingListItem

---
 .../idatt1002/demo/data/ShoppingListItem.java     | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

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 daeae6a..f2bfbb7 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
@@ -27,6 +27,21 @@ public class ShoppingListItem extends Item {
     this.unit = unit;
   }
 
+  /**
+   * Constructor for the ShoppingListItem class. When creating a ShoppingListItem from the database, the id is known.
+   * @param ShoppingListItem_id the id of the ShoppingListItem
+   * @param item_id the id of the item
+   * @param name the name of the item
+   * @param category  the category of the item
+   * @param allergy the allergy of the item
+   * @param quantity the quantity of the item
+   * @param unit the unit of the item
+   */
+  public ShoppingListItem(int ShoppingListItem_id, int item_id, String name, String category, String allergy, int quantity, String unit) {
+    this(item_id, name, category, allergy, quantity, unit);
+    this.ShoppingListItem_id = ShoppingListItem_id;
+  }
+
   /**
    * This method returns the attributes of the shopping list item.
    * @return the attributes of the quantity item
-- 
GitLab


From 68916f81b0e23347fa731a828868b00e1772b53d Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Fri, 15 Mar 2024 14:35:07 +0100
Subject: [PATCH 2/7] added getAllAtributes method in ShoppingListItem

---
 .../ntnu/idatt1002/demo/data/ShoppingListItem.java  | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

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 f2bfbb7..e703aa6 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
@@ -43,7 +43,7 @@ public class ShoppingListItem extends Item {
   }
 
   /**
-   * This method returns the attributes of the shopping list item.
+   * This method returns the attributes of the shopping list item as saved in the database.
    * @return the attributes of the quantity item
    */
   @Override
@@ -86,6 +86,17 @@ public class ShoppingListItem extends Item {
     return "shoppinglistitem_id";
   }
 
+  /**
+   * This method returns all the attributes of the shopping list item.
+   * @return all the attributes of the quantity item
+   */
+  public List<String> getAllAttributes() {
+    List<String> attributes = new ArrayList<>(super.getAttributes());
+    attributes.add(Integer.toString(quantity));
+    attributes.add(unit);
+    return attributes;
+  }
+
   /**
    * Getter method for the quantity of the shopping list item.
    * @return the quantity of the quantity item
-- 
GitLab


From ac9873d51891baf744696e044cbcedda07db7f4c Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Mon, 18 Mar 2024 11:03:30 +0100
Subject: [PATCH 3/7] fixed checkstyle in ShoppingListItem

---
 .../idatt1002/demo/data/ShoppingListItem.java | 36 ++++++++++++++-----
 1 file changed, 27 insertions(+), 9 deletions(-)

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 e703aa6..2f550ab 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
@@ -13,22 +13,27 @@ public class ShoppingListItem extends Item {
   private final String unit;
 
   /**
-   * Constructor for the ShoppingListItem class. When creating a new ShoppingListItem, it is required that there is an
+   * Constructor for the ShoppingListItem class.
+   * When creating a new ShoppingListItem, it is required that there is an
    * item in the database with the same name, category and allergy.
+   *
    * @param name the name of the item
    * @param category the category of the item
    * @param allergy the allergy of the item
    * @param quantity the quantity of the item
    * @param unit the unit of the item
    */
-  public ShoppingListItem(int item_id, String name, String category, String allergy, int quantity, String unit) {
+  public ShoppingListItem(
+      int item_id, String name, String category, String allergy, int quantity, String unit) {
     super(item_id, name, category, allergy);
     this.quantity = quantity;
     this.unit = unit;
   }
 
   /**
-   * Constructor for the ShoppingListItem class. When creating a ShoppingListItem from the database, the id is known.
+   * Constructor for the ShoppingListItem class.
+   * When creating a ShoppingListItem from the database, the id is known.
+   *
    * @param ShoppingListItem_id the id of the ShoppingListItem
    * @param item_id the id of the item
    * @param name the name of the item
@@ -37,13 +42,20 @@ public class ShoppingListItem extends Item {
    * @param quantity the quantity of the item
    * @param unit the unit of the item
    */
-  public ShoppingListItem(int ShoppingListItem_id, int item_id, String name, String category, String allergy, int quantity, String unit) {
+  public ShoppingListItem(int ShoppingListItem_id,
+                          int item_id,
+                          String name,
+                          String category,
+                          String allergy,
+                          int quantity,
+                          String unit) {
     this(item_id, name, category, allergy, quantity, unit);
     this.ShoppingListItem_id = ShoppingListItem_id;
   }
 
   /**
-   * This method returns the attributes of the shopping list item as saved in the database.
+   * Returns the attributes of the shopping list item as saved in the database.
+   *
    * @return the attributes of the quantity item
    */
   @Override
@@ -56,7 +68,8 @@ public class ShoppingListItem extends Item {
   }
 
   /**
-   * This method returns the attribute names of the shopping list item.
+   * Returns the attribute names of the shopping list item.
+   *
    * @return the attribute names of the quantity item
    */
   @Override
@@ -69,7 +82,8 @@ public class ShoppingListItem extends Item {
   }
 
   /**
-   * This method returns the id of the shopping list item.
+   * Returns the id of the shopping list item.
+   *
    * @return the id of the quantity item
    */
   @Override
@@ -78,7 +92,8 @@ public class ShoppingListItem extends Item {
   }
 
   /**
-   * This method returns the name of the id.
+   * Returns the name of the id.
+   *
    * @return the name of the id
    */
   @Override
@@ -87,7 +102,8 @@ public class ShoppingListItem extends Item {
   }
 
   /**
-   * This method returns all the attributes of the shopping list item.
+   * Returns all the attributes of the shopping list item.
+   *
    * @return all the attributes of the quantity item
    */
   public List<String> getAllAttributes() {
@@ -99,6 +115,7 @@ public class ShoppingListItem extends Item {
 
   /**
    * Getter method for the quantity of the shopping list item.
+   *
    * @return the quantity of the quantity item
    */
   public int getQuantity() {
@@ -107,6 +124,7 @@ public class ShoppingListItem extends Item {
 
   /**
    * Getter method for the unit of the shopping list item.
+   *
    * @return the unit of the quantity item
    */
   public String getUnit() {
-- 
GitLab


From b1bc4f9a0725c3a02cfe87d8d6e6ce1d1a25a493 Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Mon, 18 Mar 2024 12:49:25 +0100
Subject: [PATCH 4/7] updated getAllFromTable in DAO to support joining two
 tables

---
 src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

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 fb10366..d8e97c0 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/dao/DAO.java
@@ -85,13 +85,22 @@ private final DBConnectionProvider connectionProvider;
    * @param table the table to get attributes from
    * @return a list of lists containing all attributes from a table
    */
-  public List<List<String>> getAllFromTable(String table) {
+  public List<List<String>> getAllFromTable(String table, String joinTable, String joinColumn) {
     List<List<String>> items = new ArrayList<>();
-    String sql = "SELECT * FROM " + table;
+    String sql;
+    if (joinTable == null) {
+      sql = "SELECT * FROM " + table;
+    } else {
+      sql = "SELECT * FROM " + table +
+          " JOIN " + joinTable + " ON " + table + "." + joinColumn + " = " + joinTable + "." +  joinColumn;
+    }
     try (Connection connection = connectionProvider.getConnection();
          PreparedStatement preparedStatement = connection.prepareStatement(sql);
          ResultSet resultSet = preparedStatement.executeQuery()) {
       List<String> columns = getColumnsFromTable(table);
+      if (joinTable != null) {
+        columns.addAll(getColumnsFromTable(joinTable));
+      }
       while (resultSet.next()) {
         List<String> item = new ArrayList<>();
         for (String column : columns) {
-- 
GitLab


From c395b96d70eebda7ba16e0386ce07243a51d9a69 Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Tue, 19 Mar 2024 10:05:00 +0100
Subject: [PATCH 5/7] edited ItemRegister to use modified getAllFromTable
 method from DAO

---
 .../java/no/ntnu/idatt1002/demo/repo/ItemRegister.java | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

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 f5bee45..38e9d90 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/repo/ItemRegister.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/repo/ItemRegister.java
@@ -40,7 +40,7 @@ public class ItemRegister {
   public void filterItemsByCategory(String category) {
     items = new ArrayList<>();
     List<List<String>> items = dao.filterFromTable("Item", "category", category, null, null);
-    packagetoItem(items);
+    packageToItem(items);
   }
 
   /**
@@ -50,7 +50,7 @@ public class ItemRegister {
   public void searchItemsByName(String name) {
     items = new ArrayList<>();
     List<List<String>> items = dao.searchFromTable("Item", name, null, null);
-    packagetoItem(items);
+    packageToItem(items);
   }
 
   /**
@@ -58,14 +58,14 @@ public class ItemRegister {
    */
   public void getAllItems() {
     items = new ArrayList<>();
-    List<List<String>> items = dao.getAllFromTable("Item");
-    packagetoItem(items);
+    List<List<String>> items = dao.getAllFromTable("Item", null, null);
+    packageToItem(items);
   }
 
   /**
    * This method packages the lists of strings into items.
    */
-  private void packagetoItem(List<List<String>> items) {
+  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)));
     }
-- 
GitLab


From c12ee28dfdd46a41527dc5e4272d7481295e0a2b Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Tue, 19 Mar 2024 10:05:21 +0100
Subject: [PATCH 6/7] Created Shoppinglist class

---
 .../idatt1002/demo/repo/ShoppingList.java     | 111 ++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100644 src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingList.java

diff --git a/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingList.java b/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingList.java
new file mode 100644
index 0000000..542cbca
--- /dev/null
+++ b/src/main/java/no/ntnu/idatt1002/demo/repo/ShoppingList.java
@@ -0,0 +1,111 @@
+package no.ntnu.idatt1002.demo.repo;
+
+import java.util.ArrayList;
+import java.util.List;
+import no.ntnu.idatt1002.demo.dao.DAO;
+import no.ntnu.idatt1002.demo.data.ShoppingListItem;
+
+/**
+ * This class represents a register for shopping list items.
+ * Allowing for communication between the database and the user interface.
+ */
+public class ShoppingList {
+  private List<ShoppingListItem> shoppingListItems;
+  private final DAO dao;
+
+  public ShoppingList(DAO dao) {
+    this.dao = dao;
+    this.shoppingListItems =  new ArrayList<>();
+  }
+
+  /**
+   * This method retrieves all the shopping list items from the register and returns them as a list.
+   *
+   * @return the shopping list items in the register as a map
+   */
+  public List<ShoppingListItem> getShoppingListItems() {
+    return shoppingListItems;
+  }
+
+
+  /**
+   * Gets all shopping list items from the database and
+   * packages them into a list of shopping list items.
+   */
+  public void getAllShoppingListItems() {
+    shoppingListItems = new ArrayList<>();
+    List<List<String>> items = dao.getAllFromTable("ShoppingListItem", "Item", "item_id");
+    packageToShoppingListItem(items);
+  }
+
+  /**
+   * Helper method to package the shopping list items into a list of shopping list items.
+   *
+   * @param items the items to package
+   */
+  private void packageToShoppingListItem(List<List<String>> items) {
+    for (List<String> item : items) {
+      shoppingListItems.add(new ShoppingListItem(
+          Integer.parseInt(item.get(0)),
+          Integer.parseInt(item.get(1)),
+          item.get(5),
+          item.get(6),
+          item.get(7),
+          Integer.parseInt(item.get(2)),
+          item.get(3)));
+    }
+  }
+
+  /**
+   * Method for adding a shopping list 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
+   */
+  public void addToShoppingList(int item_id,  int quantity, String unit) {
+    dao.addToDatabase(new ShoppingListItem(item_id, null, null, null, quantity, unit));
+  }
+
+  /**
+   * Method for deleting a shopping list item from the database.
+   *
+   * @param id the id of the shopping list item to delete
+   */
+  public void deleteFromShoppingList(int id) {
+    int index = getShoppingListItemFromId(id);
+    dao.deleteFromDatabase(shoppingListItems.get(index));
+  }
+
+  /**
+   * Helper method to get the index of the shopping list item with the given id.
+   *
+   * @param id the id of the shopping list item
+   * @return the index of the shopping list item with the given id
+   */
+  private int getShoppingListItemFromId(int id) {
+    for (ShoppingListItem item : shoppingListItems) {
+      if (item.getId() == id) {
+        return shoppingListItems.indexOf(item);
+      }
+    }
+    return -1;
+  }
+
+  /**
+   * Method for updating a shopping list item in the database.
+   *
+   * @param id the id of the shopping list item to update
+   * @param item_id the id of the item
+   * @param name the name of the item
+   * @param category the category of the item
+   * @param allergy the allergy of the item
+   * @param quantity the quantity of the item
+   * @param unit the unit of the item
+   */
+  public void updateShoppingListItem(int id, int item_id, String name, String category,
+                                     String allergy, int quantity, String unit) {
+    dao.updateDatabase(new ShoppingListItem(id, item_id, name, category, allergy, quantity, unit));
+  }
+
+}
-- 
GitLab


From 0fabd0dcd8154ae94b2f731fd7eb02fc1b279df4 Mon Sep 17 00:00:00 2001
From: florianleucht <fsleucht@stud.ntnu.no>
Date: Tue, 19 Mar 2024 10:13:09 +0100
Subject: [PATCH 7/7] Removed unused method

---
 .../ntnu/idatt1002/demo/data/ShoppingListItem.java   | 12 ------------
 1 file changed, 12 deletions(-)

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 2f550ab..d17e337 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/ShoppingListItem.java
@@ -101,18 +101,6 @@ public class ShoppingListItem extends Item {
     return "shoppinglistitem_id";
   }
 
-  /**
-   * Returns all the attributes of the shopping list item.
-   *
-   * @return all the attributes of the quantity item
-   */
-  public List<String> getAllAttributes() {
-    List<String> attributes = new ArrayList<>(super.getAttributes());
-    attributes.add(Integer.toString(quantity));
-    attributes.add(unit);
-    return attributes;
-  }
-
   /**
    * Getter method for the quantity of the shopping list item.
    *
-- 
GitLab