Mohammad Iqbal
Mohammad Iqbal

Reputation: 335

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.

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?

Upvotes: 0

Views: 57

Answers (2)

theincrediblethor
theincrediblethor

Reputation: 448

Create a Class called store that has storeName and products like:

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.

Upvotes: 1

You have to follows OOPS approach for handling these scenarios.

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>();

Upvotes: 1

Related Questions