Cart Class
import java.math.BigDecimal; import java.util.*;
public class Cart implements Comparable, Comparator{
private Map- cartOfOrders; private boolean membership; private BigDecimal totalWithMembership; private BigDecimal totalWithoutMembership; public Cart() { this.cartOfOrders = new HashMap(); totalWithMembership = BigDecimal.valueOf(0); totalWithoutMembership = BigDecimal.valueOf(0); } public void add(final ItemOrder theOrder) { ItemOrder temp = this.cartOfOrders.get(theOrder.getItem()); if(temp != null) { this.cartOfOrders.remove(theOrder.getItem()); this.totalWithMembership = totalWithMembership.subtract(temp.orderPriceMembership); this.totalWithoutMembership = totalWithoutMembership.subtract(temp.orderPriceNative); } if(theOrder.getQuantity()
return this.membership ? totalWithMembership.setScale(2, BigDecimal.ROUND_HALF_EVEN) : totalWithoutMembership.setScale(2, BigDecimal.ROUND_HALF_EVEN) ; } public void clear() { cartOfOrders.clear(); totalWithMembership = BigDecimal.valueOf(0); totalWithoutMembership = BigDecimal.valueOf(0); } public int getCartSize() { return cartOfOrders.size(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); for(Map.Entry- entry : cartOfOrders.entrySet()) { sb.append(entry.getValue().toString()); sb.append(" "); } if(sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } @Override public int hashCode() { int h = 0; for(Map.Entry
- entry : cartOfOrders.entrySet()) { h = h * 11 + entry.getKey().hashCode(); h %= 10000007; } return h; } @Override public boolean equals(final Object another) { if(another instanceof Cart) { Cart an = (Cart)another; if(this.cartOfOrders.size() == an.cartOfOrders.size()) { for(Map.Entry
- entry: an.cartOfOrders.entrySet()) { ItemOrder target = this.cartOfOrders.get(entry.getKey()); if(target == null || !target.equals(entry.getValue()) ) { return false; } } return true; } } return false; } @Override public int compare(Cart o1, Cart o2) { return Integer.compare(o1.hashCode(), o2.hashCode()); } @Override public int compareTo(Cart o) { return compare(this, o); }
}
Item Class
import java.math.BigDecimal; import java.text.NumberFormat; import java.util.*;public final class Item implements Comparable- , Comparator
- { private String name; private BigDecimal price; private int bulkQuantity; private BigDecimal bulkPrice; private boolean bulk;
private static NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
public Item(final String theName, final BigDecimal thePrice) { if(!thePrice.abs().equals(thePrice) || theName.length() == 0) { throw new IllegalArgumentException("price can't be negative and name can't be empty"); } this.name = theName; this.price = thePrice; } public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity, final BigDecimal theBulkPrice) { this(theName, thePrice); if(theBulkQuantity
} public BigDecimal getPrice() { return price; } public int getBulkQuantity() { return bulkQuantity; } public BigDecimal getBulkPrice() { return bulkPrice; } public boolean isBulk() { return this.bulk; } @Override public String toString() { String ret = this.name + " " + nf.format(this.price); if(bulk) { ret += "( " + this.bulkQuantity + " for " + nf.format(this.bulkPrice) + " )"; } return ret; } @Override public boolean equals(final Object theOther) { if(theOther instanceof Item) { return ((Item)theOther).toString().equals(this.toString()); } return false; } @Override public int hashCode() { return this.name.length() % 100007; } @Override public int compareTo(Item o) { return this.toString().compareTo(o.toString()); } @Override public int compare(Item o1, Item o2) { return Integer.compare(o1.hashCode(), o2.hashCode()); }
}
ItemOrder Class
import java.math.BigDecimal; import java.util.*;
public final class ItemOrder implements Comparable { private Item item; private int quantity; protected BigDecimal orderPriceNative; protected BigDecimal orderPriceMembership; public ItemOrder(final Item theItem, final int theQuantity) { this.item = theItem; this.quantity = theQuantity; this.orderPriceNative = this.item.getPrice().multiply(BigDecimal.valueOf((double) this.quantity)); if(this.item.isBulk()) { int bulkSet = this.quantity / this.item.getBulkQuantity(); int remain = this.quantity % this.item.getBulkQuantity(); this.orderPriceMembership = this.item.getBulkPrice().multiply(BigDecimal.valueOf((double) bulkSet)) .add(this.item.getPrice().multiply(BigDecimal.valueOf((double) remain))); }else { this.orderPriceMembership = orderPriceNative; } } public Item getItem() { return item; } public int getQuantity() { return quantity; } @Override public String toString() { return item.toString() + " with a quantity of " + this.quantity; } @Override public int hashCode() { return this.item.hashCode(); } @Override public boolean equals(final Object theOther) { if(theOther instanceof ItemOrder) { ItemOrder t = (ItemOrder)theOther; return Objects.equals(t.getItem(), this.getItem()) && Objects.deepEquals(t.getQuantity(), this.getQuantity()); } return false; } @Override public int compareTo(ItemOrder o) { return Integer.compare(this.hashCode(), o.hashCode()); }
}
JUnit 5 Tests for New Bookstore Application Review the posted course material related to JUnit 5 and implement the specified unit tests for the New Bookstore Application. Make sure that you get 100% coverage for all three units (i.e., Java classes): a. Write test cases (i.e., JUnit 5 test methods) for the equals(), hashCodel) , compare To(), and compare() methods of the Item class. b. Write test cases (i.e., JUnit 5 test methods) for the equals(), hashCode(), and compareTo() method of the ItemOrder class c. Write test cases (i.e., JUnit 5 test methods) for the equals() and hashCode() methods of the Cart class. JUnit 5 Tests for New Bookstore Application Review the posted course material related to JUnit 5 and implement the specified unit tests for the New Bookstore Application. Make sure that you get 100% coverage for all three units (i.e., Java classes): a. Write test cases (i.e., JUnit 5 test methods) for the equals(), hashCodel) , compare To(), and compare() methods of the Item class. b. Write test cases (i.e., JUnit 5 test methods) for the equals(), hashCode(), and compareTo() method of the ItemOrder class c. Write test cases (i.e., JUnit 5 test methods) for the equals() and hashCode() methods of the Cart class