Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Product Class Your tests must be written using JUnit 5 . Constructing a Product with a non - negative price Getting and setting non -

Product Class
Your tests must be written using JUnit 5.
Constructing a Product with a non-negative price
Getting and setting non-negative prices
Getting and setting sku
Getting and setting description
public class Product {
private String sku;
private double unitPrice;
private String description;
/**
* Represents a Product with a SKU number, unit price, and description.
*
* @param sku
* @param unitPrice must be non-negative
* @param description
*/
public Product(String sku, double unitPrice, String description){
if (unitPrice <0){
throw new IllegalArgumentException("Unit price must be non-negative.");
}
this.sku = sku;
this.unitPrice = unitPrice;
this.description = description;
}
/**
* Returns the unit price
* @return unit price
*/
public double getUnitPrice(){
return unitPrice;
}
/**
* Sets the unit price
* @param unitPrice must be non-negative
*/
public void setUnitPrice(double unitPrice){
if (unitPrice <0){
throw new IllegalArgumentException("Unit price must be non-negative.");
}
this.unitPrice = unitPrice;
}
/**
* Returns the description
* @return the description
*/
public String getDescription(){
return description;
}
/**
* Sets the description
* @param description
*/
public void setDescription(String description){
this.description = description;
}
/**
* Returns the SKU number
* @return the SKU number
*/
public String getSku(){
return sku;
}
@Override
public String toString(){
return "Product [sku="+ sku +", unitPrice="+ unitPrice +", description="+ description +"]";
}
/**
* Products are equal *only* if their sku numbers are equal
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj){
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass()!= obj.getClass())
return false;
Product other =(Product) obj;
if (sku == null){
if (other.sku != null)
return false;
} else if (!sku.equals(other.sku))
return false;
return true;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Get Funded The Startup Entrepreneurs Guide To Seriously Successful Fundraising

Authors: John Biggs, Eric Villines

1st Edition

1260459063, 978-1260459067

Students also viewed these Databases questions