diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Category.java b/src/main/java/no/ntnu/idatt1002/demo/data/Category.java new file mode 100644 index 0000000000000000000000000000000000000000..dfb85f4b2b903cf763fe4d653afe757402e4d2ff --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Category.java @@ -0,0 +1,9 @@ +package no.ntnu.idatt1002.demo.data; + +public enum Category { + + FOOD, + CLOTHES, + OTHER, + +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Item.java b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java new file mode 100644 index 0000000000000000000000000000000000000000..d56f1299e5cabae13d7da4de2370337209938958 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Item.java @@ -0,0 +1,67 @@ +package no.ntnu.idatt1002.demo.data; + +public class Item { + + private Category category; + private String description = ""; + private float price; + + public Item (Category category, String description, float price){ + if(category == null || price <= 1.0f) { + throw new IllegalArgumentException("The item must have a category and a price."); + } else { + this.category = category; + this.price = price; + this.description = description; + } + } + + + public Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + + + +/* @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj == null) { + return false; + } + + if (obj.getClass() != this.getClass()) { + return false; + } + + final Item otherItem = (Item) obj; + return true; + *//*return Objects.equals(true);*//* + }*/ + + +} + diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d66acb7ce1ccb0e5077608660438b9cec565783b --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java @@ -0,0 +1,26 @@ +package no.ntnu.idatt1002.demo.data; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ItemTest { + + @Test + @DisplayName("The Item constructor throws exceptions when it should.") + void constructorThrows(){ + 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)); + 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)); + } + +} \ No newline at end of file