#UpdateQuantity of an object inside an arraylist

1 messages · Page 1 of 1 (latest)

flint horizonBOT
#

<@&987246399047479336> please have a look, thanks.

frail roost
#

ueueue

#

just need to get the value of the index cast it into an integer and increment it and update the arraylist

sacred hazel
dapper kelp
#

x = x + 1; will increment x by 1

#

or in short x++;

#

but u first need to get hands on the value

#

currently u have the index only

#

(FYI, items is a List, not an array - so these comments are technically incorrect)

frail roost
#

int valueOfIndex = items.get(indexOfItem);
incompatible types: Items cannot be converted to int if i use Items object to get the value Items valueOfIndex = items.get(indexOfItem);
how can i increment it?

#

private ArrayList<Items> items;

sacred hazel
#

can u show me Items

frail roost
#

private String itemName;
private String itemDesc;
private int itemType;
private int itemQuantity;

sacred hazel
#

is there any method that looks like setItemQuantity in Items?

frail roost
#

all getters and setters yeah

sacred hazel
#

alright very good

#

Do this

#
int indexOfItem = items.indexOf(itemname);
Items item = items.get(indexOfItem);
item.setQuantity(item.getQuantity()++);
#

Just rename the setters cuz idk what u named them

#

does this make sense?

polar glacier
#

you have forgotten the ()

sacred hazel
#

sorry mb.

frail roost
#

unexpected type
required: variable
found: value

#

im getting this

#

Items item = items.get(indexOfItem);
item.setItemQuantity(item.getItemQuantity()++);

sacred hazel
#

Uh can u paste entire Items class here?

frail roost
#

public class Items {

public String getItemName() {
    return itemName;
}

public void setItemName(String itemName) {
    this.itemName = itemName;
}

public String getItemDesc() {
    return itemDesc;
}

public void setItemDesc(String itemDesc) {
    this.itemDesc = itemDesc;
}

public int getItemType() {
    return itemType;
}

public void setItemType(int itemType) {
    this.itemType = itemType;
}

public int getItemQuantity() {
    return itemQuantity;
}

public void setItemQuantity(int itemQuantity) {
    this.itemQuantity = itemQuantity;
}
private String itemName;
private String itemDesc;
private int itemType;
private int itemQuantity;

}

polar glacier
#

the method call gives you an rvalue

sacred hazel
#

rvalue?

polar glacier
#

right value

frail roost
#

public void updateQuantity(Items itemname){
// if it exists
if(items.contains(itemname)) {
// get the index of the parameters
int indexOfItem = items.indexOf(itemname);
// get the value inside the array
Items item = items.get(indexOfItem);
item.setItemQuantity(item.getItemQuantity()++);
// increment the value

       // set the new incremeneted value
     
     
       }
   }  and then this is the class public class Inventory {
static Items item = new Items();
private ArrayList<Items> items;
sacred hazel
#
            int currentQuantity = item.getItemQuantity();
            item.setItemQuantity(currentQuantity++);```
#

I suppose u can make a variable for it

polar glacier
#

++ only works on lvalues.

and when you use post increment then setQuantity will set the value before the incrementation (not sure if that is wanted here)

sacred hazel
#

small thing I overlooked btw.

#
            int currentQuantity = item.getItemQuantity();
            item.setItemQuantity(currentQuantity + 1);```
frail roost
#

thank youu for the helpp

sacred hazel
#

yo pikachu listen

frail roost
#

pika pika

sacred hazel
#

using increment operator won't help there.

#

it sets the value before updating it

frail roost
#

hmm is there other ways to like implement it i needed it to update the inventory if it already exists

polar glacier
#

there is also preincrement

#

++variable

sacred hazel
#

js use +1 its simple

polar glacier
#

that increments before the value is returned

sacred hazel
#

🤷‍♂️ its upto u

frail roost
#

aightt will keep it inmindd