public class Investment
extends java.lang.Object
implements java.io.Serializable
Stock
object, a quantity representing the number of shares
purchased from that stock, and the purchase price
(a.k.a the book value) of the stock.
Note that this class aggregates the stock via a regular aggregation, not a composition.
The class is mainly data (little behavior) and is meant to facilitate data storage and transfer by packaging the three investment attributes mentioned above in one object.
Constructor and Description |
---|
Investment(Investment investment)
Construct a (shallow) copy of the passed Investment.
|
Investment(Stock stock,
int quantity,
double bookValue)
Construct an investment having the passed fields.
|
Modifier and Type | Method and Description |
---|---|
Investment |
cloneMe()
Copy all attributes of
this object. |
boolean |
equals(java.lang.Object other)
Determine if
this investment is the
same as the passed parameter. |
double |
getBookValue()
Determine the book value of the stock held in
this investment. |
int |
getQty()
Determine the number of shares held in
this
investment. |
static Investment |
getRandom()
Create a random investment.
|
Stock |
getStock()
Determine the stock held in
this investment. |
int |
hashCode()
Compute a hash code for
this Investment. |
void |
setBookValue(double bookValue)
Mutate the book value of the stock held in
this investment. |
void |
setQty(int quantity)
Mutate the number of shares of
this investment. |
java.lang.String |
toString()
Return a string representation of
this investment. |
public Investment(Stock stock, int quantity, double bookValue)
stock
- a reference to the Stock
object.quantity
- the number of shares.bookValue
- the book value (purchase price of one share).public Investment(Investment investment)
investment
- the Investment object to copy.public Stock getStock()
this
investment.Stock
object in this
investment.public int getQty()
this
investment.this
investment.public void setQty(int quantity)
this
investment.quantity
- the number of shares.public double getBookValue()
this
investment.this
investment.public void setBookValue(double bookValue)
this
investment.bookValue
- the book value.public java.lang.String toString()
this
investment.toString
in class java.lang.Object
toString()
of the stock (or "null" if it is null
)
followed by a space, followed by the string "QTY=q BV=bv"
,
where q
and bv
are the quantity and
book value of the stock in this
investment.public boolean equals(java.lang.Object other)
this
investment is the
same as the passed parameter. The two are considered equal if the
stocks they hold are equal (as defined by the Stock's equals() method)
or both are null
and the two book values are equal to the nearest cent. Note that the
two quantities need not be equal (so applications can optionally
consolidate equal investments).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
Investment.hashCode
in class java.lang.Object
public Investment cloneMe()
this
object.
For pedagogical purposes, deep copying is performed
(in contrast with the copy constructor).Investment
object
having exactly the same attributes as this
one.public static Investment getRandom()
.LL
"
set simulated by the Stock
class.
This method was added for pedagogical reasons.