type.lib
Class Investment

java.lang.Object
  extended by type.lib.Investment
All Implemented Interfaces:
java.io.Serializable

public class Investment
extends java.lang.Object
implements java.io.Serializable

This class encapsulates a stock investment. Each instance of this class holds a 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.

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

Constructor Summary
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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Investment

public Investment(Stock stock,
                  int quantity,
                  double bookValue)
Construct an investment having the passed fields.

Parameters:
stock - a reference to the Stock object.
quantity - the number of shares.
bookValue - the book value (purchase price of one share).

Investment

public Investment(Investment investment)
Construct a (shallow) copy of the passed Investment.

Parameters:
investment - the Investment object to copy.
Method Detail

getStock

public Stock getStock()
Determine the stock held in this investment.

Returns:
a reference to the Stock object in this investment.

getQty

public int getQty()
Determine the number of shares held in this investment.

Returns:
the number of shares of the stock in this investment.

setQty

public void setQty(int quantity)
Mutate the number of shares of this investment.

Parameters:
quantity - the number of shares.

getBookValue

public double getBookValue()
Determine the book value of the stock held in this investment.

Returns:
the book value of the stock in this investment.

setBookValue

public void setBookValue(double bookValue)
Mutate the book value of the stock held in this investment.

Parameters:
bookValue - the book value.

toString

public java.lang.String toString()
Return a string representation of this investment.

Overrides:
toString in class java.lang.Object
Returns:
the 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.

equals

public boolean equals(java.lang.Object other)
Determine if 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).

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 Investment.

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

cloneMe

public Investment cloneMe()
Copy all attributes of this object. For pedagogical purposes, deep copying is performed (in contrast with the copy constructor).

Returns:
a reference to a new Investment object having exactly the same attributes as this one.

getRandom

public static Investment getRandom()
Create a random investment. The created investment contains a randomly chosen stock with a quantity uniformly distributed between between 1 and 5000 and a bookvalue normally distributed around the actual price with 0.02 standard deviation. The stock is chosen randomly from the ".LL" set simulated by the Stock class. This method was added for pedagogical reasons.

Returns:
a randomly chosen investment.


Java by Abstraction: A Client-View Approach