package test3; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Objects; /** * A bill that includes an itemized list of purchased items, the date that the * bill was created, and a total price of all of the items in the bill. The * itemized bill and its list form a composition. The itemized bill and its date * form a composition. The total price of the bill is guaranteed to be equal to * the sum of the prices of the items in the bill. * * @author EECS2030 Winter 2016-17 * */ public class ItemizedBill { private int total; private Date date; private List items; /** * Initialize this bill so that its total is zero, its list of items is * empty, and its date is the current date. */ public ItemizedBill() { this(new ArrayList()); } /** * Initialize this bill from a list of purchased items. The total price of * the bill is computed as the sum of the prices of the purchased items. The * date of issue is the current date. The list of items is deep copied. * * @param items * a list of purchased items */ public ItemizedBill(List items) { this.items = new ArrayList(); this.total = 0; for (Item item : items) { this.items.add(new Item(item)); this.total += item.getPrice(); } this.date = new Date(); } /** * Initialize this bill by copying another bill. The total price of the * bill, the date of issue, and the list of items purchased are all copied * from the other bill. The list of items is a deep copy of the list of * items of the other bill. * * @param other * the bill to copy */ public ItemizedBill(ItemizedBill other) { this.total = other.total; this.date = new Date(other.date.getTime()); this.items = new ArrayList<>(); for (Item item : items) { this.items.add(new Item(item)); } } /** * Returns the list of items purchased for this bill. The returned list * cannot be used to modify this bill, and the items in the returned list * cannot be used to modify the items belonging to this bill. * * @return the list of items purchased for this bill */ public List getItems() { List copy = new ArrayList<>(); for (Item item : this.items) { copy.add(new Item(item)); } return copy; } /** * Add an item to the list of items purchased for this bill. The total price * of the bill is updated to include the price of the added item. * * @param item * add an item to the list of items purchased for this bill */ public void addItem(Item item) { this.setTotal(this.getTotal() + item.getPrice()); this.items.add(item); } /** * Returns a sorted list of items purchased for this bill. The list is a * deep copy of the list belonging to this bill. Items are sorted as * specified in Item.compareTo. * * @return the sorted list of items purchased for this bill */ public List getSortedItems() { List it = this.getItems(); Collections.sort(it); return it; } /** * Returns the total price of the bill. * * @return the total price of the bill */ public final int getTotal() { return this.total; } /** * Set the total price of the bill. * * @param total * the total price of the bill * @throws IllegalArgumentException * if the total is less than zero */ public final void setTotal(int total) { if (total < 0) { throw new IllegalArgumentException("Bill total cannot be negative."); } this.total = total; } /** * Returns a copy of the date for when this bill was issued. * * @return a copy of the date for when this bill was issued */ public final Date getDate() { return new Date(this.date.getTime()); } /** * Returns a hash code for this bill. * * @return a hash code for this bill * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return Objects.hash(this.total, this.date, this.items); } /** * Compares two itemized bills for equality. The result is true if and only * if the argument is not null and is an ItemizedBill object that has: * *
    *
  • the same total price as this itemized bill *
  • the same date of issue as this itemized bill *
  • the same list of items purchased ignoring the order in which items * appear in the list *
* *

* equals does not modify the list of items in this bill. * * @param obj * the object to compare this item against * @return true if the given object represents an ItemizedBill equivalent to * this bill, false otherwise */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (this.getClass() != obj.getClass()) { return false; } ItemizedBill other = (ItemizedBill) obj; if (this.total != other.total) { return false; } if (!this.date.equals(other.date)) { return false; } return this.getSortedItems().equals(other.getSortedItems()); } }