public class Stock
extends java.lang.Object
implements java.io.Serializable
toString
and
equals
), the class provides accessors for all,
and mutators for some, of its attributes. The stock price is
updated automatically when the stock is first created or when
the symbol is mutated; and it can also be updated manually via
the refresh()
method.
The class simulates two kinds of stock exchanges:
HR.L
, where L
is
any letter in the alphabet. The class assigns fixed names and prices
to these stocks, which makes this exchange suitable for conducting
reproducible tests.
.LL
(a dot followed by two letters).
The class assigns fixed names to these stocks and prices that
are functions of real time.
Modifier and Type | Field and Description |
---|---|
char |
delimiter
This field determines what character is inserted
between the stock symbol and its name in the return
of the
toString() method (assuming
the symbol exists). |
java.lang.String |
name
The name of
this stock as listed
on the exchange. |
static boolean |
titleCaseName
This field controls the format of the stock name
as returned by the
getName()
method. |
Constructor and Description |
---|
Stock()
Construct a default Stock having a
null symbol. |
Stock(Stock stock)
Construct a copy of the passed Stock.
|
Stock(java.lang.String symbol)
Construct a Stock having the (capitalized) passed symbol.
|
Modifier and Type | Method and Description |
---|---|
Stock |
cloneMe()
Copy the state of
this object. |
boolean |
equals(java.lang.Object other)
Test the equality of stock objects.
|
char |
getDelimiter()
Determine the delimiter of
this
Stock. |
java.lang.String |
getName()
Determine the name of
this
Stock as listed on the exchange. |
double |
getPrice()
Determine the price of
this
Stock. |
java.lang.String |
getSymbol()
Determine the symbol of
this
Stock. |
int |
hashCode()
Compute a hash code for
this Stock. |
void |
refresh()
Connect to the exchange and
obtain the name and price of
this
stock and update its attributes accordingly. |
boolean |
setDelimiter(char myDelimiter)
Mutator to change the delimiter of
this
stock to the passed one. |
void |
setSymbol(java.lang.String symbol)
Mutator to change the symbol of
this
stock to the (capitalized) passed symbol. |
java.lang.String |
toString()
Construct a string representation of
this Stock. |
public java.lang.String name
this
stock as listed
on the exchange. If no such stock exists,
the name
is set to null
.
(Bad design --this field should not be made public.)
public char delimiter
toString()
method (assuming
the symbol exists). The default is space.
(Bad design --should have been kept private. Access would have still been possible via the existing public accessor and mutator.)
public static boolean titleCaseName
getName()
method. If set to false
(default),
the name is returned exactly as listed on the exchange.
Otherwise, it is returned in title case (1st letter
of each word capitalized).public Stock()
null
symbol.
This convenience constructor has the same effect as passing
null
to the 1-parameter, overloaded constructor.public Stock(java.lang.String symbol)
refresh()
method.symbol
- the (ticker) symbol of the stock to construct.public Stock(Stock stock)
stock
- the Stock to copy.java.lang.RuntimeException
- if stock = null
public java.lang.String getName()
this
Stock as listed on the exchange. The letter
case is
as indicated in the titleCaseName
field.this
Stock. If no such stock exists, null
is returned.public java.lang.String getSymbol()
this
Stock.this
Stock.public double getPrice()
this
Stock.this
Stock as last obtained from the exchange.public char getDelimiter()
this
Stock.this
Stock.public boolean setDelimiter(char myDelimiter)
this
stock to the passed one. No change is made,
however, if the passed delimiter is a letter or a digit.myDelimiter
- the new delimiter charactertrue
if the change was made (i.e. if
the passed parameter is neither a letter nor a digit), and
return false
otherwise.public void setSymbol(java.lang.String symbol)
this
stock to the (capitalized) passed symbol.
The stock attributes are re-set as per the
refresh()
method.symbol
- the symbol to change to.public java.lang.String toString()
this
Stock.toString
in class java.lang.Object
delimiter
character, followed
by the stock's name. However if the symbol
doesn't exist, the string "No such stock!"
is returned.public boolean equals(java.lang.Object other)
this
one if
it is a stock object with the same symbol 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
Stock.hashCode
in class java.lang.Object
public void refresh()
this
stock and update its attributes accordingly.
If the symbol is null
or is not
listed on the exchange, the
name is set to null
and its
price to 0.public Stock cloneMe()
this
object.Stock
object
having exactly the state as this
one.