net.sf.freecol.common.model
Class Market

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.model.FreeColGameObject
          extended by net.sf.freecol.common.model.Market
All Implemented Interfaces:
Ownable

public final class Market
extends FreeColGameObject
implements Ownable

This class implements a simple economic model whereby a market holds all goods that have been sold and the price of a particular type of good is determined solely by its availability in that market.


Field Summary
static int CUSTOM_HOUSE
          Constant for specifying the access to this Market when buying and selling goods.
static int EUROPE
          Constant for specifying the access to this Market when buying and selling goods.
static int MAXIMUM_PRICE
           
static int MINIMUM_PRICE
           
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ID_ATTRIBUTE_TAG
 
Constructor Summary
Market(Game game, org.w3c.dom.Element e)
          Initiates a new Market from an XML representation.
Market(Game game, Player player)
           
Market(Game game, java.lang.String id)
          Initiates a new Market with the given ID.
Market(Game game, javax.xml.stream.XMLStreamReader in)
          Initiates a new Market from an XML representation.
 
Method Summary
 boolean addGoodsToMarket(GoodsType goodsType, int amount)
          Add (or remove) some goods to this market.
 void addTransactionListener(TransactionListener listener)
          Adds a transaction listener for notification of any transaction
 void buy(GoodsType goodsType, int amount, Player player)
          Buys an amount of a particular type of good with the cost being met by a supplied player.
 int costToBuy(GoodsType type)
          Determines the cost to buy a single unit of a particular type of good.
 int getBidPrice(GoodsType type, int amount)
          Gets the price of a given goods when the Player buys.
 MarketData getMarketData(GoodsType goodsType)
          Return the market data for a type of goods.
 Player getOwner()
          Gets the owner of this Market.
 int getSalePrice(Goods goods)
          Gets the price of a given goods when the Player sells.
 int getSalePrice(GoodsType type, int amount)
          Gets the price of a given goods when the Player sells.
 TransactionListener[] getTransactionListener()
          Returns an array of all the TransactionListener added to this Market.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
 ModelMessage makePriceMessage(GoodsType goodsType)
          Make up a ModelMessage describing the change in this Market for a specified type of goods.
 int paidForSale(GoodsType type)
          Determines the price paid for the sale of a single unit of a particular type of goods.
 void putMarketData(GoodsType goodsType, MarketData data)
          Describe putMarketData method here.
 void removeTransactionListener(TransactionListener listener)
          Removes a transaction listener
 void sell(Goods goods, Player player)
          Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.
 void sell(GoodsType type, int amount, Player player)
          Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.
 void sell(GoodsType type, int amount, Player player, int marketAccess)
          Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.
 void setOwner(Player owner)
          Sets the owner of this Market.
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
addToRemoveElement, dispose, equals, equals, getFreeColGameObject, getFreeColGameObject, getGame, getGameOptions, getIntegerID, hashCode, hasID, isDisposed, isUninitialized, readFromXML, setGame, setId, toSavedXML, toString, toXML, updateFreeColGameObject, updateID
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readFromXMLElement, removePropertyChangeListener, removePropertyChangeListener, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, writeAttribute, writeFreeColGameObject
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MINIMUM_PRICE

public static final int MINIMUM_PRICE
See Also:
Constant Field Values

MAXIMUM_PRICE

public static final int MAXIMUM_PRICE
See Also:
Constant Field Values

EUROPE

public static final int EUROPE
Constant for specifying the access to this Market when buying and selling goods.

See Also:
Constant Field Values

CUSTOM_HOUSE

public static final int CUSTOM_HOUSE
Constant for specifying the access to this Market when buying and selling goods.

See Also:
Constant Field Values
Constructor Detail

Market

public Market(Game game,
              Player player)

Market

public Market(Game game,
              javax.xml.stream.XMLStreamReader in)
       throws javax.xml.stream.XMLStreamException
Initiates a new Market from an XML representation.

Parameters:
game - The Game this object belongs to.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

Market

public Market(Game game,
              org.w3c.dom.Element e)
Initiates a new Market from an XML representation.

Parameters:
game - The Game this object belongs to.
e - An XML-element that will be used to initialize this object.

Market

public Market(Game game,
              java.lang.String id)
Initiates a new Market with the given ID. The object should later be initialized by calling either FreeColGameObject.readFromXML(XMLStreamReader) or FreeColObject.readFromXMLElement(Element).

Parameters:
game - The Game in which this object belong.
id - The unique identifier for this object.
Method Detail

getMarketData

public MarketData getMarketData(GoodsType goodsType)
Return the market data for a type of goods.

Parameters:
goodsType - a GoodsType value
Returns:
a MarketData value

putMarketData

public void putMarketData(GoodsType goodsType,
                          MarketData data)
Describe putMarketData method here.

Parameters:
goodsType - a GoodsType value
data - a MarketData value

getOwner

public Player getOwner()
Gets the owner of this Market.

Specified by:
getOwner in interface Ownable
Returns:
The owner of this Market.

setOwner

public void setOwner(Player owner)
Sets the owner of this Market.

Specified by:
setOwner in interface Ownable
Parameters:
owner - The Player that shall own this Market.

costToBuy

public int costToBuy(GoodsType type)
Determines the cost to buy a single unit of a particular type of good.

Parameters:
type - A GoodsType value.
Returns:
The cost to buy a single unit of the given type of goods.

paidForSale

public int paidForSale(GoodsType type)
Determines the price paid for the sale of a single unit of a particular type of goods.

Parameters:
type - A GoodsType value.
Returns:
The price for a single unit of the given type of goods if it is for sale.

sell

public void sell(Goods goods,
                 Player player)
Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player. The goods is sold using EUROPE as the accesspoint for this market. Note that post-independence this no longer refers to a specific port.

Parameters:
goods - The Goods object being sold.
player - The Player selling the goods.

sell

public void sell(GoodsType type,
                 int amount,
                 Player player)
Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player. The goods is sold using EUROPE as the accesspoint for this market.

Parameters:
type - The type of goods to be sold.
amount - The amount of goods to be sold.
player - The Player selling the goods.

sell

public void sell(GoodsType type,
                 int amount,
                 Player player,
                 int marketAccess)
Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.

Parameters:
type - The type of goods to be sold.
amount - The amount of goods to be sold.
player - The Player selling the goods.
marketAccess - The place where goods are traded.

buy

public void buy(GoodsType goodsType,
                int amount,
                Player player)
Buys an amount of a particular type of good with the cost being met by a supplied player.

Parameters:
goodsType - The type of the good that is being bought.
amount - The amount of goods that are being bought.
player - The Player buying the goods.
Throws:
java.lang.IllegalStateException - If the player cannot afford to buy the goods.

addGoodsToMarket

public boolean addGoodsToMarket(GoodsType goodsType,
                                int amount)
Add (or remove) some goods to this market.

Parameters:
goodsType - The GoodsType to add.
amount - The amount of goods.
Returns:
True if the goods changes price due to this change.

getBidPrice

public int getBidPrice(GoodsType type,
                       int amount)
Gets the price of a given goods when the Player buys.

Parameters:
type - a GoodsType value
amount - The amount of goods.
Returns:
The bid price of the given goods.

getSalePrice

public int getSalePrice(GoodsType type,
                        int amount)
Gets the price of a given goods when the Player sells.

Parameters:
type - a GoodsType value
amount - The amount of goods.
Returns:
The sale price of the given goods.

getSalePrice

public int getSalePrice(Goods goods)
Gets the price of a given goods when the Player sells.

Parameters:
goods - a Goods value
Returns:
an int value

addTransactionListener

public void addTransactionListener(TransactionListener listener)
Adds a transaction listener for notification of any transaction

Parameters:
listener - the listener

removeTransactionListener

public void removeTransactionListener(TransactionListener listener)
Removes a transaction listener

Parameters:
listener - the listener

getTransactionListener

public TransactionListener[] getTransactionListener()
Returns an array of all the TransactionListener added to this Market.

Returns:
all of the TransactionListener added or an empty array if no listeners have been added

makePriceMessage

public ModelMessage makePriceMessage(GoodsType goodsType)
Make up a ModelMessage describing the change in this Market for a specified type of goods.

Parameters:
goodsType - The GoodsType that has changed price.
Returns:
A message describing the change.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Returns the tag name of the root element representing this object.

Returns:
the tag name.