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!