public class Item extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<Item>
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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. |
public Item(java.lang.String number, java.lang.String name, double price)
number
- the item number.name
- the name of the item.price
- the sale price per unit of the item.java.lang.RuntimeException
- if the name or number is null or if the price is negative.public java.lang.String getNumber()
this
item.this
item.public java.lang.String getName()
this
item.this
item.public int getStock()
this
item.this
item.public double getUnitCost()
this
item. This price is
computed by dividing the total purchases amount by the total number of
purchased units.this
item.public boolean setUnitPrice(double price)
this
item.price
- the new sale price per unit of the item.true
if the price is changed and false
otherwise. The price is changed only if the passed parameter is
not negative.public double getUnitPrice()
this
item.this
item.public int getSoldQty()
this
item.this
item.public double getSales()
this
item.this
item.public int getPurchasedQty()
this
item.this
item.public double getPurchases()
this
item.this
item.public boolean sell(int qty)
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.qty
- the sold quantity.true
if the sale was processed and
false
if it failed.public boolean sell(int qty, double amount)
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.qty
- the sold quantity.amount
- the sale amount for the entire quantity qty
.true
if the sale was processed and
false
if it failed.public void purchase(int qty, double amount)
this
item for
the indicated purchase amount.qty
- the purchased quantity.amount
- the purchase amount for the entire quantity qty
.public java.lang.String toString()
this
item.toString
in class java.lang.Object
Item# xxx name
", where xxx
is the item number and name
is its name.public boolean equals(java.lang.Object other)
this
item if it is an Item
object with the same
number as this
one.equals
in class java.lang.Object
other
- a reference to the object to test equality with.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.public int hashCode()
this
Item.hashCode
in class java.lang.Object
public int compareTo(Item other)
compareTo
in interface java.lang.Comparable<Item>
other
- the Object to compare to.compareTo(String)
in
String
when invoked on the toString
of
this item.