Skip to content
Snippets Groups Projects
Commit 69a30686 authored by Andreas Kluge Svendsrud's avatar Andreas Kluge Svendsrud
Browse files

Merge branch 'HS_Item-class' into 'master'

Hs item class

See merge request !1
parents 275dea65 0702c60c
No related branches found
No related tags found
1 merge request!1Hs item class
Pipeline #202364 passed
package no.ntnu.idatt1002.demo.data;
public enum Category {
FOOD,
CLOTHES,
OTHER,
}
package no.ntnu.idatt1002.demo.data;
/**
* The Item class represents an object or service purchased in real life. The item belongs to a category and
* has a price and description. The description may be left blank, but the Item must belong to a category and must
* have a price.
* @author HanneSofie
*
*/
public class Item {
private Category category;
private String description = "";
private float price;
/**
* 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
* IllegalArgumentException is thrown.
* @param category The category the Item belongs to.
* @param price The price of an Item as a float.
*/
public Item (Category category, 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;
}
}
/**
* 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.
*/
public Item (Category category, String description, float price){
this(category, price);
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.
*/
public String getDescription() {
return description;
}
/**
* The method sets the description of the Item object equal to the passed String. The String may be empty.
* @param description The new description of the Item object as a String.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* The method returns the price of the given Item object as a float.
* @return The price of the Item as a float.
*/
public float getPrice() {
return price;
}
/**
* 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.
*/
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);*//*
}*/
}
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
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