type.lib
Class Stock

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

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

This class encapsulates a stock. It holds attributes relevant to a stock; such as its symbol, name, and last-traded price; and refreshes them on demand. In addition to standard methods (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:

For pedagogical reasons, certain attributes were made public to demonstrate the negative consequences of violating S/E guidelines. These anomalies are noted where applicable (see also the StockNS class).

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

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

Field Detail

name

public java.lang.String name
The name of 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.)


delimiter

public 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). The default is space.

(Bad design --should have been kept private. Access would have still been possible via the existing public accessor and mutator.)


titleCaseName

public static boolean titleCaseName
This field controls the format of the stock name as returned by the 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).

Constructor Detail

Stock

public Stock()
Construct a default Stock having a null symbol. This convenience constructor has the same effect as passing null to the 1-parameter, overloaded constructor.


Stock

public Stock(java.lang.String symbol)
Construct a Stock having the (capitalized) passed symbol. All of the stock attributes will be set as per the refresh() method.

Parameters:
symbol - the (ticker) symbol of the stock to construct.

Stock

public Stock(Stock stock)
Construct a copy of the passed Stock.

Parameters:
stock - the Stock to copy.
Throws:
java.lang.RuntimeException - if stock = null
Method Detail

getName

public java.lang.String getName()
Determine the name of this Stock as listed on the exchange. The letter case is as indicated in the titleCaseName field.

Returns:
the name of this Stock. If no such stock exists, null is returned.

getSymbol

public java.lang.String getSymbol()
Determine the symbol of this Stock.

Returns:
the symbol of this Stock.

getPrice

public double getPrice()
Determine the price of this Stock.

Returns:
the price of this Stock as last obtained from the exchange.

getDelimiter

public char getDelimiter()
Determine the delimiter of this Stock.

Returns:
the delimiter of this Stock.

setDelimiter

public boolean setDelimiter(char myDelimiter)
Mutator to change the delimiter of this stock to the passed one. No change is made, however, if the passed delimiter is a letter or a digit.

Parameters:
myDelimiter - the new delimiter character
Returns:
true if the change was made (i.e. if the passed parameter is neither a letter nor a digit), and return false otherwise.

setSymbol

public void setSymbol(java.lang.String symbol)
Mutator to change the symbol of this stock to the (capitalized) passed symbol. The stock attributes are re-set as per the refresh() method.

Parameters:
symbol - the symbol to change to.

toString

public java.lang.String toString()
Construct a string representation of this Stock.

Overrides:
toString in class java.lang.Object
Returns:
the stock's symbol (capitalized), followed by the delimiter character, followed by the stock's name. However if the symbol doesn't exist, the string "No such stock!" is returned.

equals

public boolean equals(java.lang.Object other)
Test the equality of stock objects. An object is considered equal to this one if it is a stock object with the same symbol as this one.

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

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

refresh

public void refresh()
Connect to the exchange and obtain the name and price of 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.


cloneMe

public Stock cloneMe()
Copy the state of this object.

Returns:
a reference to a new Stock object having exactly the state as this one.


Java by Abstraction: A Client-View Approach