diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java b/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java index 06d8d71617204348a5cbc1a8b2fd789998d205b3..15a59f7e76e795eab11785935ba4b4c47ea0627c 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java @@ -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."); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Income.java b/src/main/java/no/ntnu/idatt1002/demo/data/Income.java index 20bab51cd67487900312bd71af7242ec0291ff5c..10e2f7ae936cf79681999a5373f5e48f6b9ef147 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Income.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Income.java @@ -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."); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java b/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java index e749c19cc83599d719fe689e5ed40ea0cb87babe..1ead533938575fd5483f2b4b32c3d00f8d7dafd0 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java @@ -2,5 +2,6 @@ package no.ntnu.idatt1002.demo.data; public enum IncomeCategory { SALARY, - STUDENT_LOAN + STUDENT_LOAN, + GIFT } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java index 41a6c85191b2438e34829a401dc999c36f7f0489..562b2999c66603bdbeb78b5b3ad1f36e061c1615 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java @@ -1,8 +1,8 @@ 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; diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java index 52387d92cd4a847f4e48a9247faf819916ac2062..e72597e935fe46c9778707d2bd5914beb80d5e82 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/ExpenseTest.java @@ -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 diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java index 71071f5025cfe1de22f603a62d650e10d52fb3a4..6e71180496623fbc78971c3ea22be4ab061f2f1f 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/IncomeTest.java @@ -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")); } diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java index 0d3d0a08079fcf768cfb4f42037d1803b56673a7..7ab5a18f900b32ea7ba05230739bdb74fa8c42c4 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java @@ -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