Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
idatt1002_2023_9
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Kluge Svendsrud
idatt1002_2023_9
Commits
b0e620d3
Commit
b0e620d3
authored
2 years ago
by
Andreas
Browse files
Options
Downloads
Patches
Plain Diff
Changed ItemOverView to be a superclass for Income and Expense
parent
5afe7e0b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!5
Created register for storing information on Income and Expense
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java
+24
-44
24 additions, 44 deletions
src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java
src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java
+16
-27
16 additions, 27 deletions
...st/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java
with
40 additions
and
71 deletions
src/main/java/no/ntnu/idatt1002/demo/data/ItemOverview.java
+
24
−
44
View file @
b0e620d3
...
...
@@ -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 I
ncome
object to i
ncome
.
* @param newI
ncome
The I
ncome
you want to add.
* Add an I
tem
object to i
tems
.
* @param newI
tem
The I
tem
you want to add.
*/
public
void
addI
ncome
(
Income
newI
ncome
){
if
(
i
ncome
.
contains
(
newI
ncome
)){
throw
new
IllegalArgumentException
(
"This i
ncome
is already registered"
);
public
void
addI
tem
(
Income
newI
tem
){
if
(
i
tems
.
contains
(
newI
tem
)){
throw
new
IllegalArgumentException
(
"This i
tem
is already registered"
);
}
i
ncome
.
add
(
newI
ncome
);
i
tems
.
add
(
newI
tem
);
}
/**
*
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
();
}
}
This diff is collapsed.
Click to expand it.
src/test/java/no/ntnu/idatt1002/demo/data/ItemOverviewTest.java
+
16
−
27
View file @
b0e620d3
...
...
@@ -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
(
"addI
ncome
method throws exception when it should"
)
void
addI
ncome
Throws
(){
itemOverview
.
addI
ncome
(
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
(
"addI
tem
method throws exception when it should"
)
void
addI
tem
Throws
(){
Income
i
ncome
=
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
(
"getTotal
Income
method gives correct amount"
)
@DisplayName
(
"getTotal
Sum
method gives correct amount"
)
void
getTotalIncomeCorrectAmount
(){
itemOverview
.
addI
ncome
(
new
Income
(
"description1"
,
59.9f
,
false
,
IncomeCategory
.
SALARY
,
"03.03.23"
));
itemOverview
.
addI
ncome
(
new
Income
(
"description2"
,
62.4f
,
true
,
IncomeCategory
.
GIFT
,
"01.02.21"
));
itemOverview
.
addI
ncome
(
new
Income
(
"description3"
,
9.81f
,
false
,
IncomeCategory
.
SALARY
,
"05.07.23"
));
itemOverview
.
addI
tem
(
new
Income
(
"description1"
,
59.9f
,
false
,
IncomeCategory
.
SALARY
,
"03.03.23"
));
itemOverview
.
addI
tem
(
new
Income
(
"description2"
,
62.4f
,
true
,
IncomeCategory
.
GIFT
,
"01.02.21"
));
itemOverview
.
addI
tem
(
new
Income
(
"description3"
,
9.81f
,
false
,
IncomeCategory
.
SALARY
,
"05.07.23"
));
double
totalIncome
=
59.9f
+
62.4f
+
9.81f
;
assertEquals
(
Math
.
round
(
itemOverview
.
getTotal
Income
()),
Math
.
round
(
totalIncome
));
assertEquals
(
Math
.
round
(
itemOverview
.
getTotal
Sum
()),
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment