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

Create Income and Expense as subclasses of Item with respective category enum classes

parent 7800a8b5
No related branches found
No related tags found
1 merge request!3Hs item class
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;
}
}
package no.ntnu.idatt1002.demo.data;
public enum Category {
public enum ExpenseCategory {
FOOD,
CLOTHES,
......
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);
}
}
package no.ntnu.idatt1002.demo.data;
public enum IncomeCategory {
SALARY,
STUDENT_LOAN
}
......@@ -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;
}
/**
......
......@@ -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
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