gpt4 book ai didi

java - 将项目添加到 Apple

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:21 24 4
gpt4 key购买 nike

我正在尝试创建一个添加苹果的方法。列表中的每个苹果对象都包含一个单独的列表,用于存储苹果。我只是很困惑,不知道如何分解这整件事。以下是我如何设置这一切....

public class CustomerDatabase {
private ArrayList<Customer> customerList = null;

public CustomerDatabase() {
customerList = new ArrayList<>();
}
}

最佳答案

尝试这样的事情:

public void addProduct(String n, String p)
{
// loop though the Customers
for (Customer c : customerList)
{
// look for the one. i do not know what n is, assumed name from paramter n
if (n.equals(c.getName()))
{
// when found:

// add product to the wishlist, checking it is not on the list already
if (!c.getWishlist().contains(p))
c.getWishlist().add(p);
// if adding order of the wishlist is not important, another collection type
// EG a Set, which can only hold a value once, might be useful...

// exit the method, without throwing exception
return;
}
}
// if the loop finished, no Customer was found, throw exception
throw new IllegalArgumentException();
}

关于java - 将项目添加到 Apple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46459580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com