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 74fe9d893306bba98cb37d02d9e1d411f2a86cfc..5066f6b0d128352d9ae918b454feee68e73141d2 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 @@ -58,4 +58,14 @@ public class Expense extends Item{ } this.category = expenseCategory; } + + /** + * Returns a String that represents the Expense. + * Also includes the ExpenseCategory + * @return The Expense as a String + */ + @Override + public String toString() { + return super.toString()+"\ncategory="+category.toString()+"\n"; + } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java new file mode 100644 index 0000000000000000000000000000000000000000..1a279301d3d658581181a8b2b0bbcea2edbe56ee --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java @@ -0,0 +1,38 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import java.util.List; + +/** + * ExpenseRegister is a class for + * storing Expenses. Subclass of + * ItemRegister. + */ +public class ExpenseRegister extends ItemRegister<Expense> { + + /** + * Class constructor that creates an empty List for storing Expense. + */ + public ExpenseRegister() { + super(); + } + + /** + * Class constructor that takes in a List of Expense as argument. + * + * @param expenses the List of Expense you want to register. + */ + public ExpenseRegister(List<Expense> expenses){ + super(expenses); + } + + /** + * Method for getting every Expense + * in a given ExpenseCategory. + * + * @param category the ExpenseCategory you want to get every Expense of. + * @return a List of every Expense with category. + */ + public List<Expense> getExpenseByCategory(ExpenseCategory category){ + return this.items.stream().filter(expense -> expense.getCategory().compareTo( category) == 0).toList(); + } +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..390906b5fd4d7a6b36fbde586f394f173b766c7a --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java @@ -0,0 +1,112 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.io.*; + + +/** + * FileHandling is a class for writing and reading + * Item-objects to and from a file. + */ +public class FileHandling { + /** + * Method for writing (adding) an ItemRegister to a file. + * + * @param itemRegister the ItemRegister you want to write to a file. + * @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"))) { + 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 { + 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(); + 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=",""); + incomeCategory = switch (line) { + case "GIFT" -> IncomeCategory.GIFT; + case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN; + default -> IncomeCategory.SALARY; + }; + } + if(line.isEmpty() || (nextLine == null)) { + if(description.equals("")){ + incomeRegister.addItem(new Income(amount, reoccuring, incomeCategory, date)); + } else{ + incomeRegister.addItem(new Income(description, amount, reoccuring, incomeCategory, date)); + } + description = ""; + } + } + } + return incomeRegister; + } + + public ItemRegister<Expense> 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(); + 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=", ""); + expenseCategory = switch (line) { + case "FOOD" -> ExpenseCategory.FOOD; + case "CLOTHES" -> ExpenseCategory.CLOTHES; + case "BOOKS" -> ExpenseCategory.BOOKS; + default -> ExpenseCategory.OTHER; + }; + } + if (line.isEmpty() || (nextLine == null)) { + if (description.equals("")) { + expenseRegister.addItem(new Expense(amount, reoccuring, expenseCategory, date)); + } else { + expenseRegister.addItem(new Expense(description, amount, reoccuring, expenseCategory, date)); + } + description = ""; + } + } + } + return expenseRegister; + } +} \ No newline at end of file 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 646b1d82d10449eec9656abfcfab03e5ad7b9ff2..480636b05c985767e18d7b42c9ae970ed9f05db7 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 @@ -66,6 +66,13 @@ public class Income extends Item{ this.category = category; } - - + /** + * Returns a String that represents the Income. + * Also includes the IncomeCategory + * @return The Income as a String + */ + @Override + public String toString() { + return super.toString()+"\ncategory="+category.toString()+"\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 new file mode 100644 index 0000000000000000000000000000000000000000..fa1dfbc8217e2be81e961cf6d02b1c7fa378cd17 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java @@ -0,0 +1,36 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import java.util.List; + +/** + * IncomeRegister is a class for + * storing Income. Subclass of + * ItemRegister. + */ +public class IncomeRegister extends ItemRegister<Income>{ + /** + * Class constructor that creates an empty List for storing Income. + */ + public IncomeRegister(){ + super(); + } + + /** + * Class constructor that takes in a List of Income as argument. + * + * @param income the List of Income you want to register. + */ + public IncomeRegister(List<Income> income){ + super(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. + */ + public List<Income> getIncomeByCategory(IncomeCategory category){ + return items.stream().filter(income -> income.getCategory().compareTo(category) == 0).toList(); + } +} 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 818d21f388da4945ada87728d4883605464bf7e3..04b6bc51b37f2b6014237b1ad467d6c7ded9c9d6 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 @@ -124,6 +124,27 @@ public abstract class Item { return Objects.hash(description, amount, recurring, date); } + /** + * Returns a String that represents the Item + * @return The Item as a String + */ + @Override + public String toString() { + String isReoccuring = ""; + if(recurring){ + isReoccuring = "Reoccurring"; + } + else{ + isReoccuring = "Not reoccurring"; + } + if(!description.isBlank()){ + return "\ndate=" + date+"\ndescription=" + description+"\namount="+amount+"\nisReoccuring="+isReoccuring; + } + else{ + return "\ndate="+date+"\namount="+amount+"\nisReoccuring="+isReoccuring; + } + } + /* @Override public boolean equals(Object obj) { if (this == obj) { 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 8a8b35fcee5af8677efa29009360159880c77f18..80b7ad88ada1ee56a6c865c6964edcc1cd404044 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 @@ -1,47 +1,49 @@ package no.ntnu.idatt1002.demo.data.Economics; import java.util.ArrayList; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; +import java.util.List; /** - * ItemRegister is a generic class used by ExpenseRegister and - * IncomeRegister. - * @param <T> Income or Expense + * ItemRegister is a generic class used + * for storing either Income or Expense. + * + * @param <T> Income or Expense */ -public class ItemRegister<T>{ - ArrayList<T> items; +public abstract class ItemRegister<T extends Item>{ + List<T> items; /** - * An "empty" class constructor. + * Class constructor that creates an empty List for storing T=(Income or Expense). */ public ItemRegister() { - this.items = new ArrayList<>(); //ArrayList for storing T´s + this.items = new ArrayList<>(); } /** - * Class constructor that takes in an ArrayList of T as argument. - * @param items An ArrayList of Income or Expense you want to overview. + * Class constructor that takes in a List of T=(Income or Expense) as argument. + * + * @param items the List of T=(Income or Expense) you want to register. */ - public ItemRegister(ArrayList<T> items) { + public ItemRegister(List<T> items) { this.items = items; } /** - * Get an ArrayList of every T. - * @return T ArrayList. + * Get a List of every T=(Income or Expense). + * @return T=(Income or Expense) List. */ - public ArrayList<T> getItems() { + public List<T> getItems() { return items; } /** - * Add a new T to the register. Throws IllegalArgumentException - * if the new T is already registered. - * @param newItem The T you want to add. + * Add a new T=(Income or Expense) to the register. Throws IllegalArgumentException + * if the new T=(Income or Expense) is already registered. + * + * @param newItem the T=(Income or Expense) you want to add. + * @throws IllegalArgumentException if the item is already in the register. */ - public void addItem(T newItem){ + public void addItem(T newItem) throws IllegalArgumentException{ if(items.contains(newItem)){ throw new IllegalArgumentException("This item is already registered"); } @@ -49,21 +51,34 @@ public class ItemRegister<T>{ } /** - * Get the sum of all T´s in items. - * @return Sum of all T´s. + * Check if items is empty. + * + * @return true or false depending on if items is empty. + */ + public boolean isEmpty(){ + return this.items.isEmpty(); + } + + /** + * Get the sum of all T´s=(Income or Expenses) in items. + * + * @return sum of all the T´s=(Income or Expenses). */ public double getTotalSum(){ - ArrayList<Item> castedItems = (ArrayList<Item>) items; //Casts items as an ArrayList of Item´s - return castedItems.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum(); + return items.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum(); } - public <S> ArrayList<T> getItemsBasedOnCategory(S Category){ - if(Category instanceof IncomeCategory){ //Checks if it´s an instance of IncomeCategory - ArrayList<Income> castedIncome = (ArrayList<Income>) items; - return (ArrayList<T>) castedIncome.stream().filter(anIncome -> anIncome.getCategory().compareTo((IncomeCategory) Category) == 0).collect(Collectors.toList()); - } else if(Category instanceof ExpenseCategory){ //Checks if it´s an instance of ExpenseCategory - ArrayList<Expense> castedExpenses = (ArrayList<Expense>) items; - return (ArrayList<T>) castedExpenses.stream().filter(anExpense -> anExpense.getCategory().compareTo((ExpenseCategory) Category) == 0).collect(Collectors.toList()); - } else{return null;} + @Override + public String toString() { + StringBuilder stringItems = new StringBuilder(); + for(Item item : items) { + stringItems.append(item.toString()); + } + if(stringItems.isEmpty()) { + return "The register is Empty"; + } + else { + return stringItems.toString(); + } } } diff --git a/src/main/resources/expenseRegisterTest.itemRegister b/src/main/resources/expenseRegisterTest.itemRegister new file mode 100644 index 0000000000000000000000000000000000000000..7f98526b7b5d9fa27c8d5ec080a8b33f9fb0b143 --- /dev/null +++ b/src/main/resources/expenseRegisterTest.itemRegister @@ -0,0 +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/incomeRegisterTest.itemRegister new file mode 100644 index 0000000000000000000000000000000000000000..6bfe0943a9c759b6838d594b03262e3727212ce9 --- /dev/null +++ b/src/main/resources/incomeRegisterTest.itemRegister @@ -0,0 +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 new file mode 100644 index 0000000000000000000000000000000000000000..1d6d108bdfeb2c53567332800b45e689166f2925 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java @@ -0,0 +1,64 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +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)); + } + + @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 getTotalSum and getExpenseByCategory methods") + class testGetExpenseByCategoryMethod { + Expense expense1; + Expense expense2; + Expense expense3; + + @BeforeEach + void addExpensesToRegister() { + expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21"); + expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23"); + + expenseRegister.addItem(expense1); + expenseRegister.addItem(expense2); + expenseRegister.addItem(expense3); + } + + @Test + @DisplayName("getTotalSum method gives correct amount") + void getTotalSumCorrectAmount() { + double totalIncome = 59.9f + 62.4f + 9.81f; + assertEquals(Math.round(expenseRegister.getTotalSum()), Math.round(totalIncome)); + } + + @Test + @DisplayName("getExpenseByCategory gives expected Expenses back") + void getExpensesByCategoryGivesExpectedExpensesBack() { + List<Expense> expenseSalary = expenseRegister.getExpenseByCategory(ExpenseCategory.CLOTHES); + assertTrue(expenseSalary.contains(expense1) && expenseSalary.contains(expense3)); + } + } +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..6c6289c7253593061aa7b0feec2c0295da62255e --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java @@ -0,0 +1,152 @@ +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.*; + +class FileHandlingTest { + FileHandling fileHandling = new FileHandling(); + ExpenseRegister expenseRegister = new ExpenseRegister(); + IncomeRegister incomeRegister = new IncomeRegister(); + @Nested + @DisplayName("FileHandling IncomeRegister to file") + class fileHandlingIncomeRegisterToFile{ + String fileTitle = "incomeRegisterTest"; + @Nested + @DisplayName("FileHandling incomeRegister with income that has description") + class fileHandlingIncomeRegisterWithIncomeThatHasDescription{ + @BeforeEach + void addIncomeToIncomeRegister(){ + Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23"); + incomeRegister.addItem(income1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct incomeRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling incomeRegister with income that does not have description") + class fileHandlingIncomeRegisterWithIncomeThatDoesNotHaveDescription{ + @BeforeEach + void addIncomeToIncomeRegister(){ + Income income1 = new Income(59.9f, false, IncomeCategory.GIFT, "03.03.23"); + incomeRegister.addItem(income1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct incomeRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling incomeRegister with multiple Income") + class fileHandlingIncomeRegisterWithMultipleIncome{ + @BeforeEach + void addIncomeToIncomeRegister(){ + Income income1 = new Income(59.9f, false, IncomeCategory.GIFT, "03.03.23"); + Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); + Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); + incomeRegister.addItem(income1); + incomeRegister.addItem(income2); + incomeRegister.addItem(income3); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct incomeRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString()); + } + } + } + @Nested + @DisplayName("FileHandling ExpenseRegister to file") + class fileHandlingExpenseRegisterToFile{ + String fileTitle = "expenseRegisterTest"; + @Nested + @DisplayName("FileHandling ExpenseRegister with Expense that has description") + class fileHandlingExpenseRegisterWithExpenseThatHasDescription{ + @BeforeEach + void addExpenseToExpenseRegister(){ + Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + expenseRegister.addItem(expense1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct expenseRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling ExpenseRegister with Expense that does not have description") + class fileHandlingExpenseRegisterWithExpenseThatDoesNotHaveDescription{ + @BeforeEach + void addExpenseToExpenseRegister(){ + Expense expense1 = new Expense(59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + expenseRegister.addItem(expense1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct expenseRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling ExpenseRegister with multiple Expense") + class fileHandlingExpenseRegisterWithMultipleExpensen{ + @BeforeEach + void addExpenseToExpenseRegister(){ + Expense expense1 = new Expense(59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + Expense expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21"); + Expense expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23"); + + expenseRegister.addItem(expense1); + expenseRegister.addItem(expense2); + expenseRegister.addItem(expense3); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct expenseRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString()); + } + } + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..1c4c173d8ff39ac3cab11d021c24a861404ee8a8 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java @@ -0,0 +1,64 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.List; + +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 getTotalSum and getIncomeByCategory methods") + class testGetTotalSumAndGetIncomeByCategoryMethods { + + Income income1; + Income income2; + Income income3; + + @BeforeEach + void addIncomeToRegister() { + income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); + income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); + income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); + incomeRegister.addItem(income1); + incomeRegister.addItem(income2); + incomeRegister.addItem(income3); + } + + @Test + @DisplayName("getTotalSum method gives correct amount") + void getTotalSumCorrectAmount() { + double totalIncome = 59.9f + 62.4f + 9.81f; + assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome)); + } + + @Test + @DisplayName("getItemsByCategory gives expected Income back") + void getIncomeByCategoryGivesCorrectIncome() { + List<Income> incomeSalary = incomeRegister.getIncomeByCategory(IncomeCategory.SALARY); + assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3)); + } + } +} + 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 deleted file mode 100644 index 5894f13141a103625e4c083e1f5b174b824fbbbd..0000000000000000000000000000000000000000 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java +++ /dev/null @@ -1,130 +0,0 @@ -package no.ntnu.idatt1002.demo.data.Economics; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; - -import static org.junit.jupiter.api.Assertions.*; - -public class ItemRegisterTest { - ItemRegister<Income> incomeRegister; - ItemRegister<Expense> expenseRegister; - - @Nested - @DisplayName("Test ItemRegister when using Income") - class incomeRegisterTests { - @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() { - Income income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); - Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); - Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); - incomeRegister.addItem(income1); - incomeRegister.addItem(income2); - incomeRegister.addItem(income3); - - double totalIncome = 59.9f + 62.4f + 9.81f; - assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome)); - } - - @Test - void getIncomeBasedOnCategoryGivesCorrectIncome() { - Income income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); - Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); - Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); - - incomeRegister.addItem(income1); - incomeRegister.addItem(income2); - incomeRegister.addItem(income3); - ArrayList<Income> incomeSalary = incomeRegister.getItemsBasedOnCategory(IncomeCategory.SALARY); - assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3)); - } - } - - @Nested - @DisplayName("Test ItemRegister when using Expense") - class expenseRegisterTests { - @BeforeEach - public void createItemOverView() { - expenseRegister = new ItemRegister(); - } - - @Test - @DisplayName("addItem method throws exception when it should") - void addItemThrows() { - Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); - assertThrows(IllegalArgumentException.class, () -> { - expenseRegister.addItem(expense); - 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); - expenseRegister.addItem(expense2); - }); - } - - @Test - @DisplayName("getTotalSum method gives correct amount") - void getTotalSumCorrectAmount(){ - Expense expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); - Expense expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21"); - Expense expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23"); - - expenseRegister.addItem(expense1); - expenseRegister.addItem(expense2); - expenseRegister.addItem(expense3); - double totalIncome = 59.9f + 62.4f + 9.81f; - assertEquals(Math.round(expenseRegister.getTotalSum()), Math.round(totalIncome)); - } - - @Test - void getIncomeBasedOnCategoryGivesCorrectIncome() { - Expense expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); - Expense expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21"); - Expense expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23"); - - expenseRegister.addItem(expense1); - expenseRegister.addItem(expense2); - expenseRegister.addItem(expense3); - ArrayList<Expense> expenseSalary = expenseRegister.getItemsBasedOnCategory(ExpenseCategory.CLOTHES); - assertTrue(expenseSalary.contains(expense1) && expenseSalary.contains(expense3)); - } - } -} - \ No newline at end of file