0

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?

3
  • what will you do with the products? I suppose that you have to search for them to buy/sell products so I would go rather for a Map with productID as the key and product as the value. Commented Oct 4, 2019 at 6:13
  • different stores can have the same product? Commented Oct 4, 2019 at 6:13
  • different store can have the same product name, but not necesarilly it is the same product. Let's say both StoreA and StoreB have product named "Pencil". The Pencil in storeA is not the same as the Pencil in storeB even though it both have the same name. Commented Oct 4, 2019 at 6:15

2 Answers 2

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>();
Sign up to request clarification or add additional context in comments.

4 Comments

should I create a getter to fetch the productList?
Yes you have to add all the required getters and setters
one more thing, I noticed that the store List is declared outside Product and Store class, does that mean I have to declare the store List at the main method?
yes it is just a sample template. Not the running code
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.