From b0e620d3810769ee0f8e3603ebe933d3eceae02b Mon Sep 17 00:00:00 2001 From: Andreas <andreksv@ntnu.no> Date: Thu, 9 Mar 2023 13:32:54 +0100 Subject: [PATCH] Changed ItemOverView to be a superclass for Income and Expense --- .../idatt1002/demo/data/ItemOverview.java | 68 +++++++------------ .../idatt1002/demo/data/ItemOverviewTest.java | 43 +++++------- 2 files changed, 40 insertions(+), 71 deletions(-) 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 2c4f1551..03fcdb81 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java @@ -3,72 +3,52 @@ package no.ntnu.idatt1002.demo.data; import java.util.ArrayList; /** - * RevenueOverview is a class for storing and getting - * information on your total income and expense. + * ItemOverview is a class for storing and getting + * information on items. Is also a + * superclass for Income- and Expense overview. TODO: Make income and expense overview */ public class ItemOverview { - ArrayList<Income> income; - ArrayList<Expense> expense; + ArrayList<Item> items; /** - * The class constructor. + * An "empty" class constructor. */ public ItemOverview(){ - this.income = new ArrayList<>(); //ArrayList for storing income - this.expense = new ArrayList<>(); //ArrayList for storing expense + this.items = new ArrayList<>(); //ArrayList for storing item´s } /** - * Get an ArrayList of every income. - * @return Income ArrayList. + * Class constructor that takes in an ArrayList of Item´s as argument + * @param items An ArrayList of the Item´s you want to overview */ - public ArrayList<Income> getIncome() { - return income; + public ItemOverview(ArrayList<Item> items){ + this.items = items; } /** - * Get an ArrayList of every expense. - * @return Expense ArrayList. + * Get an ArrayList of every item. + * @return item ArrayList. */ - public ArrayList<Expense> getExpense() { - return expense; + public ArrayList<Item> getItems() { + return items; } /** - * Add an Income object to income. - * @param newIncome The Income you want to add. + * Add an Item object to items. + * @param newItem The Item you want to add. */ - public void addIncome(Income newIncome){ - if(income.contains(newIncome)){ - throw new IllegalArgumentException("This income is already registered"); + public void addItem(Income newItem){ + if(items.contains(newItem)){ + throw new IllegalArgumentException("This item is already registered"); } - income.add(newIncome); + items.add(newItem); } /** - * Add an Expense object to income. - * @param newExpense The Expense you want to add. + * Get the sum of all Item´s in items + * @return Sum of all Item´s */ - public void addExpense(Expense newExpense){ - if(expense.contains(newExpense)){ - throw new IllegalArgumentException("This expense is already registered"); - } - expense.add(newExpense); - } - - /** - * Get the sum of all Income in income. - * @return Sum of all Income. - */ - public double getTotalIncome(){ - return income.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum(); - } - - /** - * Get the sum of all Expense in expense. - * @return Sum of all Expense. - */ - public double getTotalExpense(){ - return expense.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum(); + public double getTotalSum(){ + return items.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum(); } } diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java index 61861aaf..ef9b58f7 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java @@ -4,8 +4,7 @@ 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; +import static org.junit.jupiter.api.Assertions.*; public class ItemOverviewTest { ItemOverview itemOverview; @@ -14,38 +13,28 @@ public class ItemOverviewTest { 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")); + @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, () -> {itemOverview.addItem(income); itemOverview.addItem(income);}); } @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")); + @DisplayName("addItem method does not throw exception when it should not") + void addItemDoesNotThrow(){ + Income income1 = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); + Income income2 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23"); + assertDoesNotThrow(() -> {itemOverview.addItem(income1); itemOverview.addItem(income2);}); } @Test - @DisplayName("getTotalIncome method gives correct amount") + @DisplayName("getTotalSum 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")); + itemOverview.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); + itemOverview.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21")); + itemOverview.addItem(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)); + assertEquals(Math.round(itemOverview.getTotalSum()), 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)); - } -} +} \ No newline at end of file -- GitLab