es.ucm.fdi.hysteria.game
Class ItemRepository

java.lang.Object
  extended by es.ucm.fdi.hysteria.game.ItemRepository
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class ItemRepository
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

An inventory stores the items the player picks during the game. The inventory can be examined, presenting the names of the objects contained in it. Additionally, we can examine a single object in the inventory.

Author:
Sandor
See Also:
Serialized Form

Field Summary
protected  java.util.Set<Item> _items
          The collection that stores the items collected by the player
 
Constructor Summary
ItemRepository()
          Creates a new, empty inventory
 
Method Summary
 void addItem(Item newItem)
          Adds a new item to the inventory, if there is no item with the same name
 void clear()
          Clears the repository.
 java.lang.Object clone()
           
 java.util.Set<Item> getAllItems()
          Gets all items from the repository.
 java.util.Set<Item> getItems(java.lang.String name)
          Gets items by name from the repository.
 int getTotalValue()
          Computes the total number of points provided by the items stored in the inventory
 float getTotalWeight()
          Computes the total weight of actual item repository
 boolean hasItem(Item item)
          Checks whether the repository contains an specific item.
 boolean isEmpty()
          Checks whether the repository is empty.
 void removeItem(Item item)
          Removes an item from the repository.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_items

protected java.util.Set<Item> _items
The collection that stores the items collected by the player

Constructor Detail

ItemRepository

public ItemRepository()
Creates a new, empty inventory

Method Detail

addItem

public void addItem(Item newItem)
             throws java.lang.IllegalArgumentException,
                    ItemAlreadyInRepositoryException
Adds a new item to the inventory, if there is no item with the same name

Parameters:
newItem - The item that can be stored in the inventory
Throws:
java.lang.IllegalArgumentException
ItemAlreadyInRepositoryException

getAllItems

public java.util.Set<Item> getAllItems()
Gets all items from the repository.

Returns:
The set of items.

getItems

public java.util.Set<Item> getItems(java.lang.String name)
                             throws java.lang.IllegalArgumentException
Gets items by name from the repository.

Parameters:
name - The name.
Returns:
The set of items.
Throws:
java.lang.IllegalArgumentException - Argument 'name' cannot be null.

getTotalValue

public int getTotalValue()
Computes the total number of points provided by the items stored in the inventory

Returns:
The total number of points as the sum up of the item values

getTotalWeight

public float getTotalWeight()
Computes the total weight of actual item repository

Returns:
Size of the repository

isEmpty

public boolean isEmpty()
Checks whether the repository is empty.

Returns:
true if the repository is empty; false otherwise.

clear

public void clear()
Clears the repository.


removeItem

public void removeItem(Item item)
                throws java.lang.IllegalArgumentException,
                       ItemNotInRepositoryException
Removes an item from the repository.

Parameters:
item - The item.
Throws:
java.lang.IllegalArgumentException - Argument 'item' cannot be null.
ItemNotInRepositoryException - Item is not in this repository.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

hasItem

public boolean hasItem(Item item)
                throws java.lang.IllegalArgumentException
Checks whether the repository contains an specific item.

Parameters:
item - The item.
Returns:
true if the repository contains the item; false otherwise.
Throws:
java.lang.IllegalArgumentException

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object