From 58f5e66b42d923e41f1fcbfc778ad67b6c0196c3 Mon Sep 17 00:00:00 2001
From: HSoreide <sofie.scisly@gmail.com>
Date: Sun, 5 Mar 2023 11:08:54 +0100
Subject: [PATCH] Add date as a property to Item

---
 .../no/ntnu/idatt1002/demo/data/Expense.java  | 10 ++--
 .../no/ntnu/idatt1002/demo/data/Income.java   | 12 +++--
 .../idatt1002/demo/data/IncomeCategory.java   |  3 +-
 .../no/ntnu/idatt1002/demo/data/Item.java     | 50 +++++++++++++++----
 .../ntnu/idatt1002/demo/data/ExpenseTest.java |  6 +--
 .../ntnu/idatt1002/demo/data/IncomeTest.java  |  6 +--
 .../no/ntnu/idatt1002/demo/data/ItemTest.java |  7 +--
 7 files changed, 66 insertions(+), 28 deletions(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java b/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java
index 06d8d716..15a59f7e 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java
@@ -11,9 +11,10 @@ public class Expense extends Item{
      * @param amount The amount of the current Expense object as a double.
      * @param recurring True if the current Expense repeats at regular intervals.
      * @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory.
+     * @param date The date of the Expense at format "dd.mm.yy".
      */
-    public Expense(double amount, boolean recurring, ExpenseCategory category) {
-        super(amount, recurring);
+    public Expense(double amount, boolean recurring, ExpenseCategory category, String date) {
+        super(amount, recurring, date);
 
         if(category == null) {
             throw new IllegalArgumentException("The income must belong to a category.");
@@ -28,9 +29,10 @@ public class Expense extends Item{
      * @param amount The amount of the current Expense object as a double.
      * @param recurring True if the current income repeats at regular intervals.
      * @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory
+     * @param date The date of the Expense at format "dd.mm.yy".
      */
-    public Expense(String description, double amount, boolean recurring, ExpenseCategory category) {
-        super(description, amount, recurring);
+    public Expense(String description, double amount, boolean recurring, ExpenseCategory category, String date) {
+        super(description, amount, recurring, date);
         if(category == null) {
             throw new IllegalArgumentException("The income must belong to a category.");
         }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Income.java b/src/main/java/no/ntnu/idatt1002/demo/data/Income.java
index 20bab51c..10e2f7ae 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Income.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Income.java
@@ -18,9 +18,10 @@ public class Income extends Item{
      * @param amount The amount of the current income object as a double.
      * @param recurring True if the current income repeats at regular intervals.
      * @param category The category to which the Income belongs to, provided as a value of IncomeCategory.
+     * @param date The date of the Income at format "dd.mm.yy".
      */
-    public Income(double amount, boolean recurring, IncomeCategory category) {
-        super(amount, recurring);
+    public Income(double amount, boolean recurring, IncomeCategory category, String date) {
+        super(amount, recurring, date);
 
         if(category == null) {
             throw new IllegalArgumentException("The income must belong to a category.");
@@ -34,10 +35,11 @@ public class Income extends Item{
      * @param description A description of the income as a String.
      * @param amount The amount of the current income object as a double.
      * @param recurring True if the current income repeats at regular intervals.
-     * @param category The category to which the income belongs to, provided as a value of IncomeCategory
+     * @param category The category to which the income belongs to, provided as a value of IncomeCategory.
+     * @param date The date of the Income at format "dd.mm.yy".
      */
-    public Income(String description, double amount, boolean recurring, IncomeCategory category) {
-        super(description, amount, recurring);
+    public Income(String description, double amount, boolean recurring, IncomeCategory category, String date) {
+        super(description, amount, recurring, date);
         if(category == null) {
             throw new IllegalArgumentException("The income must belong to a category.");
         }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java b/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java
index e749c19c..1ead5339 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java
@@ -2,5 +2,6 @@ package no.ntnu.idatt1002.demo.data;
 
 public enum IncomeCategory {
     SALARY,
-    STUDENT_LOAN
+    STUDENT_LOAN,
+    GIFT
 }
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 41a6c851..562b2999 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java
@@ -1,8 +1,8 @@
 package no.ntnu.idatt1002.demo.data;
 
 /**
- * The Item class represents an object or service purchased in real life. The item belongs to a category and
- * has a price and description. The description may be left blank, but the Item must belong to a category and must
+ * 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
  * have a price.
  * @author HanneSofie
  *
@@ -10,19 +10,21 @@ package no.ntnu.idatt1002.demo.data;
 public class Item {
     private String description = "";
     private double amount;
-    private final boolean recurring;
+    private boolean recurring;
+    private String date;      // Format example: 09.08.23
 
     /**
      * The constructor of a new Item object takes in an amount as a double. If the amount is left blank, an
      * IllegalArgumentException is thrown.
      * @param amount price of an Item as a float.
      */
-    public Item (double amount, boolean recurring){
-        if(amount <= 1.0f) {
-            throw new IllegalArgumentException("The item must have an amount.");
+    public Item (double amount, boolean recurring, String date){
+        if(amount <= 1.0f || date.isBlank()) {
+            throw new IllegalArgumentException("Both amount and date must be provided.");
         } else {
             this.amount = amount;
             this.recurring = recurring;
+            this.date = date;
         }
     }
 
@@ -33,8 +35,8 @@ public class Item {
      * @param description A description of the item as a String.
      * @param amount The price of the item as a float.
      */
-    public Item (String description, double amount, boolean recurring){
-       this(amount, recurring);
+    public Item (String description, double amount, boolean recurring, String date){
+       this(amount, recurring, date);
        this.description=description;
     }
 
@@ -67,6 +69,9 @@ public class Item {
      * @param amount The new price of the Item as a float.
      */
     public void setAmount(double amount) {
+        if(amount <= 1.0f ) {
+            throw new IllegalArgumentException("A positive amount must be provided.");
+        }
         this.amount = amount;
     }
 
@@ -78,7 +83,34 @@ public class Item {
         return recurring;
     }
 
-/*    @Override
+    /**
+     * The method changes the boolean value of the 'recurring' field of the Item object.
+     * @param recurring A boolean value being true if the Item is recurring and false otherwise.
+     */
+    public void setRecurring(boolean recurring) {
+        this.recurring = recurring;
+    }
+
+    /**
+     * The method returns the date of the item object (income/expense).
+     * @return The date of the transaction.
+     */
+    public String getDate() {
+        return date;
+    }
+
+    /**
+     * Sets the date field of the Item object to a new String provided as an argument.
+     * @param newDate The new date with which to record the Item.
+     */
+    public void setDate(String newDate) {
+        if(date.isBlank()) {
+            throw new IllegalArgumentException("A date must be provided.");
+        }
+        this.date = newDate;
+    }
+
+    /*    @Override
     public boolean equals(Object obj) {
         if (this == obj) {
             return true;
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java
index 52387d92..e72597e9 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java
@@ -10,14 +10,14 @@ class ExpenseTest {
     @Test
     @DisplayName("The Expense constructor throws exceptions when it should.")
     void constructorThrows(){
-        assertThrows(IllegalArgumentException.class, () -> new Expense("description", 8.5f, false, null));
-        assertThrows(IllegalArgumentException.class, () -> new Expense("description", -10.0f, false, ExpenseCategory.BOOKS));
+        assertThrows(IllegalArgumentException.class, () -> new Expense("description", 8.5f, false, null, "03.03.23"));
+        assertThrows(IllegalArgumentException.class, () -> new Expense("description", -10.0f, false, ExpenseCategory.BOOKS, "03.03.23"));
     };
 
     @Test
     @DisplayName("The Expense constructor does not throw exceptions when it should not.")
     void constructorDoesThrow() {
-        assertDoesNotThrow(() -> new Expense("description", 59.9f, false,  ExpenseCategory.BOOKS));
+        assertDoesNotThrow(() -> new Expense("description", 59.9f, false,  ExpenseCategory.BOOKS, "03.03.23"));
     }
 
 }
\ No newline at end of file
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java
index 71071f50..6e711804 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java
@@ -10,14 +10,14 @@ class IncomeTest {
     @Test
     @DisplayName("The Income constructor throws exceptions when it should.")
     void constructorThrows(){
-        assertThrows(IllegalArgumentException.class, () -> new Income("description", 8.5f, false, null));
-        assertThrows(IllegalArgumentException.class, () -> new Income("description", -10.0f, false, IncomeCategory.SALARY));
+        assertThrows(IllegalArgumentException.class, () -> new Income("description", 8.5f, false, null, "03.03.23"));
+        assertThrows(IllegalArgumentException.class, () -> new Income("description", -10.0f, false, IncomeCategory.SALARY, "03.03.23"));
     };
 
     @Test
     @DisplayName("The Income constructor does not throw exceptions when it should not.")
     void constructorDoesThrow() {
-        assertDoesNotThrow(() -> new Income("description", 59.9f, false, IncomeCategory.SALARY));
+        assertDoesNotThrow(() -> new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
     }
 
 
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java
index 0d3d0a08..7ab5a18f 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java
@@ -10,14 +10,15 @@ class ItemTest {
     @Test
     @DisplayName("The Item constructor throws exceptions when it should.")
     void constructorThrows(){
-        assertThrows(IllegalArgumentException.class, () -> new Item("description", 0f, false));
-        assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false));
+        assertThrows(IllegalArgumentException.class, () -> new Item("description", 0f, false, "03.03.23"));
+        assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false, "03.03.23"));
+        assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false, ""));
     };
 
     @Test
     @DisplayName("The Item constructor does not throw exceptions when it should not.")
     void constructorDoesThrow() {
-        assertDoesNotThrow(() -> new Item("description", 59.9f, false));
+        assertDoesNotThrow(() -> new Item("description", 59.9f, false, "03.03.23"));
     }
 
 }
\ No newline at end of file
-- 
GitLab