【发布时间】:2018-10-09 14:46:00
【问题描述】:
我必须创建一个 Shops 对象的数组列表,其中包含名称、描述和另一个产品对象的数组列表。 addProduct 方法留空,因为这是我面临的问题。我需要在使用 Shops 对象创建的商店数组列表中选择一个特定的商店名称,然后在产品数组列表中添加一个或多个产品。我不明白我怎么能做到这一点。如果你们能帮助我。
这是我目前的代码:
//class of shops but I removed the getters and setters to keep the code short
public class Shops {
private String name;
private String desc;
private ArrayList<Products> product;
public Shops(String name, String desc, ArrayList<Products> product) {
this.name = name;
this.desc = desc;
this.product = product;
}
//another class called Shop assistant which adds products to a specific shop
public class ShopAssistant {
ArrayList<Shops> shop = new ArrayList<>();
public void addShops(Shops shop) {
shop.add(shop);
}
public void addProduct(Products product ) {
//add products to product arraylist which should be linked to shop arraylist
}
【问题讨论】:
-
我认为这是一个糟糕的设计。 Shop 应该提供一个 API 来维护其产品列表。
标签: java arrays arraylist collections