type.lib
Class Item

java.lang.Object
  extended by type.lib.Item
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Item>
Direct Known Subclasses:
Fresh

public class Item
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<Item>

This class encapsulates a stock item. Each item is characterized by a unique number, name, and sale price (per unit). The class provides methods for processing purchase and sale transactions and keeps track of their overall quantity and amount.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca
See Also:
Serialized Form

Constructor Summary
Item(java.lang.String number, java.lang.String name, double price)
          Construct an inventory item with the given number, name, and sale price per unit.
 
Method Summary
 int compareTo(Item other)
          Compares this object with the specified object for order.
 boolean equals(java.lang.Object other)
          Test the equality of items.
 java.lang.String getName()
          Determine the name of this item.
 java.lang.String getNumber()
          Determine the number of this item.
 int getPurchasedQty()
          Determine the total number of units purchased from this item.
 double getPurchases()
          Determine the overall purchases amount of this item.
 double getSales()
          Determine the overall sales amount of this item.
 int getSoldQty()
          Determine the total number of units sold from this item.
 int getStock()
          Determine the stock quantity of this item.
 double getUnitCost()
          Determine the cost per unit of this item.
 double getUnitPrice()
          Determine the sale price per unit of this item.
 int hashCode()
          Compute a hash code for this Item.
 void purchase(int qty, double amount)
          Purchase the indicated number of units from this item for the indicated purchase amount.
 boolean sell(int qty)
          Sell the indicated number of units from this item at the posted sale price.
 boolean sell(int qty, double amount)
          Sell the indicated number of units from this item for the indicated sale amount.
 boolean setUnitPrice(double price)
          Change the posted sale price per unit of this item.
 java.lang.String toString()
          Construct a string representation of this item.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Item

public Item(java.lang.String number,
            java.lang.String name,
            double price)
Construct an inventory item with the given number, name, and sale price per unit. The cost price and opening stock quantity are initialized to zero.

Parameters:
number - the item number.
name - the name of the item.
price - the sale price per unit of the item.
Throws:
java.lang.RuntimeException - if the name or number is null or if the price is negative.
Method Detail

getNumber

public java.lang.String getNumber()
Determine the number of this item.

Returns:
the name of this item.

getName

public java.lang.String getName()
Determine the name of this item.

Returns:
the name of this item.

getStock

public int getStock()
Determine the stock quantity of this item.

Returns:
the stock quantity of this item.

getUnitCost

public double getUnitCost()
Determine the cost per unit of this item. This price is computed by dividing the total purchases amount by the total number of purchased units.

Returns:
the cost per unit of this item.

setUnitPrice

public boolean setUnitPrice(double price)
Change the posted sale price per unit of this item.

Parameters:
price - the new sale price per unit of the item.
Returns:
true if the price is changed and false otherwise. The price is changed only if the passed parameter is not negative.

getUnitPrice

public double getUnitPrice()
Determine the sale price per unit of this item.

Returns:
the posted sale price per unit of this item.

getSoldQty

public int getSoldQty()
Determine the total number of units sold from this item.

Returns:
the total number of units sold from this item.

getSales

public double getSales()
Determine the overall sales amount of this item.

Returns:
the overall sales amount of this item.

getPurchasedQty

public int getPurchasedQty()
Determine the total number of units purchased from this item.

Returns:
the total number of units purchased from this item.

getPurchases

public double getPurchases()
Determine the overall purchases amount of this item.

Returns:
the overall purchases amount of this item.

sell

public boolean sell(int qty)
Sell the indicated number of units from this item at the posted sale price. This convenience method has the same effect as invoking the 2-parameter version with getUnitPrice()*qty as amount. The sale request fails if there is not enough quantity in stock.

Parameters:
qty - the sold quantity.
Returns:
true if the sale was processed and false if it failed.

sell

public boolean sell(int qty,
                    double amount)
Sell the indicated number of units from this item for the indicated sale amount. The method allows you to specify the total sale amount so that discounts, shipping, and other charges can be incorporated. If the sale is ordinary, i.e. at the posted sale price, use the more convenient one-parameter version of this method. The sale request fails if there is not enough quantity in stock.

Parameters:
qty - the sold quantity.
amount - the sale amount for the entire quantity qty.
Returns:
true if the sale was processed and false if it failed.

purchase

public void purchase(int qty,
                     double amount)
Purchase the indicated number of units from this item for the indicated purchase amount.

Parameters:
qty - the purchased quantity.
amount - the purchase amount for the entire quantity qty.

toString

public java.lang.String toString()
Construct a string representation of this item.

Overrides:
toString in class java.lang.Object
Returns:
the string: "Item# xxx name", where xxx is the item number and name is its name.

equals

public boolean equals(java.lang.Object other)
Test the equality of items. An object is considered equal to this item if it is an Item object with the same number as this one.

Overrides:
equals in class java.lang.Object
Parameters:
other - a reference to the object to test equality with.
Returns:
true if other is not null and it points to an object that is equal (as defined above) to this object. The return is false otherwise.

hashCode

public int hashCode()
Compute a hash code for this Item.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.

compareTo

public int compareTo(Item other)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Specified by:
compareTo in interface java.lang.Comparable<Item>
Parameters:
other - the Object to compare to.
Returns:
the same return as that of compareTo(String) in String when invoked on the toString of this item.


Java by Abstraction: A Client-View Approach