Question: Write tests for the following Order Class o Constructing an Order o Adding a product with non - negative quantity to the order o Removing
Write tests for the following
Order Class
o Constructing an Order
o Adding a product with nonnegative quantity to the order
o Removing a product with a nonnegative quantity from the order
o Checking if a product is in the order
o Checking the quantity of a product in the order
o Getting the total price of the order
o Getting the number of unique products in the order
o Getting the total number of items in the order
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;
Remove a quantity of a product from the order. If the product is already in the order, decrease the order quantity by the quantity.
If the quantity is reduced to zero, remove the product from the order.
If the product is in the order, return true. If the product is not in the order, return false.
@param product
@param quantity must be nonnegative
@return true if the product was in the order, false otherwise
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;
Order other Order obj;
if number other.number
return false;
return true;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
