I am trying to implement an item class to add items to rooms in a game in BlueJ. However in the Room class the addItem method is not working because of a problem with the "items.add". I have no idea what I am doing wrong.
public class Room
{
private String description;
private HashMap<String, Room> exits; // stores exits of this room.
private ArrayList<Item> items;
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description)
{
this.description = description;
exits = new HashMap<String, Room>();
ArrayList<Item> items = new ArrayList<Item>();
}
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* @return The short description of the room
* (the one that was defined in the constructor).
*/
public String getShortDescription()
{
return description;
}
/**
* Return a description of the room in the form:
* You are in the kitchen.
* Exits: north west
* @return A long description of this room
*/
public String getLongDescription()
{
return "You " + description + ".\n" + getExitString() + ".\n" + "The item(s) in this room are: " + getItemString();
}
/**
* Add item when room is created
*/
public void addItem(String description, int weight)
{
items.add(description, weight);
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
private String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}
/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
* @param direction The exit's direction.
* @return The room in the given direction.
*/
public Room getExit(String direction)
{
return exits.get(direction);
}
/**
*
*/
public String getItemString()
{
String returnString = "";
{
returnString += " " + items;
}
return returnString;
}
}