From 3cca4c9b3228987a214401e9d721c9452effc25b Mon Sep 17 00:00:00 2001
From: Andreas <andreksv@ntnu.no>
Date: Mon, 20 Mar 2023 14:09:22 +0100
Subject: [PATCH] Made method removeItem in ItemRegister and tests

---
 .../demo/data/Economics/FileHandling.java     | 76 ++++++++++++-------
 .../demo/data/Economics/ItemRegister.java     | 25 ++++--
 .../data/Economics/ExpenseRegisterTest.java   | 54 +++++++++----
 .../demo/data/Economics/FileHandlingTest.java |  1 -
 .../data/Economics/IncomeRegisterTest.java    | 57 ++++++++++----
 5 files changed, 147 insertions(+), 66 deletions(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java
index 390906b5..bfbe5ab7 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java
@@ -10,7 +10,15 @@ import java.io.*;
  * FileHandling is a class for writing and reading
  * Item-objects to and from a file.
  */
-public class FileHandling {
+public class FileHandling{
+    private static final String filePath = "src/main/resources/Economics/";
+    private static final String fileType = ".register";
+    private static final String date = "date=";
+    private static final String description = "description=";
+    private static final String amount = "amount=";
+    private static final String isReoccuring = "isReoccuring=";
+    private static final String category = "category=";
+
     /**
      * Method for writing (adding) an ItemRegister to a file.
      *
@@ -18,36 +26,42 @@ public class FileHandling {
      * @throws IOException if an input or output exception occurred.
      */
     public <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException {
-        try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/" + fileTitle + ".itemRegister"))) {
+        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) {
             bw.write(itemRegister.toString());
         } catch (IOException ex) {
             throw new IOException("Error writing story to file: " + ex.getMessage());
         }
     }
 
-    public ItemRegister<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException {
+    /**
+     * Method for reading (getting) an IncomeRegister from a file.
+     *
+     * @param fileTitle the name of the file you want to read from
+     * @return the IncomeRegister from the file.
+     * @throws IOException if an input or output exception occurred
+     */
+    public IncomeRegister readIncomeRegisterFromFile(String fileTitle) throws IOException {
         IncomeRegister incomeRegister = new IncomeRegister();
         String date = "";
         String description = "";
         double amount = 0;
         boolean reoccuring = false;
         IncomeCategory incomeCategory = null;
-        try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
-            br.readLine();
+        try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
             String line;
             String nextLine = br.readLine();
             while ((line = nextLine) != null) {
                 nextLine = br.readLine();
-                if(line.startsWith("date=")) {
-                    date = line.replace("date=", "");
-                } else if (line.startsWith("description=")) {
-                    description = line.replace("description=","");
-                } else if (line.startsWith("amount=")) {
-                    amount = Double.parseDouble(line.replace("amount=",""));
-                } else if (line.startsWith("isReoccuring=")) {
-                    reoccuring = line.replace("isReoccuring=","").equals("Reoccurring");
-                } else if (line.startsWith("category=")) {
-                    line = line.replace("category=","");
+                if(line.startsWith(FileHandling.date)) {
+                    date = line.replace(FileHandling.date, "");
+                } else if (line.startsWith(FileHandling.description)) {
+                    description = line.replace(FileHandling.description,"");
+                } else if (line.startsWith(FileHandling.amount)) {
+                    amount = Double.parseDouble(line.replace(FileHandling.amount,""));
+                } else if (line.startsWith(FileHandling.isReoccuring)) {
+                    reoccuring = line.replace(FileHandling.isReoccuring,"").equals("Reoccurring");
+                } else if (line.startsWith(FileHandling.category)) {
+                    line = line.replace(FileHandling.category,"");
                     incomeCategory = switch (line) {
                         case "GIFT" -> IncomeCategory.GIFT;
                         case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN;
@@ -67,29 +81,35 @@ public class FileHandling {
         return incomeRegister;
     }
 
-    public ItemRegister<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException {
+    /**
+     * Method for reading (getting) an ExpenseRegister from a file.
+     *
+     * @param fileTitle the name of the file you want to read from.
+     * @return the ExpenseRegister from the file.
+     * @throws IOException if an input or output exception occurred
+     */
+    public ExpenseRegister readExpenseRegisterFromFile(String fileTitle) throws IOException {
         ExpenseRegister expenseRegister = new ExpenseRegister();
         String date = "";
         String description = "";
         double amount = 0;
         boolean reoccuring = false;
         ExpenseCategory expenseCategory = null;
-        try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
-            br.readLine();
+        try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
             String line;
             String nextLine = br.readLine();
             while ((line = nextLine) != null) {
                 nextLine = br.readLine();
-                if (line.startsWith("date=")) {
-                    date = line.replace("date=", "");
-                } else if (line.startsWith("description=")) {
-                    description = line.replace("description=", "");
-                } else if (line.startsWith("amount=")) {
-                    amount = Double.parseDouble(line.replace("amount=", ""));
-                } else if (line.startsWith("isReoccuring=")) {
-                    reoccuring = line.replace("isReoccuring=", "").equals("Reoccurring");
-                } else if (line.startsWith("category=")) {
-                    line = line.replace("category=", "");
+                if (line.startsWith(FileHandling.date)) {
+                    date = line.replace(FileHandling.date, "");
+                } else if (line.startsWith(FileHandling.description)) {
+                    description = line.replace(FileHandling.description, "");
+                } else if (line.startsWith(FileHandling.amount)) {
+                    amount = Double.parseDouble(line.replace(FileHandling.amount, ""));
+                } else if (line.startsWith(FileHandling.isReoccuring)) {
+                    reoccuring = line.replace(FileHandling.isReoccuring, "").equals("Reoccurring");
+                } else if (line.startsWith(FileHandling.category)) {
+                    line = line.replace(FileHandling.category, "");
                     expenseCategory = switch (line) {
                         case "FOOD" -> ExpenseCategory.FOOD;
                         case "CLOTHES" -> ExpenseCategory.CLOTHES;
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
index 80b7ad88..d5b242c5 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
@@ -13,7 +13,7 @@ public abstract class ItemRegister<T extends Item>{
     List<T> items;
 
     /**
-     * Class constructor that creates an empty List for storing T=(Income or Expense).
+     * Class constructor that creates an empty List for storing itemĀ´s.
      */
     public ItemRegister() {
         this.items = new ArrayList<>();
@@ -29,19 +29,18 @@ public abstract class ItemRegister<T extends Item>{
     }
 
     /**
-     * Get a List of every T=(Income or Expense).
-     * @return T=(Income or Expense) List.
+     * Get a List of every item.
+     * @return item List.
      */
     public List<T> getItems() {
         return items;
     }
 
     /**
-     * Add a new T=(Income or Expense) to the register. Throws IllegalArgumentException
-     * if the new T=(Income or Expense) is already registered.
+     * Add a new item to the register.
      *
-     * @param newItem the T=(Income or Expense) you want to add.
-     * @throws IllegalArgumentException if the item is already in the register.
+     * @param newItem the item you want to add.
+     * @throws IllegalArgumentException if newItem is already in the register.
      */
     public void addItem(T newItem) throws IllegalArgumentException{
         if(items.contains(newItem)){
@@ -50,6 +49,18 @@ public abstract class ItemRegister<T extends Item>{
         items.add(newItem);
     }
 
+    /**
+     * Remove an item from the register.
+     *
+     * @param itemWantRemoved the item you want to remove.
+     * @throws IllegalArgumentException if the itemWantRemoved is not in the register.
+     */
+    public void removeItem(T itemWantRemoved) throws IllegalArgumentException{
+        if(!items.remove(itemWantRemoved)){
+            throw new IllegalArgumentException("The item is not in the register");
+        }
+    }
+
     /**
      * Check if items is empty.
      *
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java
index 1d6d108b..3f8c5564 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java
@@ -12,25 +12,51 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class ExpenseRegisterTest {
     ExpenseRegister expenseRegister = new ExpenseRegister();
-    @Test
-    @DisplayName("addItem method throws exception when it should")
-    void addItemThrows() {
-        Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
-        expenseRegister.addItem(expense);
-        assertThrows(IllegalArgumentException.class, () -> expenseRegister.addItem(expense));
+    @Nested
+    @DisplayName("Test addItem")
+    class testAddItem {
+        @Test
+        @DisplayName("addItem method throws exception when it should")
+        void addItemThrows() {
+            Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
+            expenseRegister.addItem(expense);
+            assertThrows(IllegalArgumentException.class, () -> expenseRegister.addItem(expense));
+        }
+
+        @Test
+        @DisplayName("addItem method does not throw exception when it should not")
+        void addItemDoesNotThrow() {
+            Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
+            Expense expense2 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
+            assertDoesNotThrow(() -> expenseRegister.addItem(expense1));
+            assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
+        }
     }
 
-    @Test
-    @DisplayName("addItem method does not throw exception when it should not")
-    void addItemDoesNotThrow() {
-        Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
-        Expense expense2 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
-        assertDoesNotThrow(() -> expenseRegister.addItem(expense1));
-        assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
+    @Nested
+    @DisplayName("Test removeItem")
+    class testRemoveItem{
+        @Test
+        @DisplayName("removeItem does throw exception when it should")
+        void removeItemDoesThrow(){
+            Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
+            Expense notExpense1 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
+            expenseRegister.addItem(expense1);
+            assertThrows(IllegalArgumentException.class, () -> expenseRegister.removeItem(notExpense1));
+        }
+        @Test
+        @DisplayName("removeItem method does not throw exception when it should not")
+        void removeItemDoesNotThrow(){
+            Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
+            Expense expense1Copy = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
+            expenseRegister.addItem(expense1);
+            assertDoesNotThrow(() -> expenseRegister.removeItem(expense1Copy));
+        }
+
     }
 
     @Nested
-    @DisplayName("Test getTotalSum and getExpenseByCategory methods")
+    @DisplayName("Test getTotalSum and getExpenseByCategory")
     class testGetExpenseByCategoryMethod {
         Expense expense1;
         Expense expense2;
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java
index 6c6289c7..aaa5097a 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java
@@ -2,7 +2,6 @@ package no.ntnu.idatt1002.demo.data.Economics;
 
 import org.junit.jupiter.api.*;
 
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import static org.junit.jupiter.api.Assertions.*;
 
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java
index 1c4c173d..64d18f51 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java
@@ -11,27 +11,52 @@ import static org.junit.jupiter.api.Assertions.*;
 
 public class IncomeRegisterTest {
     IncomeRegister incomeRegister = new IncomeRegister();
-    @Test
-    @DisplayName("addItem method throws exception when it should")
-    void addItemThrows() {
-        Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
-        incomeRegister.addItem(income);
-        assertThrows(IllegalArgumentException.class, () -> 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));
-        assertDoesNotThrow(() -> incomeRegister.addItem(income2));
+    @Nested
+    @DisplayName("Test addItem")
+    class testAddItem{
+        @Test
+        @DisplayName("addItem method throws exception when it should")
+        void addItemThrows() {
+            Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
+            incomeRegister.addItem(income);
+            assertThrows(IllegalArgumentException.class, () -> 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));
+            assertDoesNotThrow(() -> incomeRegister.addItem(income2));
+        }
     }
 
     @Nested
-    @DisplayName("test getTotalSum and getIncomeByCategory methods")
-    class testGetTotalSumAndGetIncomeByCategoryMethods {
+    @DisplayName("Test removeItem")
+    class testRemoveItem{
+        @Test
+        @DisplayName("removeItem does throw exception when it should")
+        void removeItemDoesThrow(){
+            Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
+            Income notIncome1 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
+            incomeRegister.addItem(income1);
+            assertThrows(IllegalArgumentException.class, () -> incomeRegister.removeItem(notIncome1));
+        }
+        @Test
+        @DisplayName("removeItem method does not throw exception when it should not")
+        void removeItemDoesNotThrow(){
+            Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
+            Income income1Copy = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
+            incomeRegister.addItem(income1);
+            assertDoesNotThrow(() -> incomeRegister.removeItem(income1Copy));
+        }
 
+    }
+    @Nested
+    @DisplayName("Test getTotalSum and getIncomeByCategory")
+    class testGetTotalSumAndGetIncomeByCategoryMethods {
         Income income1;
         Income income2;
         Income income3;
-- 
GitLab