I'm trying to make this method in Java that adds an item to your inventory.
The bags[] array holds the names to the items and the items[] array hold the amount of items that correlates with that name.
I'm trying to make it so that if the itemName of the item being added already exists in the bags[] array, only the amount is added to that correlating index.
public class Inventory {
static String[] bags = {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"};
static int[] items = {0,0,0,0,0,0,0,0,0,0};
static int i = 0; //Iterate for loop//
static int x = 0;
static int y = 0;//Change open bag slot//
String itemName = "";
int amount = 0;
public static void addInv(String itemName, int amount){
bags[x] = itemName;
items[x] = items[x] + amount;
x++;
System.out.println("BAG UPFATE");
for(i = 0; i<9; i++){
System.out.println(bags[i] +"\t"+ items[i]);
}
}
}
Instead what I have is; adding each new itemName to a new index of the array, I've tried checking if
if(itemName == bags[x]){
items[x] = items[x] + amount;
}
to only add the amount to that position in the array but I keep getting really confused. I was wondering if anyone here could save me. haha