#UpdateQuantity of an object inside an arraylist
1 messages · Page 1 of 1 (latest)
ueueue
just need to get the value of the index cast it into an integer and increment it and update the arraylist
why are u casting it to an integer
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)
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;
can u show me Items
private String itemName;
private String itemDesc;
private int itemType;
private int itemQuantity;
is there any method that looks like setItemQuantity in Items?
all getters and setters yeah
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?
you have forgotten the ()
sorry mb.
unexpected type
required: variable
found: value
im getting this
Items item = items.get(indexOfItem);
item.setItemQuantity(item.getItemQuantity()++);
Uh can u paste entire Items class here?
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;
}
that does not work
the method call gives you an rvalue
rvalue?
right value
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;
int currentQuantity = item.getItemQuantity();
item.setItemQuantity(currentQuantity++);```
I suppose u can make a variable for it
++ 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)
this workedd
small thing I overlooked btw.
int currentQuantity = item.getItemQuantity();
item.setItemQuantity(currentQuantity + 1);```
thank youu for the helpp
yo pikachu listen
pika pika
hmm is there other ways to like implement it i needed it to update the inventory if it already exists
js use +1 its simple
that increments before the value is returned
🤷♂️ its upto u
aightt will keep it inmindd