public class Portfolio extends java.lang.Object implements java.io.Serializable, java.lang.Iterable<Investment>
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.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_SIZE
The number of investments that
this portfolio can at most
hold by default. |
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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. |
public static final int DEFAULT_SIZE
this
portfolio can at most
hold by default. Used only if the default constructor is employed.public Portfolio(java.lang.String title, int capacity)
title
- the title of this
portfoliocapacity
- the maximum number of investments that this
portfolio can hold.java.lang.RuntimeException
- if capacity
is not positive.public Portfolio()
DEFAULT_SIZE
investments.public java.lang.String getName()
this
portfolio.this
portfolio.public int getCapacity()
this
portfolio.this
portfolio.public int size()
this
portfolio.this
portfolio.public Investment get(int index)
this
portfolio with the given
index.index
- the number of the investment to get. The index starts at zero
and increments as investments are added.java.lang.RuntimeException
- if the index is less than zero or not less than
size()
public java.util.Iterator<Investment> iterator()
this
portfolio.iterator
in interface java.lang.Iterable<Investment>
@Deprecated public Investment getFirst()
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.this
portfolio, or
null
if this portfolio is empty.@Deprecated public Investment getNext()
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.null
if there are no
more investments in this
portfolio.public boolean add(Investment inv)
this
portfolio.inv
- a reference to the investment to add.true
if the investment was added or
false
if it could not be added because the portfolio
is full.public Investment remove(int index)
this
portfolio.index
- the number of the investment to remove. The index is
zero-based.java.lang.RuntimeException
- if the index is less than zero or not less than
size()
public java.lang.String toString()
this
Portfolio.toString
in class java.lang.Object
public static Portfolio getRandom()
getRandom(false)
.public static Portfolio getRandom(boolean same)
Investment.getRandom()
mehtod of the Investment
class.
Note that there is no guarantee that the investments are distinct.
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.