From 9f32ea3662b81734cf6ce441951f8e9de74c2258 Mon Sep 17 00:00:00 2001 From: HSoreide <sofie.scisly@gmail.com> Date: Thu, 2 Mar 2023 12:18:49 +0100 Subject: [PATCH] Implement Item class with unit tests and Category enum --- .../no/ntnu/idatt1002/demo/data/Category.java | 9 +++ .../no/ntnu/idatt1002/demo/data/Item.java | 67 +++++++++++++++++++ .../no/ntnu/idatt1002/demo/data/ItemTest.java | 26 +++++++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Category.java create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Item.java create mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/ItemTest.java 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 00000000..dfb85f4b --- /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 00000000..d56f1299 --- /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 00000000..d66acb7c --- /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 -- GitLab