Skip to content
Snippets Groups Projects
Commit 1407bbf5 authored by Andreas's avatar Andreas
Browse files

Remade ItemOverview with Generics, and fixed tests

parent af4ba34b
No related branches found
No related tags found
1 merge request!5Created register for storing information on Income and Expense
Pipeline #205201 passed
package no.ntnu.idatt1002.demo.data.Economics; package no.ntnu.idatt1002.demo.data.Economics;
import java.util.ArrayList; 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. * ItemRegister is a generic class used by ExpenseRegister and
* information on items. Superclass for Income- and ExpenseOverview. * IncomeRegister.
* @param <T> Income or Expense
*/ */
public class ItemOverview<T>{ public class ItemRegister<T>{
ArrayList<T> items; ArrayList<T> items;
/** /**
* An "empty" class constructor. * An "empty" class constructor.
*/ */
public ItemOverview() { public ItemRegister() {
this.items = new ArrayList<>(); //ArrayList for storing item´s this.items = new ArrayList<>(); //ArrayList for storing T´s
} }
/** /**
* Class constructor that takes in an ArrayList of Item´s as argument. * Class constructor that takes in an ArrayList of T as argument.
* @param items An ArrayList of the Item´s you want to overview. * @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; this.items = items;
} }
/** /**
* Get an ArrayList of every item. * Get an ArrayList of every T.
* @return item ArrayList. * @return T ArrayList.
*/ */
public ArrayList<T> getItems() { public ArrayList<T> getItems() {
return items; 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){ public void addItem(T newItem){
if(items.contains(newItem)){ if(items.contains(newItem)){
throw new IllegalArgumentException("This item is already registered"); throw new IllegalArgumentException("This item is already registered");
...@@ -39,14 +48,22 @@ public class ItemOverview<T>{ ...@@ -39,14 +48,22 @@ public class ItemOverview<T>{
items.add(newItem); items.add(newItem);
} }
/** /**
* Get the sum of all Item´s in items. * Get the sum of all T´s in items.
* @return Sum of all Item´s. * @return Sum of all T´s.
*/ */
public double getTotalSum(){ 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(); 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;}
}
} }
...@@ -2,57 +2,129 @@ package no.ntnu.idatt1002.demo.data.Economics; ...@@ -2,57 +2,129 @@ package no.ntnu.idatt1002.demo.data.Economics;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class IncomeOverviewTest { public class ItemRegisterTest {
ItemRegister<Income> incomeRegister; ItemRegister<Income> incomeRegister;
ItemRegister<Expense> expenseRegister;
@BeforeEach @Nested
public void createItemOverView(){ @DisplayName("Test ItemRegister when using Income")
incomeRegister = new ItemRegister(); class incomeRegisterTests {
} @BeforeEach
@Test public void createItemOverView() {
@DisplayName("addItem method throws exception when it should") incomeRegister = new ItemRegister();
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 @Test
@DisplayName("addItem method does not throw exception when it should not") @DisplayName("addItem method throws exception when it should")
void addItemDoesNotThrow(){ void addItemThrows() {
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23"); Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
Income income2 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23"); assertThrows(IllegalArgumentException.class, () -> {
assertDoesNotThrow(() -> { incomeRegister.addItem(income);
incomeRegister.addItem(income1); incomeRegister.addItem(income2);}); 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 incomeRegister.addItem(income1);
@DisplayName("getTotalSum method gives correct amount") incomeRegister.addItem(income2);
void getTotalSumCorrectAmount(){ incomeRegister.addItem(income3);
incomeRegister.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); ArrayList<Income> incomeSalary = incomeRegister.getItemsBasedOnCategory(IncomeCategory.SALARY);
incomeRegister.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21")); assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3));
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));
} }
@Test @Nested
void getIncomeBasedOnCategoryGivesCorrectIncome(){ @DisplayName("Test ItemRegister when using Expense")
incomeRegister.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23")); class expenseRegisterTests {
incomeRegister.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21")); @BeforeEach
incomeRegister.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23")); public void createItemOverView() {
ArrayList<Income> incomeCategory = new ArrayList<>(); expenseRegister = new ItemRegister();
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); @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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment