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

Added Equals method

parent 0e9e3fb3
No related branches found
No related tags found
1 merge request!5Created register for storing information on Income and Expense
Pipeline #205200 failed
package no.ntnu.idatt1002.demo.data.Economics;
import java.util.ArrayList;
/**
*ExpenseOverview is an abstract class for storing and getting
* information on Expense.
*/
public class ExpenseOverview {
private ArrayList<Expense> expenses;
public ExpenseOverview(){
this.expenses = new ArrayList<>();
}
/**
* Class constructor that takes in an ArrayList of Expense´s as argument
* @param expenses An ArrayList of the Expense´s you want to overview
*/
public ExpenseOverview(ArrayList<Expense> expenses){
this.expenses = expenses;
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import java.util.ArrayList;
/**
* IncomeOverview is an abstract class for storing and getting
* information on income.
*/
public class IncomeOverview extends ItemOverview {
private ArrayList<Income> income;
/**
* An "empty" class constructor.
*/
public IncomeOverview(){
this.income = new ArrayList<>();
}
/**
* Class constructor that takes in an ArrayList of Income as argument
* @param income An ArrayList of the Income you want to overview
*/
public IncomeOverview(ArrayList<Income> income){
this.income = income;
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import java.util.Objects;
/**
* The Item class represents a good or service purchased in real life. The item belongs to a category and
* has an amount, description and a date. The description may be left blank, but the Item must belong to a category and must
......@@ -110,6 +112,18 @@ public abstract class Item {
this.date = newDate;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Item item)) return false;
return Double.compare(item.amount, amount) == 0 && recurring == item.recurring && Objects.equals(description, item.description) && Objects.equals(date, item.date);
}
@Override
public int hashCode() {
return Objects.hash(description, amount, recurring, date);
}
/* @Override
public boolean equals(Object obj) {
if (this == obj) {
......
......@@ -6,48 +6,47 @@ import java.util.ArrayList;
* ItemOverview is an abstract class for storing and getting.
* information on items. Superclass for Income- and ExpenseOverview.
*/
public abstract class ItemOverview {
ArrayList<Item> items;
public class ItemOverview<T>{
ArrayList<T> items;
/**
* An "empty" class constructor.
*/
public ItemOverview(){
public ItemOverview() {
this.items = new ArrayList<>(); //ArrayList for storing item´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.
* @param items An ArrayList of the Item´s you want to overview.
*/
public ItemOverview(ArrayList<Item> items){
public ItemOverview(ArrayList<T> items) {
this.items = items;
}
/**
* Get an ArrayList of every item.
* @return item ArrayList.
* @return item ArrayList.
*/
public ArrayList<Item> getItems() {
public ArrayList<T> getItems() {
return items;
}
/**
* Add an Item object to items.
* @param newItem The Item you want to add.
*/
public void addItem(Item newItem){
public void addItem(T newItem){
if(items.contains(newItem)){
throw new IllegalArgumentException("This item is already registered");
}
items.add(newItem);
}
/**
* Get the sum of all Item´s in items.
* @return Sum of all Item´s.
* @return Sum of all Item´s.
*/
public double getTotalSum(){
return items.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum();
ArrayList<Item> castedItems = (ArrayList<Item>) items;
return castedItems.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum();
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeCategory;
import no.ntnu.idatt1002.demo.data.Economics.IncomeOverview;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
public class IncomeOverviewTest {
IncomeOverview incomeOverview;
ItemRegister<Income> incomeRegister;
@BeforeEach
public void createItemOverView(){
incomeOverview = new IncomeOverview();
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, () -> {incomeOverview.addItem(income); incomeOverview.addItem(income);});
assertThrows(IllegalArgumentException.class, () -> {
incomeRegister.addItem(income);
incomeRegister.addItem(income);});
}
@Test
@DisplayName("addItem method does not throw exception when it should not")
void addItemDoesNotThrow(){
Income expense = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
Income income = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
assertDoesNotThrow(() -> {incomeOverview.addItem(expense); incomeOverview.addItem(income);});
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(){
incomeOverview.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
incomeOverview.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"));
incomeOverview.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
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;
assertEquals(Math.round(incomeOverview.getTotalSum()), Math.round(totalIncome));
System.out.println(incomeRegister.getTotalSum());
assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome));
}
@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);
}
}
\ No newline at end of file
}
\ 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