0

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:

Itemclass

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;
    }

}
5
  • 1
    Your structure is wrong, that's why you have problems now. An Item doesn't need a list of items. The Room on the should have such list. A list of items in the Item 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 remove Item item = new Item(itemDescription, itemWeight); from your Item constructor, it will only cause issues (StackOverflowError).
    – Tom
    Nov 24, 2016 at 16:30
  • Firstly, thanks for replying. So, I have removed "Item item = new Item(itemDescription, itemWeight);" and I can see what you mean about the Item class not needing a list of the items. Would the next step be to add "this.list = new ArrayList<Item>();" into the room constructor?
    – JZB
    Nov 24, 2016 at 16:44
  • It's just that the chapter I'm working on wants the Items to be made in the item class and the Room class to be able to access them. Do you mean to take the addItems method from the Item class and put it in the Room class? I mean if I did, wouldn't that make the Iem class obsolete?
    – JZB
    Nov 24, 2016 at 16:50
  • Yes a better structure would add methods like addItem, removeItem and listItems into the room class the have access to the items in the room. The Item 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 like Item apple = new Item("apple", 1) and then room123.addItem(apple) to put the item into the room or room123.removeItem(apple); player.addItem(apple); if the player grabs the apple from the floor and puts it into his pocket.
    – Tom
    Nov 24, 2016 at 16:57
  • Thank you Tom, the first part has worked and I have been looking at a few other situations where people have had a similar problem. I'm trying to write the methods you mentioned but after looking at the game class and the room class of this project, it seems that the rooms i.e. lab, office, outside etc. are in the game class and not actually mentioned directly in the room class. The room class seems to identify the exits and such. I think I can see where room123.addItem(apple); would go in the game class but I'm having trouble putting the methods in the room class.
    – JZB
    Nov 24, 2016 at 18:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.