Java向ArrayList添加值,指向另一个ArrayList中的值

Java向ArrayList添加值,指向另一个ArrayList中的值,java,arraylist,Java,Arraylist,设置我应该在何处创建java代码以将产品列表存储到相应的存储 例如: I have an array list of Store named storeList. It have 3 values ("StoreA", "StoreB","StoreC"). I was asked to add product to one of the store with certain command like: ADD PRODUCT StoreA and it will prompt the us

设置我应该在何处创建java代码以将产品列表存储到相应的存储

例如:

I have an array list of Store named storeList. It have 3 values ("StoreA", "StoreB","StoreC").

I was asked to add product to one of the store with certain command like:
ADD PRODUCT StoreA

and it will prompt the user to fill these:
Product Name : <user input>
Product Price: <user input>
Product Qty  : <user input>

after that, storeA will have new product named, price, and quantified according to user inputs. 
我有一个名为storeList的商店数组列表。它有3个值(“StoreA”、“StoreB”、“StoreC”)。
我被要求使用如下命令将产品添加到其中一个商店:
添加产品存储a
并提示用户填写以下内容:
产品名称:
产品价格:
产品数量:
之后,storeA将根据用户输入对新产品进行命名、定价和量化。

我正在考虑创建一个ArrayList来存储商店名称,但是从其中一个商店添加产品的最佳方法是什么?

您必须遵循OOPS方法来处理这些场景

class Product
{
    private String productName;
    private double price;
    private int quantity;
}

Class Store
{
   private String storeName;
   private List<Product> productList; //Product list can be retrieved from here
}

List<Store> storeList = new ArrayList<Store>();
类产品
{
私有字符串产品名称;
私人双价;
私人整数数量;
}
班级商店
{
私有字符串storeName;
private List productList;//可以从这里检索产品列表
}
List storeList=new ArrayList();

您必须遵循OOPS方法来处理这些场景

class Product
{
    private String productName;
    private double price;
    private int quantity;
}

Class Store
{
   private String storeName;
   private List<Product> productList; //Product list can be retrieved from here
}

List<Store> storeList = new ArrayList<Store>();
类产品
{
私有字符串产品名称;
私人双价;
私人整数数量;
}
班级商店
{
私有字符串storeName;
private List productList;//可以从这里检索产品列表
}
List storeList=new ArrayList();

创建一个名为store的类,该类包含
storeName
产品
,如:

class Store {
   String name;
   List<Product> products; 
}

class Product {
   String name;
   int quantity;
   int price; 
}
类存储{
字符串名;
列出产品清单;
}
类产品{
字符串名;
整数;
国际价格;
}

因此,您可以从用户输入创建一个
产品
对象,并将其添加到相关商店的
产品
创建一个名为store的类,该类具有
商店名
产品
,如:

class Store {
   String name;
   List<Product> products; 
}

class Product {
   String name;
   int quantity;
   int price; 
}
类存储{
字符串名;
列出产品清单;
}
类产品{
字符串名;
整数;
国际价格;
}

因此,您可以从用户输入创建一个
产品
对象,并将其添加到相关商店的
产品

如何处理这些产品?我想你必须搜索他们才能购买/销售产品,所以我宁愿选择一张地图,以productID为键,product为值。不同的商店可以有相同的产品?不同的商店可以有相同的产品名称,但不一定是相同的产品。假设StoreA和StoreB都有名为“Pencil”的产品。storeA中的铅笔与storeB中的铅笔不一样,尽管两者的名称相同。您将如何处理这些产品?我想你必须搜索他们才能购买/销售产品,所以我宁愿选择一张地图,以productID为键,product为值。不同的商店可以有相同的产品?不同的商店可以有相同的产品名称,但不一定是相同的产品。假设StoreA和StoreB都有名为“Pencil”的产品。storeA中的铅笔与storeB中的铅笔不同,尽管两者的名称相同。我是否应该创建一个getter来获取productList?是的,您必须添加所有必需的getter和setter还有一件事,我注意到store List在Product and store类外声明,这是否意味着我必须在main方法中声明存储列表?是的,它只是一个示例模板。不是正在运行的代码我应该创建一个getter来获取productList吗?是的,您必须添加所有必需的getter和setter还有一件事,我注意到存储列表是在Product和store类之外声明的,这是否意味着我必须在main方法中声明存储列表?是的,它只是一个示例模板。不是正在运行的代码