Monopoly
Class Player

java.lang.Object
  extended by Monopoly.Player
All Implemented Interfaces:
java.lang.Comparable

public class Player
extends java.lang.Object
implements java.lang.Comparable

This is the class Player. Invariant: Once created and until destroyed, this instance represents a valid player of the game Monopoly. Through all that time he has the same name(String) and cardinal number (int).

getName()->String,
getLocation()->Cell,
getMoney()->int,
getProperties()->Collection,
isInJail()->boolean,
getNumOfDoubles()->int,
The instance of a player will be destroyed either when the game ends or when the player is bankrupted.


Constructor Summary
Player(java.lang.String name, int number)
          Creates a new Player object for the monopoly game.
 
Method Summary
 boolean buy(Property pr)
          Purchase a property (Transformer).
 boolean buyHouse(Street st)
          Purchase a house for a Street (Transformer).
 void collect(int value)
          Receive an amount of money (Transformer).
 int compareTo(java.lang.Object elem)
          Compares this player with the specified object for order.
 int getFortune()
          Get this player's total fortune (property value plus cash).
 boolean getIsInJail()
          Get the player's isInJail status (Accessor).
 GetOutOfJailCard getJailCard()
          Get the first instance of GetOutOfJailCard that can be found in the player's properties (Accessor).
 Cell getLocation()
          Get the player's location (Accessor).
 int getMoney()
          Get the player's cash (Accessor).
 java.lang.String getName()
          Get the name of the player (Accessor).
 int getNumber()
          Get the cardinal number of the player (Accessor).
 int getNumOfDoubles()
          Get the number of doubles this player has rolled (Accessor).
 java.util.Collection<Collectable> getProperties()
          Get the collection of properties of that player (Accessor).
 Collectable getProperty(java.lang.String name)
          Get the property (Collectable) of this player that matches the given String (Accessor).
 void mortage(Property pr)
          Mortage a property (Transformer).
 void move(int steps, Monopoly game)
          Move the player for a number of steps (Transformer).
 void moveTo(Cell loc, Monopoly game)
          Move the player to a location (Transformer).
 boolean pay(int value)
          Pay an amount of money to the Bank (Transformer).
 boolean pay(Player owner, int value)
          Pay an amount of money to another Player (Transformer).
 void releaseProperties()
          Set the owner of this player's properties as null.
 void sell(Collectable col)
          Give a Collectable object on a trade (Transformer).
 void sellHouse(Street st)
          Sell a house of a Street (Transformer).
 void setIsInJail(boolean is)
          Set the player's isInJail status (Transformer).
 void setNumOfDoubles(int value)
          Set the number of doubles this player has rolled (Transformer).
 void setTimes(int set)
          Set the number of rounds this player has been in prison (Transformer).
 int timesInJail()
          Get the number of rounds this player has been in prison (Accessor).
 void trade(Collectable col)
          Obtain a Collectable object from a trade (Transformer).
 boolean unmortage(Property pr)
          Unmortage a property (Transformer).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Player

public Player(java.lang.String name,
              int number)
Creates a new Player object for the monopoly game.

Parameters:
name - the name of the player
number - tha cardinal number of the player
Method Detail

getName

public java.lang.String getName()
Get the name of the player (Accessor).

Returns:
the name of that Player instance
Precondition:
Always valid
Postcondition:
The same name(String) will be returned every time this method is called

getNumber

public int getNumber()
Get the cardinal number of the player (Accessor).

Returns:
the integer that the player was given when created.
Precondition:
Always valid
Postcondition:
The same number (int) will be returned every time this method is called

getLocation

public Cell getLocation()
Get the player's location (Accessor).

Returns:
the Cell that is the player's location
Precondition:
Always valid
Postcondition:
A Cell is always returned that shows the location of the player
this.getIsInJail()->true == this.getLocation()->JAIL

getProperties

public java.util.Collection<Collectable> getProperties()
Get the collection of properties of that player (Accessor).

Returns:
a Collection of Collectable objects
Precondition:
Always valid
Postcondition:
Always returns a collection (empty or not)

getNumOfDoubles

public int getNumOfDoubles()
Get the number of doubles this player has rolled (Accessor).

Returns:
the number of doubles
Precondition:
Always valid
Postcondition:
numOfDoubles inside [0,3]

getMoney

public int getMoney()
Get the player's cash (Accessor).

Returns:
the cash of this player (int)
Precondition:
Always valid
Postcondition:
An integer value is always returned according to the fortune of the player

getIsInJail

public boolean getIsInJail()
Get the player's isInJail status (Accessor).

Returns:
the status of the player
Precondition:
Always valid
Postcondition:
A boolean value is always returned according to the status of the player
this.getIsInJail()->true == this.getLocation()->JAIL

getJailCard

public GetOutOfJailCard getJailCard()
Get the first instance of GetOutOfJailCard that can be found in the player's properties (Accessor).

Returns:
a GetOutOfJailCard object if he has one; null otherwise.
Precondition:
Always valid
Postcondition:
If the player has that card it is returned

getProperty

public Collectable getProperty(java.lang.String name)
Get the property (Collectable) of this player that matches the given String (Accessor).

Parameters:
name - the String to identify the property
Returns:
a Collectable object that matches the parameter; null if there is no match.
Precondition:
Always valid
Postcondition:
getProperty(name)->null
OR
getProperty(name).toString()->name

timesInJail

public int timesInJail()
Get the number of rounds this player has been in prison (Accessor).

Returns:
int the number of rounds
Precondition:
Always valid
Postcondition:
timesInJail()-> (>0) == isInJail
0

getFortune

public int getFortune()
Get this player's total fortune (property value plus cash).

Returns:
a integer value that represents the fortune.
Precondition:
Always valid
Postcondition:
getFortune() > 0

compareTo

public int compareTo(java.lang.Object elem)
              throws java.lang.ClassCastException
Compares this player with the specified object for order. Returns a negative integer, zero, or a positive integer as this player's fortune is less than, equal to, or greater than the specified player's fortune.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
elem - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this player is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException - - if the specified object's type is other than Player.

setNumOfDoubles

public void setNumOfDoubles(int value)
Set the number of doubles this player has rolled (Transformer).

Parameters:
value - the number of doubles
Precondition:
The player must have rolled doubles
value=numOfDoubles+1 OR ZERO
value inside [0,3]
Postcondition:
The numOfDoubles will be either one
more than before or zero but always
inside the [0,3] range

buy

public boolean buy(Property pr)
Purchase a property (Transformer).

Parameters:
pr - the Property being purchased
Returns:
true if pr has been bought; false otherwise.
Precondition:
pr.getOwner()->null
Postcondition:
If the player had enough money he
now owns this property and pr.getPrice()
has been removed from his fortune;
Also pr.getOwner()->this

trade

public void trade(Collectable col)
Obtain a Collectable object from a trade (Transformer).

Parameters:
col - the Collectable being obtained
Precondition:
This method must be invoked only from within
a trade event.
Postcondition:
col.getOwner()->this

sell

public void sell(Collectable col)
Give a Collectable object on a trade (Transformer).

Parameters:
col - the Collectable being given
Precondition:
This method must be invoked only from within
a trade event.
col.getOwner()->this
Postcondition:
This player no longer owns the Collectable

mortage

public void mortage(Property pr)
Mortage a property (Transformer).

Parameters:
pr - the Property being mortaged
Precondition:
pr.getOwner()->this
pr.numOfHouses()->ZERO
pr.isMortaged()->false
Postcondition:
pr.getPrice()/2 has been added to his fortune
pr.getOwner()->this,
pr.isMortaged()->true

unmortage

public boolean unmortage(Property pr)
Unmortage a property (Transformer).

Parameters:
pr - the Property being unmortaged
Returns:
true if pr has been unmortaged; false otherwise.
Precondition:
pr.getOwner()->this
pr.isMortaged()->true
Postcondition:
pr.getPrice()/2 plus 10% has been removed from his fortune
pr.getOwner()->this,
pr.isMortaged()->false

buyHouse

public boolean buyHouse(Street st)
Purchase a house for a Street (Transformer).

Parameters:
st - the Street on which the house will be built
Returns:
true if a building was bought; false otherwise.
Precondition:
Player must own all streets of the same color
st.isMortaged()->false
st.getNumOfHouses() less than 4 and st.getNumOfHotels() less than 1
Postcondition:
st.getNumOfHouses() one more than before or st.getNumOfHotels() one more than before if he had 4 houses

sellHouse

public void sellHouse(Street st)
Sell a house of a Street (Transformer).

Parameters:
st - the Street that had the house
Precondition:
st.getNumOfHouses() more than 0
Postcondition:
st.getNumOfHouses() one less than before or st.getNumOfHotels() one less than before and st.getNumOfHouses() returns 4

pay

public boolean pay(int value)
Pay an amount of money to the Bank (Transformer).

Parameters:
value - the amount of money to pay
Returns:
true if player had enough money and payed; false otherwise.
Precondition:
Always valid
Postcondition: If he had enough money the amount was removed from his fortune.

pay

public boolean pay(Player owner,
                   int value)
Pay an amount of money to another Player (Transformer).

Parameters:
owner - the Player to pay
value - the amount of money to pay
Returns:
true if player had enough money and payed; false otherwise.
Precondition:
Always valid
Postcondition:
If he had enough money the amount was removed from his fortune.

collect

public void collect(int value)
Receive an amount of money (Transformer).

Parameters:
value - the amount of money to receive
Precondition:
Always valid
Postcondition:
The amount has been added to his fortune

setIsInJail

public void setIsInJail(boolean is)
Set the player's isInJail status (Transformer).

Parameters:
is - the status of the player
Precondition:
Always valid
Postcondition:
The player will have the status that was set

move

public void move(int steps,
                 Monopoly game)
Move the player for a number of steps (Transformer).

Parameters:
steps - the number of steps the player will move
game - the Monopoly instance that represents the current game and holds the board of Cells
Precondition:
Always valid
Postcondition:
The player will have moved as many steps as the parameter defines

moveTo

public void moveTo(Cell loc,
                   Monopoly game)
Move the player to a location (Transformer).

Parameters:
loc - the Cell to which the player will move
Precondition:
loc must be a valid Cell of Monopoly
Postcondition:
The player will have moved to loc

setTimes

public void setTimes(int set)
Set the number of rounds this player has been in prison (Transformer).

Parameters:
set - the number of rounds
Precondition:
timesInJail()-> (>0) == isInJail
0Postcondition:
timesInJail()->set

releaseProperties

public void releaseProperties()
Set the owner of this player's properties as null.
Precondition:
Always valid
Postcondition:
this.properties.isEmpty()