简体   繁体   English

更改存在 object 的 arraylist 字段的值

[英]Change value of arraylist field where object exists

I need some help cause I got stuck.我需要一些帮助,因为我被卡住了。 I have an ArrayList called orderList with type ItemOrdered(int quantity,Item item).我有一个名为 orderList 的 ArrayList,类型为 ItemOrdered(int quantity,Item item)。 I want to set a new value for item's quantity where the given item exists into ArrayList.我想为 ArrayList 中存在给定项目的项目数量设置一个新值。 I tried everything but didn't work.我尝试了一切,但没有奏效。

//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;
    }

}



It looks like you're always going to enter the if block of your if-else if-else when the stock is greater than or equal to quantity because the second condition, (.item.equals(orderList)) , will be true.当库存大于或等于数量时,您似乎总是要进入if-else if-elseif块,因为第二个条件(.item.equals(orderList))将为真。 This condition should check to see if the list contains the item, not that the list and the item are equal.这个条件应该检查列表是否包含项目,而不是列表和项目是否相等。 The next piece of the puzzle is that the objects in orderList are ItemOrdered objects and not Item objects.下一个难题是ItemOrdered orderList而不是Item对象。 That being the case, simply doing .orderList.contains(item) will also always return true.在这种情况下,简单地执行.orderList.contains(item)也将始终返回 true。

You could solve this by having a list containing Item objects as part of your ShoppingCart , or adding a new method to loop through the orderList and check item against each Item in the list.您可以通过将包含Item对象的列表作为ShoppingCart的一部分来解决此问题,或者添加一个新方法来循环遍历orderList并针对列表中的每个Item检查item

Option #1 - Adding a new List of Item objects to ShoppingCart :选项 #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");
        }

    }

}

Option #2 - Looping through the orderList:选项 #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;
    }

}

Notes on the different options:不同选项的注意事项:

  • Option 1:选项1:
    • Simple简单的
    • Have to maintain a second list必须维护第二个列表
  • Option 2:选项 2:
    • Requires more work up front需要更多的前期工作
    • You don't have to maintain two lists containing similar data您不必维护两个包含相似数据的列表
    • Looping could be expensive if the orderList is exceedingly large (probably not a huge deal for most practical cases)如果orderList非常大,循环可能会很昂贵(对于大多数实际情况来说可能不是什么大问题)
    • You may want to override the .equals() method of Item to define exactly what's being compared to determine if two items are equal您可能希望覆盖Item.equals()方法以准确定义要比较的内容以确定两个项目是否相等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM