java - 更改存在对象的 arraylist 字段的值

标签 java object arraylist reference set

我需要一些帮助,因为我被困住了。我有一个名为 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-elseif block ,因为第二个条件 (!item.equals(orderList)) 将为真。此条件应检查列表是否包含项目,而不是列表和项目是否相等。下一个难题是 orderList 中的对象是 ItemOrdered 对象而不是 Item 对象。在这种情况下,只需执行 !orderList.contains(item) 也将始终返回 true。

您可以通过将包含 Item 对象的列表作为您的 ShoppingCart 的一部分来解决此问题,或者添加一个新方法来遍历 orderList 并针对列表中的每个 Item 检查 item

选项 #1 - 将新的 Item 对象列表添加到 ShoppingCart:

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

}

不同选项的注意事项:

  • 选项 1:
    • 简单
    • 必须维护第二个列表
  • 选项 2:
    • 需要更多的前期工作
    • 您不必维护两个包含相似数据的列表
    • 如果 orderList 非常大(对于大多数实际情况来说可能不是什么大问题)
    • 您可能希望重写 Item.equals() 方法以准确定义要比较的内容以确定两个项目是否相等

关于java - 更改存在对象的 arraylist 字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62164393/

相关文章:

java - 如何监控具有多个服务器/Mule 代理的 Mule 3 ESB 社区版?

java - Hibernate 在合并后不初始化多对一关系

java - Struts 1.2 intRange 验证失败

java - Flink 是否提供 Java API 来向 JobManager 提交作业?

javascript 对象.. 将可能的条目限制为两个?

c# - 编译器不会让我使用三元运算符返回对象

java - 为什么 java clone() 留下相同的对象

java - 使用 Comparator 对 ArrayList Java 进行排序

java - 在JAVA中创建一个方法来调用另一个类的特定格式来打印ArrayList的内容

java - 创建包含不同类型对象的哈希集