Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write tests for the following Your tests must be written using JUnit 5 . Order Class o Constructing an Order o Adding a product with

Write tests for the following
Your tests must be written using JUnit 5.
Order Class
o Constructing an Order
o Adding a product with non-negative quantity to the order
o Removing a product with a non-negative 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 Order(Customer 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 non-negative
* @throws IllegalArgumentException if the quantity is negative
*/
public void addToOrder(Product product, int quantity){
if (quantity <0){
throw new IllegalArgumentException("Quantity must be non-negative.");
}
if (lines.containsKey(product)){
lines.put(product, lines.get(product)+ quantity);
} else {
lines.put(product, 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 non-negative
* @return true if the product was in the order, false otherwise
*/
public boolean removeFromOrder(Product product, int quantity){
if (quantity <0){
throw new IllegalArgumentException("Quantity must be non-negative.");
}
if (lines.containsKey(product)){
if (lines.get(product)<= quantity){
lines.remove(product);
} else {
lines.put(product, lines.get(product)- 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 contains(Product product){
return lines.containsKey(product);
}
/**
* Determine the quantity of a product in the order
* @param product to check
* @return quantity of the product in the order
*/
public int getItemQuantity(Product product){
if (contains(product)){
return lines.get(product);
} else {
return 0;
}
}
/**
* 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 =0;
for (Map.Entry 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 =0;
for (Map.Entry 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#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;
Order other =(Order) obj;
if (number != other.number)
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

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago