Skip to content
Snippets Groups Projects
Commit 9e10ea55 authored by Andreas's avatar Andreas
Browse files

Made subclasses to ItemRegister class and tests

parent 1f9b2a26
No related branches found
No related tags found
1 merge request!9Added FileHandling class for ItemRegister objects, and reworked ItemRegister with new subclasses
Pipeline #209245 passed
Showing
with 222 additions and 174 deletions
package no.ntnu.idatt1002.demo.data.Economics;
import java.util.List;
/**
* ExpenseRegister is a class for
* storing Expenses. Subclass of
* ItemRegister.
*/
public class ExpenseRegister extends ItemRegister<Expense> {
/**
* Class constructor that creates an empty List for storing Expense.
*/
public ExpenseRegister() {
super();
}
/**
* Class constructor that takes in a List of Expense as argument.
*
* @param expenses the List of Expense you want to register.
*/
public ExpenseRegister(List<Expense> expenses){
super(expenses);
}
/**
* Method for getting every Expense
* in a given ExpenseCategory.
*
* @param category the ExpenseCategory you want to get every Expense of.
* @return a List of every Expense with category.
*/
public List<Expense> getExpenseByCategory(ExpenseCategory category){
return this.items.stream().filter(expense -> expense.getCategory().compareTo( category) == 0).toList();
}
}
......@@ -4,8 +4,7 @@ import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* FileHandling is a class for writing and reading
......@@ -27,7 +26,7 @@ public class FileHandling {
}
public ItemRegister<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException {
ItemRegister<Income> incomeRegister = new ItemRegister<>();
IncomeRegister incomeRegister = new IncomeRegister();
String date = "";
String description = "";
double amount = 0;
......@@ -69,7 +68,7 @@ public class FileHandling {
}
public ItemRegister<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException {
ItemRegister<Expense> expenseRegister = new ItemRegister<>();
ExpenseRegister expenseRegister = new ExpenseRegister();
String date = "";
String description = "";
double amount = 0;
......
package no.ntnu.idatt1002.demo.data.Economics;
import java.util.List;
/**
* IncomeRegister is a class for
* storing Income. Subclass of
* ItemRegister.
*/
public class IncomeRegister extends ItemRegister<Income>{
/**
* Class constructor that creates an empty List for storing Income.
*/
public IncomeRegister(){
super();
}
/**
* Class constructor that takes in a List of Income as argument.
*
* @param income the List of Income you want to register.
*/
public IncomeRegister(List<Income> income){
super(income);
}
/**
* Method for getting every Income
* in a given IncomeCategory.
* @param category the IncomeCategory you want to get every Income of.
* @return a List of every Income with category.
*/
public List<Income> getIncomeByCategory(IncomeCategory category){
return items.stream().filter(income -> income.getCategory().compareTo(category) == 0).toList();
}
}
......@@ -2,7 +2,6 @@ package no.ntnu.idatt1002.demo.data.Economics;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* ItemRegister is a generic class used
......@@ -10,11 +9,11 @@ import java.util.stream.Collectors;
*
* @param <T> Income or Expense
*/
public class ItemRegister<T extends Item>{
public abstract class ItemRegister<T extends Item>{
List<T> items;
/**
* An "empty" class constructor.
* Class constructor that creates an empty List for storing T=(Income or Expense).
*/
public ItemRegister() {
this.items = new ArrayList<>();
......@@ -23,7 +22,7 @@ public class ItemRegister<T extends Item>{
/**
* 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.
* @param items the List of T=(Income or Expense) you want to register.
*/
public ItemRegister(List<T> items) {
this.items = items;
......@@ -69,39 +68,6 @@ public class ItemRegister<T extends Item>{
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) with the given S=(IncomeCategory or ExpenseCategory).
*/
public <S> List<T> getItemsByCategory(S category) throws IllegalArgumentException, ClassCastException {
//Checks if category is an instance of IncomeCategory
if(category instanceof IncomeCategory) {
try {
ArrayList<Income> castedIncome = (ArrayList<Income>) items;
return (ArrayList<T>) castedIncome.stream().filter(income -> income.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){
try {
ArrayList<Expense> castedExpenses = (ArrayList<Expense>) items;
return (ArrayList<T>) castedExpenses.stream().filter(expense -> expense.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");
}
}
@Override
public String toString() {
StringBuilder stringItems = new StringBuilder();
......
date=03.03.23
description=description
amount=59.900001525878906
isReoccuring=Not reoccurring
category=CLOTHES
date=03.03.23
description=description
amount=59.900001525878906
isReoccuring=Not reoccurring
category=GIFT
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.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ExpenseRegisterTest {
ExpenseRegister expenseRegister = new ExpenseRegister();
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense);
assertThrows(IllegalArgumentException.class, () -> 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));
assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
}
@Nested
@DisplayName("Test getTotalSum and getExpenseByCategory methods")
class testGetExpenseByCategoryMethod {
Expense expense1;
Expense expense2;
Expense expense3;
@BeforeEach
void addExpensesToRegister() {
expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21");
expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23");
expenseRegister.addItem(expense1);
expenseRegister.addItem(expense2);
expenseRegister.addItem(expense3);
}
@Test
@DisplayName("getTotalSum method gives correct amount")
void getTotalSumCorrectAmount() {
double totalIncome = 59.9f + 62.4f + 9.81f;
assertEquals(Math.round(expenseRegister.getTotalSum()), Math.round(totalIncome));
}
@Test
@DisplayName("getExpenseByCategory gives expected Expenses back")
void getExpensesByCategoryGivesExpectedExpensesBack() {
List<Expense> expenseSalary = expenseRegister.getExpenseByCategory(ExpenseCategory.CLOTHES);
assertTrue(expenseSalary.contains(expense1) && expenseSalary.contains(expense3));
}
}
}
......@@ -8,8 +8,8 @@ import static org.junit.jupiter.api.Assertions.*;
class FileHandlingTest {
FileHandling fileHandling = new FileHandling();
ItemRegister<Expense> expenseRegister = new ItemRegister<>();
ItemRegister<Income> incomeRegister = new ItemRegister<>();
ExpenseRegister expenseRegister = new ExpenseRegister();
IncomeRegister incomeRegister = new IncomeRegister();
@Nested
@DisplayName("FileHandling IncomeRegister to file")
class fileHandlingIncomeRegisterToFile{
......
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.List;
import static org.junit.jupiter.api.Assertions.*;
public class IncomeRegisterTest {
IncomeRegister incomeRegister = new IncomeRegister();
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
incomeRegister.addItem(income);
assertThrows(IllegalArgumentException.class, () -> 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));
assertDoesNotThrow(() -> incomeRegister.addItem(income2));
}
@Nested
@DisplayName("test getTotalSum and getIncomeByCategory methods")
class testGetTotalSumAndGetIncomeByCategoryMethods {
Income income1;
Income income2;
Income income3;
@BeforeEach
void addIncomeToRegister() {
income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21");
income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23");
incomeRegister.addItem(income1);
incomeRegister.addItem(income2);
incomeRegister.addItem(income3);
}
@Test
@DisplayName("getTotalSum method gives correct amount")
void getTotalSumCorrectAmount() {
double totalIncome = 59.9f + 62.4f + 9.81f;
assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome));
}
@Test
@DisplayName("getItemsByCategory gives expected Income back")
void getIncomeByCategoryGivesCorrectIncome() {
List<Income> incomeSalary = incomeRegister.getIncomeByCategory(IncomeCategory.SALARY);
assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3));
}
}
}
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 ItemRegisterTest {
ItemRegister<Income> incomeRegister = new ItemRegister<>();
ItemRegister<Expense> expenseRegister = new ItemRegister<>();
@Nested
@DisplayName("Test ItemRegister when using Income")
class incomeRegisterTests {
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
incomeRegister.addItem(income);
assertThrows(IllegalArgumentException.class, () -> 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));
assertDoesNotThrow(() -> incomeRegister.addItem(income2));
}
Income income1;
Income income2;
Income income3;
@BeforeEach
void addIncomeToRegister(){
income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21");
income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23");
incomeRegister.addItem(income1);
incomeRegister.addItem(income2);
incomeRegister.addItem(income3);
}
@Test
@DisplayName("getTotalSum method gives correct amount")
void getTotalSumCorrectAmount() {
double totalIncome = 59.9f + 62.4f + 9.81f;
assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome));
}
@Test
@DisplayName("getItemsByCategory gives expected Income back")
void getIncomeByCategoryGivesCorrectIncome() {
List<Income> incomeSalary = incomeRegister.getItemsByCategory(IncomeCategory.SALARY);
assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3));
}
@Test
@DisplayName("getItemsByCategory throws exception when it takes in ExpenseCategory")
void getIncomeByExpenseCategoryThrowsException(){
assertThrows(ClassCastException.class, () -> incomeRegister.getItemsByCategory(ExpenseCategory.CLOTHES));
}
}
@Nested
@DisplayName("Test ItemRegister when using Expense")
class expenseRegisterTests {
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense);
assertThrows(IllegalArgumentException.class, () -> 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));
assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
}
Expense expense1;
Expense expense2;
Expense expense3;
@BeforeEach
void addExpensesToRegister(){
expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21");
expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23");
expenseRegister.addItem(expense1);
expenseRegister.addItem(expense2);
expenseRegister.addItem(expense3);
}
@Test
@DisplayName("getTotalSum method gives correct amount")
void getTotalSumCorrectAmount(){
double totalIncome = 59.9f + 62.4f + 9.81f;
assertEquals(Math.round(expenseRegister.getTotalSum()), Math.round(totalIncome));
}
@Test
@DisplayName("getItemsByCategory gives expected Expenses back")
void getIncomeBasedOnCategoryGivesCorrectIncome() {
List<Expense> expenseSalary = expenseRegister.getItemsByCategory(ExpenseCategory.CLOTHES);
assertTrue(expenseSalary.contains(expense1) && expenseSalary.contains(expense3));
}
@Test
@DisplayName("getItemsByCategory throws exception when it takes in IncomeCategory")
void getIncomeByExpenseCategoryThrowsException(){
assertThrows(ClassCastException.class, () -> expenseRegister.getItemsByCategory(IncomeCategory.SALARY));
}
}
@Test
@DisplayName("getItemsByCategory throws exception when IncomeCategory or ExpenseCategory is not used")
void getItemsByCategoryThrowsExceptionWhenIncomeCategoryOrExpenseCategoryIsNotUsed(){
assertThrows(IllegalArgumentException.class, () -> incomeRegister.getItemsByCategory("Not IncomeCategory"));
}
}
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