In: Computer Science
You must write tests for the following:
all tests must be written in JUnit 5.
public class Customer {
private static int nextNumber = 1;
private int number;
private String firstName;
private String lastName;
/**
* Represents a customer with customer number (autogenerated), first and last name.
* @param firstName
* @param lastName
*/
public Customer(String firstName, String lastName) {
this.number = nextNumber++;
this.firstName = firstName;
this.lastName = lastName;
}
/**
* Returns the first name
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* Returns the last name
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* Sets the first name
* @param firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Sets the last name
* @param lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Returns the customer number
* @return the customer number
*/
public int getNumber() {
return number;
}
@Override
public String toString() {
return "Customer [number=" + number + ", name=" + firstName + " " + lastName + "]";
}
/**
* Customers are equal *only* if their customer 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;
Customer other = (Customer) obj;
if (number != other.number)
return false;
return true;
}
}
import java.util.HashMap;
import java.util.Map;
/**
* Represents an order with an ordernumber (autogenerated), customer, and order lines with products and quantities.
*
* @author kwurst
* @version Fall 2018
*/
public class Order {
private static int nextNumber;
private int number;
private Customer customer;
private Map<Product, Integer> 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<Product, Integer>();
}
/**
* 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<Product,Integer> 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<Product,Integer> 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;
}
}
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;
}
Hi, Please find the solution and rate the answer. Please note that Junit 5 has not yet released. Latest version is 4.9. I have used 4.9 for this program.
import org.junit.Test;
import java.util.Random;
public class TEst {
String getRandomString(int size){
Random random = new Random();
int rSize = Math.abs(random.nextInt() % size + 1);
String sample = "abcdefghijklmnopqrstuvwxyz0123456789";
String retStr="";
for (int i = 0; i < rSize; i++) {
retStr += Character.toString(sample.charAt(Math.abs(random.nextInt()) % 36));
}
return retStr;
}
@Test
public void testCustomerNoAutoInc(){
Customer c = new Customer(getRandomString(5),getRandomString(5));
Customer c1 = new Customer(getRandomString(5),getRandomString(5));
assert c1.getNumber()==c.getNumber()+1:"Number not auto incrementing";//Text after colon will print if assert fails
}
@Test
public void testOrderIncrement(){
Customer c = new Customer(getRandomString(5),getRandomString(5));
Customer c1 = new Customer(getRandomString(5),getRandomString(5));
Order o = new Order(c);
Order o1 = new Order(c1);
//getNumber method is not present in Order class. PLs do required.
// assert o.getNumber()+1 = o1.getNumber():"Order number not incrementing";
}
@Test
public void testAddAlreadyInOrder(){
Customer c = new Customer(getRandomString(5),getRandomString(5));
Order o = new Order(c);
Product p = new Product("SKU23423", 2342.22, "Washing machine by Apollo");
o.addToOrder(p,1);
o.addToOrder(p,1);
assert o.getItemQuantity(p) == 2 : "Order not being updated";
}
@Test
public void testRemoveLessThanAlreadyInOrder(){
Customer c = new Customer(getRandomString(5),getRandomString(5));
Order o = new Order(c);
Product p = new Product("SKU23423", 2342.22, "Washing machine by Apollo");
o.addToOrder(p,2);
o.removeFromOrder(p,1);
assert o.getItemQuantity(p) == 1 : "Order was not removed";
}
@Test
public void testRemoveMoreThanAlreadyInOrder(){
Customer c = new Customer(getRandomString(5),getRandomString(5));
Order o = new Order(c);
Product p = new Product("SKU23423", 2342.22, "Washing machine by Apollo");
o.addToOrder(p,2);
o.removeFromOrder(p,3);
assert o.getOrderItemCount() == 0 : "Order was not removed";
}
}


sAll
tests are passing. Below is project structure as a maven
project.

