I am currently trying to improve "World of Zuul" using BlueJ. I am at the point where I have made an Item
class and put a few items in there. The next step is to place the items into rooms which I think presents the current problem I am facing: How do you reference or use an ArrayList
established in another class? Hopefully if I can understand how that is done, then I may be able to work out how to place a specific item or items in a specific room.
Any help would be appreciated.
Please see my code below:
Item
class
public Item(String itemDescription, int itemWeight)
{
// initialise instance variables
itemDescription = itemDescription;
itemWeight = itemWeight;
this.list = new ArrayList<Item>();
Item item = new Item(itemDescription, itemWeight);
}
/**
* Name of item
*/
public String getItemDescription()
{
return itemDescription;
}
/**
* Weight of an Item
*/
public int itemWeight()
{
return itemWeight;
}
/**
* Show items
*/
public ArrayList<Item> getItems ()
{
return list;
}
public String getItemString()
{
String returnString = "Item:";
{
returnString += ""+list;
}
return returnString;
}
/**
* Presents name and weight of item
*/
public String toString()
{
return "Item: " + itemDescription + "Weight " + itemWeight;
}
/**
* List of items
*/
public void addItem()
{
list.add(new Item("can of coke",1));
list.add(new Item("bike",6));
list.add(new Item("textbook",4));
list.add(new Item("£20 note",1));
list.add(new Item("stick",2));
list.add(new Item("Theater leaflet",1));
list.add(new Item("mobile phone",2));
}
Room
class
public class Room
{
private String description;
private HashMap<String, Room> exits;
public Room(String description)
{
this.description = description;
}
/**
* Define the exits of this room. Every direction either leads
* to another room or is null (no exit there).
* @param north The north exit.
* @param east The east east.
* @param south The south exit.
* @param west The west exit.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public Room getExit(String direction)
{
return exits.get(direction);
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
/**
* Return a long description of this room, of the form;
* You are in the kitchen.
* Exits: north west
* @return A description of the room, including exits.
*/
public String getLongDescription()
{
return "You are" + description + "./n" +getExitString();
}
/**
* Return a description of the room's exits,
* for example, "Exits: north west".
* @return A description of the available exits.
*
*/
public String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit :keys) {
returnString += "" + exits;
}
return returnString;
}
}
Item
doesn't need a list of items. TheRoom
on the should have such list. A list of items in theItem
class would only make sense if that item is some kind of box which contains other items. But you don't have such items yet. Btw, please removeItem item = new Item(itemDescription, itemWeight);
from yourItem
constructor, it will only cause issues (StackOverflowError
).addItem
,removeItem
andlistItems
into the room class the have access to the items in the room. TheItem
class itself isn't obsolete, because you still need something which represents an item and that is what this class is made for. So you can have something likeItem apple = new Item("apple", 1)
and thenroom123.addItem(apple)
to put the item into the room orroom123.removeItem(apple); player.addItem(apple);
if the player grabs the apple from the floor and puts it into his pocket.