From e25c7fd59b8642a3eec0bbc80753fe34d3531d5c Mon Sep 17 00:00:00 2001 From: HSoreide <sofie.scisly@gmail.com> Date: Sun, 5 Mar 2023 09:53:07 +0100 Subject: [PATCH] Create Income and Expense as subclasses of Item with respective category enum classes --- .../no/ntnu/idatt1002/demo/data/Expense.java | 34 +++++++++++ .../{Category.java => ExpenseCategory.java} | 2 +- .../no/ntnu/idatt1002/demo/data/Income.java | 11 ++++ .../idatt1002/demo/data/IncomeCategory.java | 6 ++ .../no/ntnu/idatt1002/demo/data/Item.java | 56 ++++++------------- .../no/ntnu/idatt1002/demo/data/ItemTest.java | 4 +- 6 files changed, 70 insertions(+), 43 deletions(-) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Expense.java rename src/main/java/no/ntnu/idatt1002/demo/data/{Category.java => ExpenseCategory.java} (71%) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Income.java create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/IncomeCategory.java 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 00000000..a6baabe4 --- /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 dfb85f4b..0d40833f 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 00000000..d01818ac --- /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 00000000..e749c19c --- /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 dceffacc..41a6c851 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 d66acb7c..ba055e33 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 -- GitLab