Skip to content
Snippets Groups Projects
Commit 58f5e66b authored by HSoreide's avatar HSoreide
Browse files

Add date as a property to Item

parent 73950527
No related branches found
No related tags found
1 merge request!3Hs item class
Pipeline #202362 passed
......@@ -11,9 +11,10 @@ public class Expense extends Item{
* @param amount The amount of the current Expense object as a double.
* @param recurring True if the current Expense repeats at regular intervals.
* @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory.
* @param date The date of the Expense at format "dd.mm.yy".
*/
public Expense(double amount, boolean recurring, ExpenseCategory category) {
super(amount, recurring);
public Expense(double amount, boolean recurring, ExpenseCategory category, String date) {
super(amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
......@@ -28,9 +29,10 @@ public class Expense extends Item{
* @param amount The amount of the current Expense object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory
* @param date The date of the Expense at format "dd.mm.yy".
*/
public Expense(String description, double amount, boolean recurring, ExpenseCategory category) {
super(description, amount, recurring);
public Expense(String description, double amount, boolean recurring, ExpenseCategory category, String date) {
super(description, amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
......
......@@ -18,9 +18,10 @@ public class Income extends Item{
* @param amount The amount of the current income object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the Income belongs to, provided as a value of IncomeCategory.
* @param date The date of the Income at format "dd.mm.yy".
*/
public Income(double amount, boolean recurring, IncomeCategory category) {
super(amount, recurring);
public Income(double amount, boolean recurring, IncomeCategory category, String date) {
super(amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
......@@ -34,10 +35,11 @@ public class Income extends Item{
* @param description A description of the income as a String.
* @param amount The amount of the current income object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the income belongs to, provided as a value of IncomeCategory
* @param category The category to which the income belongs to, provided as a value of IncomeCategory.
* @param date The date of the Income at format "dd.mm.yy".
*/
public Income(String description, double amount, boolean recurring, IncomeCategory category) {
super(description, amount, recurring);
public Income(String description, double amount, boolean recurring, IncomeCategory category, String date) {
super(description, amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
......
......@@ -2,5 +2,6 @@ package no.ntnu.idatt1002.demo.data;
public enum IncomeCategory {
SALARY,
STUDENT_LOAN
STUDENT_LOAN,
GIFT
}
package no.ntnu.idatt1002.demo.data;
/**
* The Item class represents an object or service purchased in real life. The item belongs to a category and
* has a price and description. The description may be left blank, but the Item must belong to a category and must
* 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
* have a price.
* @author HanneSofie
*
......@@ -10,19 +10,21 @@ package no.ntnu.idatt1002.demo.data;
public class Item {
private String description = "";
private double amount;
private final boolean recurring;
private boolean recurring;
private String date; // Format example: 09.08.23
/**
* The constructor of a new Item object takes in an amount as a double. If the amount is left blank, an
* IllegalArgumentException is thrown.
* @param amount price of an Item as a float.
*/
public Item (double amount, boolean recurring){
if(amount <= 1.0f) {
throw new IllegalArgumentException("The item must have an amount.");
public Item (double amount, boolean recurring, String date){
if(amount <= 1.0f || date.isBlank()) {
throw new IllegalArgumentException("Both amount and date must be provided.");
} else {
this.amount = amount;
this.recurring = recurring;
this.date = date;
}
}
......@@ -33,8 +35,8 @@ public class Item {
* @param description A description of the item as a String.
* @param amount The price of the item as a float.
*/
public Item (String description, double amount, boolean recurring){
this(amount, recurring);
public Item (String description, double amount, boolean recurring, String date){
this(amount, recurring, date);
this.description=description;
}
......@@ -67,6 +69,9 @@ public class Item {
* @param amount The new price of the Item as a float.
*/
public void setAmount(double amount) {
if(amount <= 1.0f ) {
throw new IllegalArgumentException("A positive amount must be provided.");
}
this.amount = amount;
}
......@@ -78,7 +83,34 @@ public class Item {
return recurring;
}
/* @Override
/**
* The method changes the boolean value of the 'recurring' field of the Item object.
* @param recurring A boolean value being true if the Item is recurring and false otherwise.
*/
public void setRecurring(boolean recurring) {
this.recurring = recurring;
}
/**
* The method returns the date of the item object (income/expense).
* @return The date of the transaction.
*/
public String getDate() {
return date;
}
/**
* Sets the date field of the Item object to a new String provided as an argument.
* @param newDate The new date with which to record the Item.
*/
public void setDate(String newDate) {
if(date.isBlank()) {
throw new IllegalArgumentException("A date must be provided.");
}
this.date = newDate;
}
/* @Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
......
......@@ -10,14 +10,14 @@ class ExpenseTest {
@Test
@DisplayName("The Expense constructor throws exceptions when it should.")
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Expense("description", 8.5f, false, null));
assertThrows(IllegalArgumentException.class, () -> new Expense("description", -10.0f, false, ExpenseCategory.BOOKS));
assertThrows(IllegalArgumentException.class, () -> new Expense("description", 8.5f, false, null, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Expense("description", -10.0f, false, ExpenseCategory.BOOKS, "03.03.23"));
};
@Test
@DisplayName("The Expense constructor does not throw exceptions when it should not.")
void constructorDoesThrow() {
assertDoesNotThrow(() -> new Expense("description", 59.9f, false, ExpenseCategory.BOOKS));
assertDoesNotThrow(() -> new Expense("description", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23"));
}
}
\ No newline at end of file
......@@ -10,14 +10,14 @@ class IncomeTest {
@Test
@DisplayName("The Income constructor throws exceptions when it should.")
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Income("description", 8.5f, false, null));
assertThrows(IllegalArgumentException.class, () -> new Income("description", -10.0f, false, IncomeCategory.SALARY));
assertThrows(IllegalArgumentException.class, () -> new Income("description", 8.5f, false, null, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Income("description", -10.0f, false, IncomeCategory.SALARY, "03.03.23"));
};
@Test
@DisplayName("The Income constructor does not throw exceptions when it should not.")
void constructorDoesThrow() {
assertDoesNotThrow(() -> new Income("description", 59.9f, false, IncomeCategory.SALARY));
assertDoesNotThrow(() -> new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
}
......
......@@ -10,14 +10,15 @@ class ItemTest {
@Test
@DisplayName("The Item constructor throws exceptions when it should.")
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Item("description", 0f, false));
assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false));
assertThrows(IllegalArgumentException.class, () -> new Item("description", 0f, false, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false, ""));
};
@Test
@DisplayName("The Item constructor does not throw exceptions when it should not.")
void constructorDoesThrow() {
assertDoesNotThrow(() -> new Item("description", 59.9f, false));
assertDoesNotThrow(() -> new Item("description", 59.9f, false, "03.03.23"));
}
}
\ 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