From af4ba34b6848494659a0d2955d66a20d7706b9fb Mon Sep 17 00:00:00 2001
From: Andreas <andreksv@ntnu.no>
Date: Tue, 14 Mar 2023 15:08:17 +0100
Subject: [PATCH] Added Equals method

---
 .../demo/data/Economics/ExpenseOverview.java  | 23 --------
 .../demo/data/Economics/IncomeOverview.java   | 26 ---------
 .../idatt1002/demo/data/Economics/Item.java   | 14 +++++
 .../{ItemOverview.java => ItemRegister.java}  | 27 +++++----
 .../data/Economics/IncomeOverviewTest.java    | 43 --------------
 .../demo/data/Economics/ItemRegisterTest.java | 58 +++++++++++++++++++
 6 files changed, 85 insertions(+), 106 deletions(-)
 delete mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseOverview.java
 delete mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverview.java
 rename src/main/java/no/ntnu/idatt1002/demo/data/Economics/{ItemOverview.java => ItemRegister.java} (59%)
 delete mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverviewTest.java
 create mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java

diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseOverview.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseOverview.java
deleted file mode 100644
index 7dff5877..00000000
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseOverview.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package no.ntnu.idatt1002.demo.data.Economics;
-
-import java.util.ArrayList;
-
-/**
- *ExpenseOverview is an abstract class for storing and getting
- * information on Expense.
- */
-public class ExpenseOverview {
-    private ArrayList<Expense> expenses;
-
-    public ExpenseOverview(){
-        this.expenses = new ArrayList<>();
-    }
-
-    /**
-     * Class constructor that takes in an ArrayList of Expense´s as argument
-     * @param expenses   An ArrayList of the Expense´s you want to overview
-     */
-    public ExpenseOverview(ArrayList<Expense> expenses){
-        this.expenses = expenses;
-    }
-}
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverview.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverview.java
deleted file mode 100644
index 1257d12d..00000000
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverview.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package no.ntnu.idatt1002.demo.data.Economics;
-
-import java.util.ArrayList;
-
-/**
- * IncomeOverview is an abstract class for storing and getting
- * information on income.
- */
-public class IncomeOverview extends ItemOverview {
-    private ArrayList<Income> income;
-
-    /**
-     * An "empty" class constructor.
-     */
-    public IncomeOverview(){
-        this.income = new ArrayList<>();
-    }
-
-    /**
-     * Class constructor that takes in an ArrayList of Income as argument
-     * @param income   An ArrayList of the Income you want to overview
-     */
-    public IncomeOverview(ArrayList<Income> income){
-        this.income = income;
-    }
-}
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Item.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Item.java
index ba80deff..818d21f3 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Item.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Item.java
@@ -1,5 +1,7 @@
 package no.ntnu.idatt1002.demo.data.Economics;
 
+import java.util.Objects;
+
 /**
  * The Item class represents a good or service purchased in real life. The item belongs to a category and
  * has an amount, description and a date. The description may be left blank, but the Item must belong to a category and must
@@ -110,6 +112,18 @@ public abstract class Item {
         this.date = newDate;
     }
 
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (!(o instanceof Item item)) return false;
+        return Double.compare(item.amount, amount) == 0 && recurring == item.recurring && Objects.equals(description, item.description) && Objects.equals(date, item.date);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(description, amount, recurring, date);
+    }
+
     /*    @Override
     public boolean equals(Object obj) {
         if (this == obj) {
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemOverview.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
similarity index 59%
rename from src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemOverview.java
rename to src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
index c16e9633..7d72314c 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemOverview.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
@@ -6,48 +6,47 @@ import java.util.ArrayList;
  * ItemOverview is an abstract class for storing and getting.
  * information on items. Superclass for Income- and ExpenseOverview.
  */
-public abstract class ItemOverview {
-    ArrayList<Item> items;
+public class ItemOverview<T>{
+    ArrayList<T> items;
 
     /**
      * An "empty" class constructor.
      */
-    public ItemOverview(){
+    public ItemOverview() {
         this.items = new ArrayList<>();   //ArrayList for storing item´s
     }
 
     /**
      * Class constructor that takes in an ArrayList of Item´s as argument.
-     * @param items   An ArrayList of the Item´s you want to overview.
+     * @param items An ArrayList of the Item´s you want to overview.
      */
-    public ItemOverview(ArrayList<Item> items){
+    public ItemOverview(ArrayList<T> items) {
         this.items = items;
     }
 
     /**
      * Get an ArrayList of every item.
-     * @return   item ArrayList.
+     * @return item ArrayList.
      */
-    public ArrayList<Item> getItems() {
+    public ArrayList<T> getItems() {
         return items;
     }
 
-    /**
-     * Add an Item object to items.
-     * @param newItem   The Item you want to add.
-     */
-    public void addItem(Item newItem){
+    public void addItem(T newItem){
         if(items.contains(newItem)){
             throw new IllegalArgumentException("This item is already registered");
         }
         items.add(newItem);
     }
 
+
+
     /**
      * Get the sum of all Item´s in items.
-     * @return   Sum of all Item´s.
+     * @return Sum of all Item´s.
      */
     public double getTotalSum(){
-        return items.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum();
+        ArrayList<Item> castedItems = (ArrayList<Item>) items;
+        return castedItems.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum();
     }
 }
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverviewTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverviewTest.java
deleted file mode 100644
index d6745c0d..00000000
--- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeOverviewTest.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package no.ntnu.idatt1002.demo.data.Economics;
-
-import no.ntnu.idatt1002.demo.data.Economics.Income;
-import no.ntnu.idatt1002.demo.data.Economics.IncomeCategory;
-import no.ntnu.idatt1002.demo.data.Economics.IncomeOverview;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.DisplayName;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-public class IncomeOverviewTest {
-    IncomeOverview incomeOverview;
-
-    @BeforeEach
-    public void createItemOverView(){
-        incomeOverview = new IncomeOverview();
-    }
-    @Test
-    @DisplayName("addItem method throws exception when it should")
-    void addItemThrows(){
-        Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
-        assertThrows(IllegalArgumentException.class, () -> {incomeOverview.addItem(income); incomeOverview.addItem(income);});
-    }
-
-    @Test
-    @DisplayName("addItem method does not throw exception when it should not")
-    void addItemDoesNotThrow(){
-        Income expense = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
-        Income income = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
-        assertDoesNotThrow(() -> {incomeOverview.addItem(expense); incomeOverview.addItem(income);});
-    }
-
-    @Test
-    @DisplayName("getTotalSum method gives correct amount")
-    void getTotalSumCorrectAmount(){
-        incomeOverview.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
-        incomeOverview.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"));
-        incomeOverview.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
-        double totalIncome = 59.9f + 62.4f + 9.81f;
-        assertEquals(Math.round(incomeOverview.getTotalSum()), Math.round(totalIncome));
-    }
-}
\ No newline at end of file
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java
new file mode 100644
index 00000000..de3ceaf6
--- /dev/null
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java
@@ -0,0 +1,58 @@
+package no.ntnu.idatt1002.demo.data.Economics;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class IncomeOverviewTest {
+    ItemRegister<Income> incomeRegister;
+
+    @BeforeEach
+    public void createItemOverView(){
+        incomeRegister = new ItemRegister();
+    }
+    @Test
+    @DisplayName("addItem method throws exception when it should")
+    void addItemThrows(){
+        Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
+        assertThrows(IllegalArgumentException.class, () -> {
+            incomeRegister.addItem(income);
+            incomeRegister.addItem(income);});
+    }
+
+    @Test
+    @DisplayName("addItem method does not throw exception when it should not")
+    void addItemDoesNotThrow(){
+        Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
+        Income income2 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
+        assertDoesNotThrow(() -> {
+            incomeRegister.addItem(income1); incomeRegister.addItem(income2);});
+    }
+
+    @Test
+    @DisplayName("getTotalSum method gives correct amount")
+    void getTotalSumCorrectAmount(){
+        incomeRegister.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
+        incomeRegister.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"));
+        incomeRegister.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
+        double totalIncome = 59.9f + 62.4f + 9.81f;
+        System.out.println(incomeRegister.getTotalSum());
+        assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome));
+    }
+
+    @Test
+    void getIncomeBasedOnCategoryGivesCorrectIncome(){
+        incomeRegister.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
+        incomeRegister.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"));
+        incomeRegister.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
+        ArrayList<Income> incomeCategory = new ArrayList<>();
+        incomeCategory.add(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
+        incomeCategory.add(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
+        assertEquals(incomeRegister.getItemsBasedOnCategory(IncomeCategory.SALARY), incomeCategory);
+    }
+}
+ 
\ No newline at end of file
-- 
GitLab