Answered step by step
Verified Expert Solution
Link Copied!

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 5.
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 auto-increments 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 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);
}
}
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;
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

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions