From 1407bbf5bd90d61cc917fb4017f2674f2887900b Mon Sep 17 00:00:00 2001 From: Andreas <andreksv@ntnu.no> Date: Tue, 14 Mar 2023 15:09:20 +0100 Subject: [PATCH] Remade ItemOverview with Generics, and fixed tests --- .../demo/data/Economics/ItemRegister.java | 47 ++++-- .../demo/data/Economics/ItemRegisterTest.java | 150 +++++++++++++----- 2 files changed, 143 insertions(+), 54 deletions(-) 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 7d72314c..8a8b35fc 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,37 +1,46 @@ package no.ntnu.idatt1002.demo.data.Economics; import java.util.ArrayList; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; /** - * ItemOverview is an abstract class for storing and getting. - * information on items. Superclass for Income- and ExpenseOverview. + * ItemRegister is a generic class used by ExpenseRegister and + * IncomeRegister. + * @param <T> Income or Expense */ -public class ItemOverview<T>{ +public class ItemRegister<T>{ ArrayList<T> items; /** * An "empty" class constructor. */ - public ItemOverview() { - this.items = new ArrayList<>(); //ArrayList for storing item´s + public ItemRegister() { + this.items = new ArrayList<>(); //ArrayList for storing T´s } /** - * Class constructor that takes in an ArrayList of Item´s as argument. - * @param items An ArrayList of the Item´s you want to overview. + * Class constructor that takes in an ArrayList of T as argument. + * @param items An ArrayList of Income or Expense you want to overview. */ - public ItemOverview(ArrayList<T> items) { + public ItemRegister(ArrayList<T> items) { this.items = items; } /** - * Get an ArrayList of every item. - * @return item ArrayList. + * 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"); @@ -39,14 +48,22 @@ public class ItemOverview<T>{ items.add(newItem); } - - /** - * Get the sum of all Item´s in items. - * @return Sum of all Item´s. + * Get the sum of all T´s in items. + * @return Sum of all T´s. */ public double getTotalSum(){ - ArrayList<Item> castedItems = (ArrayList<Item>) items; + 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/ItemRegisterTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java index de3ceaf6..5894f131 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java @@ -2,57 +2,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 IncomeOverviewTest { +public class ItemRegisterTest { ItemRegister<Income> incomeRegister; + ItemRegister<Expense> expenseRegister; - @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);}); - } + @Nested + @DisplayName("Test ItemRegister when using Income") + class incomeRegisterTests { + @BeforeEach + public void createItemOverView() { + incomeRegister = new ItemRegister(); + } - @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("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"); - @Test - @DisplayName("getTotalSum method gives correct amount") - void getTotalSumCorrectAmount(){ - incomeRegister.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); - incomeRegister.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21")); - incomeRegister.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23")); - double totalIncome = 59.9f + 62.4f + 9.81f; - System.out.println(incomeRegister.getTotalSum()); - assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome)); + incomeRegister.addItem(income1); + incomeRegister.addItem(income2); + incomeRegister.addItem(income3); + ArrayList<Income> incomeSalary = incomeRegister.getItemsBasedOnCategory(IncomeCategory.SALARY); + assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3)); + } } - @Test - void getIncomeBasedOnCategoryGivesCorrectIncome(){ - incomeRegister.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); - incomeRegister.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21")); - incomeRegister.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23")); - ArrayList<Income> incomeCategory = new ArrayList<>(); - incomeCategory.add(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); - incomeCategory.add(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23")); - assertEquals(incomeRegister.getItemsBasedOnCategory(IncomeCategory.SALARY), incomeCategory); + @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