Member Avatar for jonoj

Hey guys :)

I'm making a code and testing it in drjava but at this stage there is just one function I am having trouble with. Here is my code so far

import java.util.ArrayList;

class Player
{
  //instance fields, a name, a place, and an inventory
  String name;
  LocationID place;
  ArrayList<ItemID> inventory;
  
  //constructor function for creating a new player
  //sets a name and a default location
  Player(String playername)
  {
    name = playername;
    place = LocationID.BEDROOM;
  }
  
  //an accessor method to return the location
  LocationID location()
  {
    return place;
  }
  
  //another accessor method to return inventory
  ArrayList<ItemID> tools()
  {
    return inventory;
  }
  
  //a mutator method to add an item to the inventory
  void addItem(ItemID item)
  {
    inventory.add(item);
  }
  
//location and item data types  
enum LocationID { PATH, PORCH, LIVING, BEDROOM}
enum ItemID { KNIFE, ROPE, LANTARN, AMULET}
  
}

here is the output of what happens when i, create a new player, test out the accessor functions and the problem happens when i try and use the mutator...

> Player j = new Player ("Jonathan")
> j.location()
BEDROOM
> j.tools()
null
> j.addItem(ROPE)
Static Error: Undefined name 'ROPE'
>

I'm sorry if this is a stupid question I'm still learning the very basics unfortunately

1.In constructor create a new Instance of ArrayList class
inventory = new ArrayList<ItemID>();
2. > j.addItem(ROPE) --> look at line 15 of your Player class

Member Avatar for jonoj

1.In constructor create a new Instance of ArrayList class
inventory = new ArrayList<ItemID>();
2. > j.addItem(ROPE) --> look at line 15 of your Player class

Hey quuba, thanks so much for your reply!

I taken into account what you are saying and it makes sense because before there was no inventory created! just the instance so when a new player what made no inventory was made! So basically the list has to be formed before anything can be added into it.

However there is still a problem with adding items into the inventory... here is my code now (exactly as before but with a updated constructor:

import java.util.ArrayList;

class Player
{
  String name;
  LocationID place;
  ArrayList<ItemID> inventory;
  
  Player(String playername)
  {
    name = playername;
    place = LocationID.BEDROOM;
    inventory = new ArrayList<ItemID>();
  }
  
  LocationID location()
  {
    return place;
  }
  
  ArrayList<ItemID> tools()
  {
    return inventory;
  }
  
  void addItem(ItemID item)
  {
    inventory.add(item);
  }
  
enum LocationID { PATH, PORCH, LIVING, BEDROOM }
enum ItemID { KNIFE, ROPE, LANTARN, AMULET }
  
}

now attempting the same process as before (I get an empty list which is a good sign but I can't seem to figure out how to put ROPE into inventory :(

> Player j = new Player("Jonathan")
> j.location()
BEDROOM
> j.tools()
[]
> j.addItem(ROPE)
Static Error: Undefined name 'ROPE'
>

Your enums are members of the Player class with default access, so you can't refer to them from just anywhere. Making them public will help, as will using their fully-qualified names (like line 12).
ps: The members of the enums are locations and items, so adding "ID" to their names is misleading

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.