From fb5e08a5e59f91c02cdf8e00edd66cb1b25bc2ef Mon Sep 17 00:00:00 2001 From: Andreas <andreksv@ntnu.no> Date: Sun, 19 Mar 2023 16:52:13 +0100 Subject: [PATCH] Reverted back to generics, T now extends items --- .../demo/data/Economics/ExpenseRegister.java | 57 -------- .../demo/data/Economics/IncomeRegister.java | 57 -------- .../demo/data/Economics/ItemRegister.java | 79 +++++++++-- .../data/Economics/ExpenseRegisterTest.java | 60 -------- .../data/Economics/IncomeRegisterTest.java | 63 --------- .../demo/data/Economics/ItemRegisterTest.java | 129 ++++++++++++++++++ 6 files changed, 195 insertions(+), 250 deletions(-) delete mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java delete mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java delete mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java delete mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java create 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 deleted file mode 100644 index 4d2b9b0b..00000000 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegister.java +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index 82f17d13..00000000 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegister.java +++ /dev/null @@ -1,57 +0,0 @@ -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 index d09bb6b3..78fbccf3 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 @@ -2,41 +2,94 @@ package no.ntnu.idatt1002.demo.data.Economics; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** - * Register class for storing Item-objects. + * ItemRegister is a generic class used + * for storing either Income or Expense. + * + * @param <T> Income or Expense */ -public abstract class ItemRegister{ - private final List<Item> items; +public class ItemRegister<T extends Item>{ + List<T> items; /** * An "empty" class constructor. */ - public ItemRegister(){ + public ItemRegister() { this.items = new ArrayList<>(); } /** - * Class constructor. - * @param items an ArrayList with Item´s you want to overview. + * Class constructor that takes in a List of T=(Income or Expense) as argument. + * + * @param items a List of T=(Income or Expense) you want to register. */ - public ItemRegister(ArrayList<Item> items){ + public ItemRegister(List<T> items) { this.items = items; } /** - * Check if the register has Item´s. - * @return true or false depending on if items is empty. + * Get a List of every T=(Income or Expense). + * @return T=(Income or Expense) List. */ - public boolean hasItems(){ - return items.isEmpty(); + public List<T> getItems() { + return items; } /** - * Get the sum of all Item´s in items - * @return sum of all Item´s + * 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) throws IllegalArgumentException{ + if(items.contains(newItem)){ + throw new IllegalArgumentException("This item is already registered"); + } + items.add(newItem); + } + + /** + * 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(){ return items.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum(); } + + /** + * Get a List of every T=(Income or Expense) + * in a given category. + * + * @param category the category you want to get every T=(Income or Expense) of. + * @param <S> IncomeCategory or ExpenseCategory + * @throws IllegalArgumentException if S is not IncomeCategory or ExpenseCategory + * @throws ClassCastException if you use an IncomeCategory for Expense register (and vice versa) + * @return a List of every T=(Income or Expense) in S=(IncomeCategory or ExpenseCategory) + */ + public <S> List<T> getItemsByCategory(S category) { + //Checks if category is an instance of IncomeCategory + if(category instanceof IncomeCategory) { + ArrayList<Income> castedIncome = (ArrayList<Income>) items; + try { + return (ArrayList<T>) castedIncome.stream().filter(anIncome -> anIncome.getCategory().compareTo((IncomeCategory) category) == 0).collect(Collectors.toList()); + } catch (ClassCastException ex) { + throw new ClassCastException("Can´t use IncomeCategory for Expense´s: " + ex.getMessage()); + } + } + //Checks category is s an instance of ExpenseCategory + else if(category instanceof ExpenseCategory){ + ArrayList<Expense> castedExpenses = (ArrayList<Expense>) items; + try { + return (ArrayList<T>) castedExpenses.stream().filter(anExpense -> anExpense.getCategory().compareTo((ExpenseCategory) category) == 0).collect(Collectors.toList()); + } catch (ClassCastException ex){ + throw new ClassCastException("Can´t use ExpenseCategory for Income: " + ex.getMessage()); + } + } else{ + throw new IllegalArgumentException("The category must be of type IncomeCategory or ExpenseCategory"); + } + } } 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 deleted file mode 100644 index 7bbfa7bd..00000000 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ExpenseRegisterTest.java +++ /dev/null @@ -1,60 +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.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 deleted file mode 100644 index 733902ac..00000000 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/IncomeRegisterTest.java +++ /dev/null @@ -1,63 +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 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 new file mode 100644 index 00000000..8752fbef --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java @@ -0,0 +1,129 @@ +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)); + } + } +} -- GitLab