0

I am trying to make a text-based game in Java. I came up with a very basic text inventory and a few methods: one to add a new item when found, one to remove it when used, and one to open the inventory itself. I used to reference the items as strings, but I also needed a description and a "found" boolean value to trigger some choices – so I decided to convert them into objects.

For context: my inventory is an ArrayList, as I needed a dynamic solution to add potential items on-the-go.

My problem: when I try to reference the "item"'s name, I get the value of the last Item I created, and not the one that I put in the "addItem()" method.

//My class, for reference:

public class Item {

static String name;
static String description;
static boolean isOwned = false;

public Item(String name, String description, boolean isOwned) {
    this.name = name;
    this.description = description;
    this.isOwned = isOwned;
}

public static String getName() {
    return name;
}

public static String getDescription() {
    return description;
}

public static boolean isOwned(boolean owned) {
    if (owned) {
        isOwned = true;
    } else if (!owned) {
        isOwned = false;
    }
    return isOwned;
}

Here's the methods I created for the inventory:

public static void addItem(Item obj) {
    inventory.add(Item.name);
    if (!startScreen) { //This is to prevent the method from printing out an
//output in the start screen, while doing some background "addItem()" processes.

        System.out.println("-------------------------------------------");
        System.out.println("\n> You have a new item: " + Item.name + ".");
        Item.isOwned(true);
    }
}

public static void removeItem(Item obj) {
    inventory.remove(Item.name);
    System.out.println("-------------------------------------------\n");
    System.out.println("> " + Item.name + " used.");
}

public static void openInventory() {
    System.out.print("> Your inventory: |");
    for (String item : inventory) {
        System.out.print(" " + item + " | ");
    }
    System.out.println("-------------------------------------------\n");
    System.out.println("> To use an item, please type the exact name."
            + "\n> Type 'X' to close the inventory.");

    String interact = in.

        if (interact.equals("x")) {
            break;
        }
    }

}

Now, assume I create two items, but want to add only the first one:

static Item sealedLetter = new Item(
    "Sealed Letter", //name
    "A letter from a desperate man to his beloved. It holds many secrets.", //description
    false); //'Found' State

static Item musicSheet = new Item(
    "Music Sheet", //name
    "A music sheet holding a melancholic melody in D minor. Someone might appreciate its gloomy tune.", //description
    false); //'Found' State

addItem(sealedLetter);

For some reason, my output will be like this, and will relate to the last created item only:

"You have a new item: Music Sheet."

As you will see, I tried to reference the items' variables and the single objects inside the methods, but it doesn't seem to work. It's obviously not functional nor efficient to create a method for each item in the inventory – although that would solve the issue, of course. What am I doing wrong? Am I not using the right parameter to reference the object?

I'm sure this issue of mine might help some people who wish to implement a similar and better system than mine – if I can get it to work, of course!

  • 2
    `static String name;` - static means that there will only ever be one of these. Every time you change the value of `name` it will change *all* values – Scary Wombat Jul 17 '18 at 01:56
  • 1
    In fact, it looks like *everything* is static, your fields, your methods, everything. Time to study *and apply* object-oriented principles, before doing anything else, including this program. Get a decent book, like Head First Object-Oriented Analysis and Design. – Hovercraft Full Of Eels Jul 17 '18 at 02:00
  • Hey Scary Wombat! Thank you for your tip; I tried to remove the static keyword earlier, but I get a reference error in my main method, stating that I cannot reference a "non-static" variable from a static context. Is there a work-around for that? 'Cause I believe that would solve my issue! EDIT: Also, thank you for your suggestion, Hovercraft! I'll look into that book as soon as I can. – Antonino Lupo Jul 17 '18 at 02:03
  • try searching for the error messages you are getting and you will find a thousand answers https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context – Scary Wombat Jul 17 '18 at 02:05
  • You have solved that problem, but backwards. The solution is not to make everything static but rather to learn how to create *instances* and create them. Again these are all basic object-oriented concepts, concepts that you really will need if you're to move forward in any sensible fashion. – Hovercraft Full Of Eels Jul 17 '18 at 02:17
  • In the `addItem(Item obj) {...}` method, the method accepts an `Item` (local variable name _obj_). But, within the method you are referring the `Item.name` - it should be `obj.name`. – prasad_ Jul 17 '18 at 02:20

0 Answers0