diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Expense.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Expense.java index 5066f6b0d128352d9ae918b454feee68e73141d2..078ec99f4e9bfdef73c76b8396026dac0cbd2a7e 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Expense.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Expense.java @@ -66,6 +66,6 @@ public class Expense extends Item{ */ @Override public String toString() { - return super.toString()+"\ncategory="+category.toString()+"\n"; + return super.toString()+"category="+category.toString()+"\n\n"; } } 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 390906b5fd4d7a6b36fbde586f394f173b766c7a..bfbe5ab7098c96ebeef9c33e614e14a8658f16b5 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/Income.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Income.java index 480636b05c985767e18d7b42c9ae970ed9f05db7..c07821dc0e79b6ec8ae934cb8ca4a1fadda78ea5 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Income.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/Income.java @@ -73,6 +73,6 @@ public class Income extends Item{ */ @Override public String toString() { - return super.toString()+"\ncategory="+category.toString()+"\n"; + return super.toString()+"category="+category.toString()+"\n\n"; } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java index fa1dfbc8217e2be81e961cf6d02b1c7fa378cd17..1a9c4a126700254f2e7cf60be624edcbf337e04b 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java @@ -27,6 +27,7 @@ public class IncomeRegister extends ItemRegister<Income>{ /** * Method for getting every Income * in a given IncomeCategory. + * * @param category the IncomeCategory you want to get every Income of. * @return a List of every Income with category. */ 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 04b6bc51b37f2b6014237b1ad467d6c7ded9c9d6..7bd8b00ee062e35ee5a51c94f359c168ef7d95ad 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 @@ -138,10 +138,10 @@ public abstract class Item { isReoccuring = "Not reoccurring"; } if(!description.isBlank()){ - return "\ndate=" + date+"\ndescription=" + description+"\namount="+amount+"\nisReoccuring="+isReoccuring; + return "date=" + date+"\ndescription=" + description+"\namount="+amount+"\nisReoccuring="+isReoccuring+"\n"; } else{ - return "\ndate="+date+"\namount="+amount+"\nisReoccuring="+isReoccuring; + return "date="+date+"\namount="+amount+"\nisReoccuring="+isReoccuring+"\n"; } } 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 80b7ad88ada1ee56a6c865c6964edcc1cd404044..dbe2cc95dd2ea265989e2981c4a159215a6844d7 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,19 @@ 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 +50,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/main/resources/expenseRegisterTest.itemRegister b/src/main/resources/Economics/expenseRegisterTest.register similarity index 100% rename from src/main/resources/expenseRegisterTest.itemRegister rename to src/main/resources/Economics/expenseRegisterTest.register index 7f98526b7b5d9fa27c8d5ec080a8b33f9fb0b143..c7d084112c6de671e412d5e185da4f5aee5a0327 100644 --- a/src/main/resources/expenseRegisterTest.itemRegister +++ b/src/main/resources/Economics/expenseRegisterTest.register @@ -1,6 +1,6 @@ - date=03.03.23 description=description amount=59.900001525878906 isReoccuring=Not reoccurring category=CLOTHES + diff --git a/src/main/resources/incomeRegisterTest.itemRegister b/src/main/resources/Economics/incomeRegisterTest.register similarity index 100% rename from src/main/resources/incomeRegisterTest.itemRegister rename to src/main/resources/Economics/incomeRegisterTest.register index 6bfe0943a9c759b6838d594b03262e3727212ce9..0b3082def4aa9d25e83b22a1dbc298bcb85459ba 100644 --- a/src/main/resources/incomeRegisterTest.itemRegister +++ b/src/main/resources/Economics/incomeRegisterTest.register @@ -1,6 +1,6 @@ - date=03.03.23 description=description amount=59.900001525878906 isReoccuring=Not reoccurring category=GIFT + 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 1d6d108bdfeb2c53567332800b45e689166f2925..3f8c5564c7cf45960e2381aa2c909ab41272bd14 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/ExpenseTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseTest.java index 338f63fb563e843c1871674e6043528cc89b9894..191c8513b06596eac7682e78dbb2ad21ee95f56c 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseTest.java @@ -1,7 +1,5 @@ package no.ntnu.idatt1002.demo.data.Economics; -import no.ntnu.idatt1002.demo.data.Economics.Expense; -import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -14,7 +12,7 @@ class ExpenseTest { void constructorThrows(){ 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.") 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 6c6289c7253593061aa7b0feec2c0295da62255e..59ffe36701caa1eee0832678c4b41a3a16b43b8f 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.*; @@ -24,7 +23,7 @@ class FileHandlingTest { } @Test @DisplayName("Writing to file does not throw exception") - void writingToFileDoesNotThrowException() throws IOException { + void writingToFileDoesNotThrowException() { assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); } @@ -44,7 +43,7 @@ class FileHandlingTest { } @Test @DisplayName("Writing to file does not throw exception") - void writingToFileDoesNotThrowException() throws IOException { + void writingToFileDoesNotThrowException() { assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); } @@ -68,7 +67,7 @@ class FileHandlingTest { } @Test @DisplayName("Writing to file does not throw exception") - void writingToFileDoesNotThrowException() throws IOException { + void writingToFileDoesNotThrowException() { assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); } @@ -93,7 +92,7 @@ class FileHandlingTest { } @Test @DisplayName("Writing to file does not throw exception") - void writingToFileDoesNotThrowException() throws IOException { + void writingToFileDoesNotThrowException() { assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); } @@ -113,7 +112,7 @@ class FileHandlingTest { } @Test @DisplayName("Writing to file does not throw exception") - void writingToFileDoesNotThrowException() throws IOException { + void writingToFileDoesNotThrowException() { assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); } @@ -138,7 +137,7 @@ class FileHandlingTest { } @Test @DisplayName("Writing to file does not throw exception") - void writingToFileDoesNotThrowException() throws IOException { + void writingToFileDoesNotThrowException() { assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); } 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 1c4c173d8ff39ac3cab11d021c24a861404ee8a8..64d18f519505a3b7c6f4f99c9d7bca187088741b 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;