diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java b/src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java index ef03d1faff06e3714b585c31bb88a676007bb21d..2c4f15510a079a96c0128cf8724ce0ac89eacf6d 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java @@ -39,6 +39,9 @@ public class ItemOverview { * @param newIncome The Income you want to add. */ public void addIncome(Income newIncome){ + if(income.contains(newIncome)){ + throw new IllegalArgumentException("This income is already registered"); + } income.add(newIncome); } @@ -47,6 +50,9 @@ public class ItemOverview { * @param newExpense The Expense you want to add. */ public void addExpense(Expense newExpense){ + if(expense.contains(newExpense)){ + throw new IllegalArgumentException("This expense is already registered"); + } expense.add(newExpense); } diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java new file mode 100644 index 0000000000000000000000000000000000000000..61861aaf837ad25de1f0e55b07c3594082b6ea2e --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java @@ -0,0 +1,51 @@ +package no.ntnu.idatt1002.demo.data; + +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.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ItemOverviewTest { + ItemOverview itemOverview; + @BeforeEach + public void makeItemOverview(){ + itemOverview = new ItemOverview(); + } + @Test + @DisplayName("addIncome method throws exception when it should") + void addIncomeThrows(){ + itemOverview.addIncome(new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); + itemOverview.addIncome(new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); + } + + @Test + @DisplayName("addExpense method throws exception when it should") + void addExpenseThrows(){ + itemOverview.addExpense(new Expense("description", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23")); + itemOverview.addExpense(new Expense("description", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23")); + } + + @Test + @DisplayName("getTotalIncome method gives correct amount") + void getTotalIncomeCorrectAmount(){ + itemOverview.addIncome(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); + itemOverview.addIncome(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21")); + itemOverview.addIncome(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23")); + + double totalIncome = 59.9f + 62.4f + 9.81f; + assertEquals(Math.round(itemOverview.getTotalIncome()), Math.round(totalIncome)); + } + + @Test + @DisplayName("getTotalExpense method gives correct amount") + void getTotalExpenseCorrectAmount(){ + itemOverview.addExpense(new Expense("description1", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23")); + itemOverview.addExpense(new Expense("description2", 78.2f, true, ExpenseCategory.FOOD, "03.04.23")); + itemOverview.addExpense(new Expense("description3", 103.5f, true, ExpenseCategory.OTHER, "07.03.23")); + + double totalExpense = 59.9f + 78.2f + 103.5f; + assertEquals(Math.round(itemOverview.getTotalExpense()), Math.round(totalExpense)); + } +}