/* * PART 1 - Create an Ontario Item * * An OntarioItem object cannot be modified. The total price is * computed based on the quantity, unit price, and tax rate which * are passed into the constructor. */ public class OntarioItem implements Item { final static float ONTARIO_TAX = (float) 0.15; /* The data members are final below to re-inforce the notion that * the values cannot change once they are initialized in the * constructor */ final private int m_quantity; final private String m_id; // Item id final private String m_name; final private float m_unitPrice; final private float m_totalPrice; /* Decimal constants in java are double. It is nice to have * a constructor which will accept constants without needing a * cast. */ public OntarioItem(int quantity, String id, String name, double unitPrice) { this(quantity, id, name, (float) unitPrice); } public OntarioItem(int quantity, String id, String name, float unitPrice) { Util.assert(quantity >= 0, "Quantity must be non-negative"); Util.assert(id != null, "Id is null"); Util.assert(name != null, "Name is null"); Util.assert(unitPrice >= 0, "Unit Price must be non-negative"); Util.assert(ONTARIO_TAX >= 0, "Tax rate must be >= 0"); m_quantity = quantity; m_id = id; m_name = name; m_unitPrice = unitPrice; // round to two decimals! m_totalPrice = ((int) (100 * m_unitPrice * m_quantity * (1 + ONTARIO_TAX)))/100.0F; } public int getQuantity() { return m_quantity; } public String getItemId() { return m_id; } public String getItemName() { return m_name; } public float getUnitPrice() { return m_unitPrice; } public float getTotalPrice() { return m_totalPrice; } /******************************* * Inherited Methods (from Object) * equals is necessary if we wish to put an Item * into a Hashtable. see part 3 of assignment. *******************************/ public String toString() { return "OntarioItem: " + getItemName() + " - Units: " + getQuantity() + " - Total Price: " + getTotalPrice(); } /** * Two items are equal if: * they are instances of the same class (OntarioItem) AND * their ID's are the same AND * the items have the same name AND * the items have the same unit price */ public boolean equals(Object o) { if (o == null || !(o instanceof OntarioItem)) { return false; } OntarioItem other = (OntarioItem) o; return other.m_id.equals(m_id) && other.m_name.equals(m_name) && other.m_unitPrice == m_unitPrice; } }