diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java b/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java new file mode 100644 index 0000000000000000000000000000000000000000..a6baabe46d9798b77f157f06f87f071f01f0d3bf --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Expense.java @@ -0,0 +1,34 @@ +package no.ntnu.idatt1002.demo.data; + +public class Expense extends Item{ + + private ExpenseCategory category; + + + public Expense(double amount, boolean recurring) { + super(amount, recurring); + } + + public Expense(String description, double amount, boolean recurring) { + super(description, amount, recurring); + } + + /** + * The method returns the category to which the Item belongs as a value of the Category enum class. + * @return The category the Item belongs to as a value of the Category enum class. + */ + public ExpenseCategory getCategory() { + return category; + } + + /** + * The method sets the category of an item to a value of the Category enum class. + * @param expenseCategory The category to which the Item should belong. + */ + public void setCategory(ExpenseCategory expenseCategory) { + if(expenseCategory == null) { + throw new IllegalArgumentException("The category must be valid."); + } + this.category = expenseCategory; + } +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Category.java b/src/main/java/no/ntnu/idatt1002/demo/data/ExpenseCategory.java similarity index 71% rename from src/main/java/no/ntnu/idatt1002/demo/data/Category.java rename to src/main/java/no/ntnu/idatt1002/demo/data/ExpenseCategory.java index dfb85f4b2b903cf763fe4d653afe757402e4d2ff..0d40833f4ff319c053a3bdea2f6c70430c64294f 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Category.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/ExpenseCategory.java @@ -1,6 +1,6 @@ package no.ntnu.idatt1002.demo.data; -public enum Category { +public enum ExpenseCategory { FOOD, CLOTHES, diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Income.java b/src/main/java/no/ntnu/idatt1002/demo/data/Income.java new file mode 100644 index 0000000000000000000000000000000000000000..d01818ac3cc82187b5d41f1152c9293896165de2 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Income.java @@ -0,0 +1,11 @@ +package no.ntnu.idatt1002.demo.data; + +public class Income extends Item{ + public Income(double amount, boolean recurring) { + super(amount, recurring); + } + + public Income(String description, double amount, boolean recurring) { + super(description, amount, recurring); + } +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java b/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java new file mode 100644 index 0000000000000000000000000000000000000000..e749c19cc83599d719fe689e5ed40ea0cb87babe --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java @@ -0,0 +1,6 @@ +package no.ntnu.idatt1002.demo.data; + +public enum IncomeCategory { + SALARY, + STUDENT_LOAN +} 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 dceffaccf0fa1ee20ba06b6caa78f8bb62f252cf..41a6c85191b2438e34829a401dc999c36f7f0489 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java @@ -8,24 +8,20 @@ package no.ntnu.idatt1002.demo.data; * */ public class Item { - private Category category; private String description = ""; - private float price; + private double amount; private final boolean recurring; /** - * The constructor of a new Item object takes in a Category and a price. The category is a value of the - * Category enum class, whereas the price is provided as a float. If category or price is left blank, an + * 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 category The category the Item belongs to. - * @param price The price of an Item as a float. + * @param amount price of an Item as a float. */ - public Item (Category category, float price, boolean recurring){ - if(category == null || price <= 1.0f) { - throw new IllegalArgumentException("The item must have a category and a price."); + public Item (double amount, boolean recurring){ + if(amount <= 1.0f) { + throw new IllegalArgumentException("The item must have an amount."); } else { - this.category = category; - this.price = price; + this.amount = amount; this.recurring = recurring; } } @@ -34,34 +30,14 @@ public class Item { * The constructor instantiates a new Item object with a category, a description and a price as arguments. It * overloads the first constructor to set the category and price and then sets the description of the item. * If either 0category or price is left blank, an IllegalArgumentException is thrown. - * @param category The category the Item belongs to. * @param description A description of the item as a String. - * @param price The price of the item as a float. + * @param amount The price of the item as a float. */ - public Item (Category category, String description, float price, boolean recurring){ - this(category, price, recurring); + public Item (String description, double amount, boolean recurring){ + this(amount, recurring); this.description=description; } - /** - * The method returns the category to which the Item belongs as a value of the Category enum class. - * @return The category the Item belongs to as a value of the Category enum class. - */ - public Category getCategory() { - return category; - } - - /** - * The method sets the category of an item to a value of the Category enum class. - * @param category The category to which the Item should belong. - */ - public void setCategory(Category category) { - if(category == null) { - throw new IllegalArgumentException("The category must be valid."); - } - this.category = category; - } - /** * The method returns the description of the given Item object as a String. * @return The description of the Item as a String. @@ -80,18 +56,18 @@ public class Item { /** * The method returns the price of the given Item object as a float. - * @return The price of the Item as a float. + * @return The amount of the Item as a float. */ - public float getPrice() { - return price; + public double getAmount() { + return amount; } /** * The method changes the price of the given Item to the passed price as a float. - * @param price The new price of the Item as a float. + * @param amount The new price of the Item as a float. */ - public void setPrice(float price) { - this.price = price; + public void setAmount(double amount) { + this.amount = amount; } /** 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 d66acb7ce1ccb0e5077608660438b9cec565783b..ba055e336b8e1b4fe570b138f60ebd79d53f7841 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java @@ -13,14 +13,14 @@ class ItemTest { Exception noCategory = assertThrows(IllegalArgumentException.class, () -> new Item(null, "description", 89.9f)); assertEquals("The item must have a category and a price.", noCategory.getMessage()); - Exception noPrice = assertThrows(IllegalArgumentException.class, () -> new Item(Category.OTHER, "description", 0f)); + Exception noPrice = assertThrows(IllegalArgumentException.class, () -> new Item(ExpenseCategory.OTHER, "description", 0f)); assertEquals("The item must have a category and a price.", noPrice.getMessage()); }; @Test @DisplayName("The Item constructor does not throw exceptions when it should not.") void constructorDoesThrow() { - assertDoesNotThrow(() -> new Item(Category.OTHER, "descriotion", 59.9f)); + assertDoesNotThrow(() -> new Item(ExpenseCategory.OTHER, "descriotion", 59.9f)); } } \ No newline at end of file