type.lib
Class Portfolio

java.lang.Object
  extended by type.lib.Portfolio
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<Investment>

public class Portfolio
extends java.lang.Object
implements java.io.Serializable, java.lang.Iterable<Investment>

This class encapsulates a portfolio of investments. It can be thought of as a set of records each representing one investment. You can access the contents of the portfolio using indexed traversal or an iterator.

The collection encapsulated by this class is an example of static allocation; i.e. its capacity is set at construction time and cannot be changed afterwards.

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

Field Summary
static int DEFAULT_SIZE
          The number of investments that this portfolio can at most hold by default.
 
Constructor Summary
Portfolio()
          Construct an empty, no-title portfolio (title = zero-length string) capable of holding DEFAULT_SIZE investments.
Portfolio(java.lang.String title, int capacity)
          Construct an empty portfolio having the passed title and capable of holding the specified number of investments.
 
Method Summary
 boolean add(Investment inv)
          Attempt to add the passed investment to this portfolio.
 Investment get(int index)
          Determine the investment in this portfolio with the given index.
 int getCapacity()
          Determine the capacity of this portfolio.
 Investment getFirst()
          Deprecated. As of version 4.0, replaced with iterator() Determine the first investment in this portfolio. This method should be invoked first whenever you intend to traverse the contents of the portfolio. If a non-null was returned, repeated invocations of getNext() will return the remaining investments in this portfolio until a null is returned.
 java.lang.String getName()
          Determine the name of this portfolio.
 Investment getNext()
          Deprecated. As of version 4.0, replaced with iterator() Determine the next investment in this portfolio. The method should be invoked only if getFirst() has already been invoked and a non-null was returned.
static Portfolio getRandom()
          Create a randomly chosen portfolio.
static Portfolio getRandom(boolean same)
          Create a randomly chosen portfolio.
 java.util.Iterator<Investment> iterator()
          Determine an Iterator over the Investment objects in this portfolio.
 Investment remove(int index)
          Remove the investment with the passed index from this portfolio.
 int size()
          Determine the number of investments added to this portfolio.
 java.lang.String toString()
          Make a string representation of this Portfolio.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
The number of investments that this portfolio can at most hold by default. Used only if the default constructor is employed.

See Also:
Constant Field Values
Constructor Detail

Portfolio

public Portfolio(java.lang.String title,
                 int capacity)
Construct an empty portfolio having the passed title and capable of holding the specified number of investments.

Parameters:
title - the title of this portfolio
capacity - the maximum number of investments that this portfolio can hold.
Throws:
java.lang.RuntimeException - if capacity is not positive.

Portfolio

public Portfolio()
Construct an empty, no-title portfolio (title = zero-length string) capable of holding DEFAULT_SIZE investments.

Method Detail

getName

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

Returns:
the name of this portfolio.

getCapacity

public int getCapacity()
Determine the capacity of this portfolio.

Returns:
the capacity of this portfolio.

size

public int size()
Determine the number of investments added to this portfolio.

Returns:
the number of investments added to this portfolio.

get

public Investment get(int index)
Determine the investment in this portfolio with the given index.

Parameters:
index - the number of the investment to get. The index starts at zero and increments as investments are added.
Returns:
the investment whose index is passed.
Throws:
java.lang.RuntimeException - if the index is less than zero or not less than size()

iterator

public java.util.Iterator<Investment> iterator()
Determine an Iterator over the Investment objects in this portfolio.

Specified by:
iterator in interface java.lang.Iterable<Investment>
Returns:
an Iterator over the courses.

getFirst

@Deprecated
public Investment getFirst()
Deprecated. As of version 4.0, replaced with iterator() Determine the first investment in this portfolio. This method should be invoked first whenever you intend to traverse the contents of the portfolio. If a non-null was returned, repeated invocations of getNext() will return the remaining investments in this portfolio until a null is returned.

Returns:
the first investment in this portfolio, or null if this portfolio is empty.

getNext

@Deprecated
public Investment getNext()
Deprecated. As of version 4.0, replaced with iterator() Determine the next investment in this portfolio. The method should be invoked only if getFirst() has already been invoked and a non-null was returned.

Returns:
the investment that follows the one returned by the last invocation of this method, or null if there are no more investments in this portfolio.

add

public boolean add(Investment inv)
Attempt to add the passed investment to this portfolio.

Parameters:
inv - a reference to the investment to add.
Returns:
true if the investment was added or false if it could not be added because the portfolio is full.

remove

public Investment remove(int index)
Remove the investment with the passed index from this portfolio.

Parameters:
index - the number of the investment to remove. The index is zero-based.
Returns:
the removed investment
Throws:
java.lang.RuntimeException - if the index is less than zero or not less than size()

toString

public java.lang.String toString()
Make a string representation of this Portfolio.

Overrides:
toString in class java.lang.Object
Returns:
the Portfolio's title, followed by a colon, followed by a space, followed by the number of investments it contains.

getRandom

public static Portfolio getRandom()
Create a randomly chosen portfolio. This is a convenience method whose invocation is equivalent to invoking getRandom(false).


getRandom

public static Portfolio getRandom(boolean same)
Create a randomly chosen portfolio. The portfolio has the title "The Random Portfolio " followed by four random letters. It contains anywhere between 2 and 20 investments with the count uniformly distributed between 2 and 20. The investments themselves are chosen randomly using the Investment.getRandom() mehtod of the Investment class. Note that there is no guarantee that the investments are distinct.
This method was added for pedagogical reasons.

Parameters:
same - a flag that determines if a new portfolio is to be generated (when false) or if a reference to the last-generated portfolio should be returned (true). You should normally set it to false to get a different portfolio per invocation.
Returns:
a randomly chosen portfolio.


Java by Abstraction: A Client-View Approach