Skip to content
Snippets Groups Projects
Commit 2bc5212c authored by Anders Emil Bergan's avatar Anders Emil Bergan
Browse files

Merge branch 'FileHandlingAndreas' into 'master'

Added FileHandling class for ItemRegister objects, and reworked ItemRegister with new subclasses

See merge request !9
parents 80928ee5 9e10ea55
No related branches found
No related tags found
1 merge request!9Added FileHandling class for ItemRegister objects, and reworked ItemRegister with new subclasses
Pipeline #209254 passed
Showing
with 565 additions and 164 deletions
......@@ -58,4 +58,14 @@ public class Expense extends Item{
}
this.category = expenseCategory;
}
/**
* Returns a String that represents the Expense.
* Also includes the ExpenseCategory
* @return The Expense as a String
*/
@Override
public String toString() {
return super.toString()+"\ncategory="+category.toString()+"\n";
}
}
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();
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
/**
* FileHandling is a class for writing and reading
* Item-objects to and from a file.
*/
public class FileHandling {
/**
* Method for writing (adding) an ItemRegister to a file.
*
* @param itemRegister the ItemRegister you want to write to a file.
* @throws IOException if an input or output exception occurred.
*/
public <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/" + fileTitle + ".itemRegister"))) {
bw.write(itemRegister.toString());
} catch (IOException ex) {
throw new IOException("Error writing story to file: " + ex.getMessage());
}
}
public ItemRegister<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException {
IncomeRegister incomeRegister = new IncomeRegister();
String date = "";
String description = "";
double amount = 0;
boolean reoccuring = false;
IncomeCategory incomeCategory = null;
try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
br.readLine();
String line;
String nextLine = br.readLine();
while ((line = nextLine) != null) {
nextLine = br.readLine();
if(line.startsWith("date=")) {
date = line.replace("date=", "");
} else if (line.startsWith("description=")) {
description = line.replace("description=","");
} else if (line.startsWith("amount=")) {
amount = Double.parseDouble(line.replace("amount=",""));
} else if (line.startsWith("isReoccuring=")) {
reoccuring = line.replace("isReoccuring=","").equals("Reoccurring");
} else if (line.startsWith("category=")) {
line = line.replace("category=","");
incomeCategory = switch (line) {
case "GIFT" -> IncomeCategory.GIFT;
case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN;
default -> IncomeCategory.SALARY;
};
}
if(line.isEmpty() || (nextLine == null)) {
if(description.equals("")){
incomeRegister.addItem(new Income(amount, reoccuring, incomeCategory, date));
} else{
incomeRegister.addItem(new Income(description, amount, reoccuring, incomeCategory, date));
}
description = "";
}
}
}
return incomeRegister;
}
public ItemRegister<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException {
ExpenseRegister expenseRegister = new ExpenseRegister();
String date = "";
String description = "";
double amount = 0;
boolean reoccuring = false;
ExpenseCategory expenseCategory = null;
try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
br.readLine();
String line;
String nextLine = br.readLine();
while ((line = nextLine) != null) {
nextLine = br.readLine();
if (line.startsWith("date=")) {
date = line.replace("date=", "");
} else if (line.startsWith("description=")) {
description = line.replace("description=", "");
} else if (line.startsWith("amount=")) {
amount = Double.parseDouble(line.replace("amount=", ""));
} else if (line.startsWith("isReoccuring=")) {
reoccuring = line.replace("isReoccuring=", "").equals("Reoccurring");
} else if (line.startsWith("category=")) {
line = line.replace("category=", "");
expenseCategory = switch (line) {
case "FOOD" -> ExpenseCategory.FOOD;
case "CLOTHES" -> ExpenseCategory.CLOTHES;
case "BOOKS" -> ExpenseCategory.BOOKS;
default -> ExpenseCategory.OTHER;
};
}
if (line.isEmpty() || (nextLine == null)) {
if (description.equals("")) {
expenseRegister.addItem(new Expense(amount, reoccuring, expenseCategory, date));
} else {
expenseRegister.addItem(new Expense(description, amount, reoccuring, expenseCategory, date));
}
description = "";
}
}
}
return expenseRegister;
}
}
\ No newline at end of file
......@@ -66,6 +66,13 @@ public class Income extends Item{
this.category = category;
}
/**
* Returns a String that represents the Income.
* Also includes the IncomeCategory
* @return The Income as a String
*/
@Override
public String toString() {
return super.toString()+"\ncategory="+category.toString()+"\n";
}
}
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();
}
}
......@@ -124,6 +124,27 @@ public abstract class Item {
return Objects.hash(description, amount, recurring, date);
}
/**
* Returns a String that represents the Item
* @return The Item as a String
*/
@Override
public String toString() {
String isReoccuring = "";
if(recurring){
isReoccuring = "Reoccurring";
}
else{
isReoccuring = "Not reoccurring";
}
if(!description.isBlank()){
return "\ndate=" + date+"\ndescription=" + description+"\namount="+amount+"\nisReoccuring="+isReoccuring;
}
else{
return "\ndate="+date+"\namount="+amount+"\nisReoccuring="+isReoccuring;
}
}
/* @Override
public boolean equals(Object obj) {
if (this == obj) {
......
package no.ntnu.idatt1002.demo.data.Economics;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.List;
/**
* ItemRegister is a generic class used by ExpenseRegister and
* IncomeRegister.
* @param <T> Income or Expense
* ItemRegister is a generic class used
* for storing either Income or Expense.
*
* @param <T> Income or Expense
*/
public class ItemRegister<T>{
ArrayList<T> items;
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<>(); //ArrayList for storing T´s
this.items = new ArrayList<>();
}
/**
* Class constructor that takes in an ArrayList of T as argument.
* @param items An ArrayList of Income or Expense you want to overview.
* Class constructor that takes in a List of T=(Income or Expense) as argument.
*
* @param items the List of T=(Income or Expense) you want to register.
*/
public ItemRegister(ArrayList<T> items) {
public ItemRegister(List<T> items) {
this.items = items;
}
/**
* Get an ArrayList of every T.
* @return T ArrayList.
* Get a List of every T=(Income or Expense).
* @return T=(Income or Expense) List.
*/
public ArrayList<T> getItems() {
public List<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.
* 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){
public void addItem(T newItem) throws IllegalArgumentException{
if(items.contains(newItem)){
throw new IllegalArgumentException("This item is already registered");
}
......@@ -49,21 +51,34 @@ public class ItemRegister<T>{
}
/**
* Get the sum of all T´s in items.
* @return Sum of all T´s.
* Check if items is empty.
*
* @return true or false depending on if items is empty.
*/
public boolean isEmpty(){
return this.items.isEmpty();
}
/**
* 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(){
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 items.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;}
@Override
public String toString() {
StringBuilder stringItems = new StringBuilder();
for(Item item : items) {
stringItems.append(item.toString());
}
if(stringItems.isEmpty()) {
return "The register is Empty";
}
else {
return stringItems.toString();
}
}
}
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));
}
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
class FileHandlingTest {
FileHandling fileHandling = new FileHandling();
ExpenseRegister expenseRegister = new ExpenseRegister();
IncomeRegister incomeRegister = new IncomeRegister();
@Nested
@DisplayName("FileHandling IncomeRegister to file")
class fileHandlingIncomeRegisterToFile{
String fileTitle = "incomeRegisterTest";
@Nested
@DisplayName("FileHandling incomeRegister with income that has description")
class fileHandlingIncomeRegisterWithIncomeThatHasDescription{
@BeforeEach
void addIncomeToIncomeRegister(){
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
incomeRegister.addItem(income1);
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
}
@Test
@DisplayName("Reading from file gives correct incomeRegister back")
void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException {
assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString());
}
}
@Nested
@DisplayName("FileHandling incomeRegister with income that does not have description")
class fileHandlingIncomeRegisterWithIncomeThatDoesNotHaveDescription{
@BeforeEach
void addIncomeToIncomeRegister(){
Income income1 = new Income(59.9f, false, IncomeCategory.GIFT, "03.03.23");
incomeRegister.addItem(income1);
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
}
@Test
@DisplayName("Reading from file gives correct incomeRegister back")
void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException {
assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString());
}
}
@Nested
@DisplayName("FileHandling incomeRegister with multiple Income")
class fileHandlingIncomeRegisterWithMultipleIncome{
@BeforeEach
void addIncomeToIncomeRegister(){
Income income1 = new Income(59.9f, false, IncomeCategory.GIFT, "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);
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
}
@Test
@DisplayName("Reading from file gives correct incomeRegister back")
void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException {
assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString());
}
}
}
@Nested
@DisplayName("FileHandling ExpenseRegister to file")
class fileHandlingExpenseRegisterToFile{
String fileTitle = "expenseRegisterTest";
@Nested
@DisplayName("FileHandling ExpenseRegister with Expense that has description")
class fileHandlingExpenseRegisterWithExpenseThatHasDescription{
@BeforeEach
void addExpenseToExpenseRegister(){
Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense1);
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
}
@Test
@DisplayName("Reading from file gives correct expenseRegister back")
void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException {
assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString());
}
}
@Nested
@DisplayName("FileHandling ExpenseRegister with Expense that does not have description")
class fileHandlingExpenseRegisterWithExpenseThatDoesNotHaveDescription{
@BeforeEach
void addExpenseToExpenseRegister(){
Expense expense1 = new Expense(59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense1);
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
}
@Test
@DisplayName("Reading from file gives correct expenseRegister back")
void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException {
assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString());
}
}
@Nested
@DisplayName("FileHandling ExpenseRegister with multiple Expense")
class fileHandlingExpenseRegisterWithMultipleExpensen{
@BeforeEach
void addExpenseToExpenseRegister(){
Expense expense1 = new Expense(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);
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
}
@Test
@DisplayName("Reading from file gives correct expenseRegister back")
void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException {
assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString());
}
}
}
}
\ No newline at end of file
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 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));
}
}
}
\ 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