It could be a very simple solution but I just started learning Java. I would like to add every instantiated Product to the productList. Is there any way to solve this problem without modifying the access modifiers?
public class Product {
private int id;
private String name;
private float defaultPrice;
private Currency defaultCurrency;
private Supplier supplier;
private static List<Product> productList;
private ProductCategory productCategory;
public Product(float defaultPrice, Currency defaultCurrency, String name) {
this.id = IdGenerator.createID();
this.defaultPrice = defaultPrice;
this.defaultCurrency = defaultCurrency;
this.name = name;
}
}
Productcontaining a list ofProducts as a variable on class level (static)?productListis initialized (if not you could add=new LinkedList<>()to the declaration) you could just doproductList.add(this)in the constructor.Inventoryclass. In that class you would have a list ofProduct. EachProductwould not have list of nestedProduct.