From 14f7314f3c74f2dee1511c45507a98fb04aed6b0 Mon Sep 17 00:00:00 2001 From: Andreas <andreksv@ntnu.no> Date: Sat, 18 Mar 2023 09:49:36 +0100 Subject: [PATCH] Removed generic ItemRegister, now use inheritance for register classes --- .../demo/data/Economics/ExpenseRegister.java | 57 ++++++++ .../demo/data/Economics/IncomeRegister.java | 57 ++++++++ .../demo/data/Economics/ItemRegister.java | 69 ---------- .../data/Economics/ExpenseRegisterTest.java | 60 ++++++++ .../data/Economics/IncomeRegisterTest.java | 63 +++++++++ .../demo/data/Economics/ItemRegisterTest.java | 130 ------------------ 6 files changed, 237 insertions(+), 199 deletions(-) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java delete mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java create mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java create mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java delete mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java 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 00000000..4d2b9b0b --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java @@ -0,0 +1,57 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Register class for Expense-objects. + */ +public class ExpenseRegister extends ItemRegister { + private final List<Expense> expenses; + + /** + * An "empty" class constructor. + */ + public ExpenseRegister(){ + this.expenses = new ArrayList<>(); + } + + /** + * Class constructor. + * @param expenses an ArrayList with Expense´s you want to overview + */ + public ExpenseRegister(ArrayList<Expense> expenses){ + this.expenses = expenses; + } + + /** + * Get a List of every Expense-object in expenses. + * @return expenses. + */ + public List<Expense> getExpenses() { + return expenses; + } + + /** + * Add an Expense to expenses. + * @param expense the Expense you want to add. + * @throws IllegalArgumentException if expense is already registered. + */ + public void addExpense(Expense expense) throws IllegalArgumentException{ + if(expenses.contains(expense)){ + throw new IllegalArgumentException("This expense is already in the register"); + } + this.expenses.add(expense); + } + + /** + * Get every Expense of a given ExpenseCategory. + * @param category the ExpenseCategory you want to find every Expense of. + * @return a List with every Expense in expenses with the category. + */ + public List<Expense> getExpenseBasedOnCategory(ExpenseCategory category){ + return expenses.stream().filter(anExpense -> anExpense.getCategory().compareTo(category) == 0).collect(Collectors.toList()); + } + +} 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 00000000..82f17d13 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java @@ -0,0 +1,57 @@ +package no.ntnu.idatt1002.demo.data.Economics; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Register class for Income-objects. + */ +public class IncomeRegister extends ItemRegister{ + private final List<Income> incomes; + + /** + * An "empty" class constructor. + */ + public IncomeRegister(){ + this.incomes = new ArrayList<>(); + } + + /** + * Class constructor. + * @param incomes an ArrayList with Income´s you want to overview + */ + public IncomeRegister(ArrayList<Income> incomes){ + this.incomes = incomes; + } + + /** + * Get a List of every Income-object in incomes. + * @return incomes. + */ + public Collection<Income> getExpenses() { + return incomes; + } + + /** + * Add an Income to incomes. + * @param income the Income you want to add. + * @throws IllegalArgumentException if income is already registered. + */ + public void addIncome(Income income){ + if(incomes.contains(income)){ + throw new IllegalArgumentException("This Income is already in the register."); + } + this.incomes.add(income); + } + + /** + * Get every Income of a given IncomeCategory. + * @param category the IncomeCategory you want to find every Income of. + * @return a List with every Income in incomes with the category. + */ + public List<Income> getIncomeBasedOnCategory(IncomeCategory category){ + return incomes.stream().filter(anIncome -> anIncome.getCategory().compareTo(category) == 0).collect(Collectors.toList()); + } + +} \ No newline at end of file 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 deleted file mode 100644 index 8a8b35fc..00000000 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java +++ /dev/null @@ -1,69 +0,0 @@ -package no.ntnu.idatt1002.demo.data.Economics; - -import java.util.ArrayList; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * ItemRegister is a generic class used by ExpenseRegister and - * IncomeRegister. - * @param <T> Income or Expense - */ -public class ItemRegister<T>{ - ArrayList<T> items; - - /** - * An "empty" class constructor. - */ - public ItemRegister() { - this.items = new ArrayList<>(); //ArrayList for storing T´s - } - - /** - * Class constructor that takes in an ArrayList of T as argument. - * @param items An ArrayList of Income or Expense you want to overview. - */ - public ItemRegister(ArrayList<T> items) { - this.items = items; - } - - /** - * Get an ArrayList of every T. - * @return T ArrayList. - */ - public ArrayList<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. - */ - public void addItem(T newItem){ - if(items.contains(newItem)){ - throw new IllegalArgumentException("This item is already registered"); - } - items.add(newItem); - } - - /** - * Get the sum of all T´s in items. - * @return Sum of all T´s. - */ - 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(); - } - - 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;} - } -} 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 00000000..7bbfa7bd --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java @@ -0,0 +1,60 @@ +package no.ntnu.idatt1002.demo.data.Economics; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class ExpenseRegisterTest { + ExpenseRegister expenseRegister; + @BeforeEach + public void createItemOverView() { + expenseRegister = new ExpenseRegister(); + } + @Test + @DisplayName("addExpense method throws exception when it should") + void addItemThrows() { + Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + expenseRegister.addExpense(expense); + assertThrows(IllegalArgumentException.class, () -> { + expenseRegister.addExpense(expense); + }); + } + @Test + @DisplayName("addExpense 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.addExpense(expense1)); + assertDoesNotThrow(() -> expenseRegister.addExpense(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.addExpense(expense1); + expenseRegister.addExpense(expense2); + expenseRegister.addExpense(expense3); + double totalIncome = 59.9f + 62.4f + 9.81f; + assertEquals(Math.round(expenseRegister.getTotalSum()), Math.round(totalIncome)); + } + + @Test + @DisplayName("getExpenseBasedOnCategory give correct Expense`s back") + void getExpenseBasedOnCategoryGivesCorrectExpense() { + 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.addExpense(expense1); + expenseRegister.addExpense(expense2); + expenseRegister.addExpense(expense3); + List<Expense> expenseSalary = expenseRegister.getExpenseBasedOnCategory(ExpenseCategory.CLOTHES); + assertTrue(expenseSalary.contains(expense1) && expenseSalary.contains(expense3)); + } +} 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 00000000..733902ac --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java @@ -0,0 +1,63 @@ +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 java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class IncomeRegisterTest { + IncomeRegister incomeRegister; + @BeforeEach + public void createItemOverView() { + incomeRegister = new IncomeRegister(); + } + @Test + @DisplayName("addIncome method throws exception when it should") + void addItemThrows() { + Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); + incomeRegister.addIncome(income); + assertThrows(IllegalArgumentException.class, () -> { + incomeRegister.addIncome(income); + }); + } + + @Test + @DisplayName("addIncome 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.addIncome(income1)); + assertDoesNotThrow(() -> incomeRegister.addIncome(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.addIncome(income1); + incomeRegister.addIncome(income2); + incomeRegister.addIncome(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.addIncome(income1); + incomeRegister.addIncome(income2); + incomeRegister.addIncome(income3); + List<Income> incomeSalary = incomeRegister.getIncomeBasedOnCategory(IncomeCategory.SALARY); + assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3)); + } +} + \ No newline at end of file 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 5894f131..00000000 --- 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 -- GitLab