[英]Change value of arraylist field where object exists
我需要一些幫助,因為我被卡住了。 我有一個名為 orderList 的 ArrayList,類型為 ItemOrdered(int quantity,Item item)。 我想為 ArrayList 中存在給定項目的項目數量設置一個新值。 我嘗試了一切,但沒有奏效。
//place new order from buyer class
public void placeOrder(int quantity, Item item) {
//ItemOrdered newitemordered = new ItemOrdered(quantity,item );
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.addItemOrdered(quantity,item);
}
//call placeOrder method
buyer.placeOrder(quantity,eshop.ItemListPen.get(x));
import java.util.ArrayList;
public class ShoppingCart {
public ArrayList<ItemOrdered> orderList = new ArrayList<ItemOrdered>();
public void addItemOrdered(int quantity,Item item) {
if(item.getStock() >= quantity && (!item.equals(orderList))){
ItemOrdered newitemordered = new ItemOrdered(quantity,item);
orderList.add(new ItemOrdered(quantity,item));
}else if(item.getStock() < quantity){
System.out.println("Sorry,this quantity is not available in stock.");
}else{
orderList.get(item).setQuantity(quantity);
System.out.println("nothing");
}
}
}
public class ItemOrdered {
static Item item;
private int quantity;
public ItemOrdered(int quantity, Item item){
this.quantity=quantity;
this.item=item;
}
public void setQuantity(int x){
quantity=quantity + x;
}
}
當庫存大於或等於數量時,您似乎總是要進入if-else if-else
的if
塊,因為第二個條件(.item.equals(orderList))
將為真。 這個條件應該檢查列表是否包含項目,而不是列表和項目是否相等。 下一個難題是ItemOrdered
orderList
而不是Item
對象。 在這種情況下,簡單地執行.orderList.contains(item)
也將始終返回 true。
您可以通過將包含Item
對象的列表作為ShoppingCart
的一部分來解決此問題,或者添加一個新方法來循環遍歷orderList
並針對列表中的每個Item
檢查item
。
選項 #1 - 向ShoppingCart
添加新的Item
對象列表:
public class ShoppingCart {
public ArrayList<ItemOrdered> orderList = new ArrayList<ItemOrdered>();
// This could also be public if you needed for some reason, but you could also achieve that by adding getter and setter methods
// depending on version of Java, you may need to include Item in <> when creating the new list: new ArrayList<Item>()
private ArrayList<Item> itemsInCart = new ArrayList<>();
public void addItemOrdered(int quantity,Item item) {
if(item.getStock() >= quantity && !itemsInCart.contains(item)){
ItemOrdered newitemordered = new ItemOrdered(quantity,item); // this could be removed, and you just create the new ItemOrdered within .add() on the next line
orderList.add(newitemordered);
itemsInCart.add(item); // have to update the new list of items
}else if(item.getStock() < quantity){
System.out.println("Sorry,this quantity is not available in stock.");
}else{
orderList.get(item).setQuantity(quantity);
System.out.println("nothing");
}
}
}
選項 #2 - 遍歷 orderList:
public class ShoppingCart {
public ArrayList<ItemOrdered> orderList = new ArrayList<ItemOrdered>();
public void addItemOrdered(int quantity,Item item) {
if(item.getStock() >= quantity && !isItemInCart(item)){
orderList.add(new ItemOrdered(quantity,item));
}else if(item.getStock() < quantity){
System.out.println("Sorry,this quantity is not available in stock.");
}else{
orderList.get(item).setQuantity(quantity);
System.out.println("nothing");
}
}
private boolean isItemInCart(Item item){
for(ItemOrdered itemOrdered : orderList){
if(itemOrdered.getItem().equals(item)){
return true;
}
}
// Item is not in cart
return false;
}
}
public class ItemOrdered {
static Item item;
private int quantity;
public ItemOrdered(int quantity, Item item){
this.quantity=quantity;
this.item=item;
}
public void setQuantity(int x){
quantity=quantity + x;
}
public Item getItem(){
return this.item;
}
}
不同選項的注意事項:
orderList
非常大,循環可能會很昂貴(對於大多數實際情況來說可能不是什么大問題)Item
的.equals()
方法以准確定義要比較的內容以確定兩個項目是否相等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.