Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write tests for the followingYour tests must be written using JUnit 5 . Order Class - Adding a product with a negative quantity to the
Write tests for the followingYour tests must be written using JUnit
Order Class
Adding a product with a negative quantity to the order
Removing a product with a negative quantity from the order
Adding a new product to the order not already in the order
Removing a product that is not in the order
equals
Adding a product with a negative quantity to the order
Removing a product with a negative quantity from the order
Adding a new product to the order not already in the order
Removing a product that is not in the order
equals
Checking that the order number autoincrements with each new order object created
Adding a product that is already in the order
Removing a product that is in the order quantity less than the quantity in the order
Removing a product that is in the order quantity that is greater than or equal to the quantity in the order
toString
public class Order
private static int nextNumber;
private int number;
private Customer customer;
private Map lines;
Represents an order with an ordernumber autogenerated customer, and order lines with products and quantities.
@param customer
public OrderCustomer customer
this.number nextNumber;
this.customer customer;
this.lines new HashMap;
Adds a quantity of a product to the order. If the product is already in the order, increase the order quantity by the quantity.
@param product
@param quantity must be nonnegative
@throws IllegalArgumentException if the quantity is negative
public void addToOrderProduct product, int quantity
if quantity
throw new IllegalArgumentExceptionQuantity must be nonnegative.";
if linescontainsKeyproduct
lines.putproduct lines.getproduct quantity;
else
lines.putproduct quantity;
public boolean removeFromOrderProduct product, int quantity
if quantity
throw new IllegalArgumentExceptionQuantity must be nonnegative.";
if linescontainsKeyproduct
if linesgetproduct quantity
lines.removeproduct;
else
lines.putproduct lines.getproduct quantity;
return true;
else
return false;
Determine if a product is in the order
@param product to check for
@return whether the product is in the order
public boolean containsProduct product
return lines.containsKeyproduct;
Determine the quantity of a product in the order
@param product to check
@return quantity of the product in the order
public int getItemQuantityProduct product
if containsproduct
return lines.getproduct;
else
return ;
Returns the total cost of the order unit prices quantity
@return the total cost of the order unit prices quantity
public double getOrderTotal
double total ;
for MapEntry entry : lines.entrySet
Product key entry.getKey;
Integer value entry.getValue;
total key.getUnitPrice value;
return total;
Returns the number of order lines.
@return the number of order lines
public int getOrderLineCount
return lines.size;
Returns the number of items in the order sum of quantity of all lines
@returnthe number of items in the order sum of quantity of all lines
public int getOrderItemCount
int total ;
for MapEntry entry : lines.entrySet
total entry.getValue;
return total;
@Override
public String toString
return "Order number number customer customer lines lines ;
Orders are equal only if their order numbers are equal
@see java.lang.Object#equalsjavalang.Object
@Override
public boolean equalsObject obj
if this obj
return true;
if obj null
return false;
if getClass obj.getClass
return false;
OrderTest other OrderTest obj;
if number other.number
return false;
return true;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started