简体   繁体   English

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

[英]Java Add Value to ArrayList pointing to Value in Another ArrayList

set where I should create a java code to store product list to respective store.设置我应该创建 java 代码以将产品列表存储到相应商店的位置。

for Example:例如:

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. 

I'm thinking to create an ArrayList to store the store name, but what is the best approach to add the product from one of the stores?我正在考虑创建一个 ArrayList 来存储商店名称,但是从其中一家商店添加产品的最佳方法是什么?

You have to follows OOPS approach for handling these scenarios.您必须遵循 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>();

Create a Class called store that has storeName and products like:创建一个名为storeName的 Class 商店,其products如下:

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

class Product {
   String name;
   int quantity;
   int price; 
}

So you can create a Product object from the user input and add it to the relevant store's products .因此,您可以根据用户输入创建一个Product object 并将其添加到相关商店的products中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM