Collection for use in stock

-1

What is the best implementation of the Collection interface for implementing an inventory class?
For example, a stock of grocery products.

My scenario is as follows:
A stock management system from a supermarket, this collection would make the products database pepel, storing the amount of items that the stock still holds for a particular product. You can not have duplicate items. As for ordination, I do not know if it is necessary or not.

    
asked by anonymous 22.05.2016 / 07:15

1 answer

2

You need to create a relationship between a key and a value. In this case, you need to map products to their respective quantities.

Who does this in Java is the Map interface. Its simplest implementation is the HashMap class. In this type of collections, keys are unique and can not be repeated.

The data type representing the quantity is simple: it can be a Integer . The product will depend on how you want to represent it. Would it be with a numeric product id (ie another Integer )? Or maybe a tag, which could be a String ?

It would look like this:

Map<Integer, Integer> produtosParaQuantidades = new HashMap<>();

or else:

Map<String, Integer> produtosParaQuantidades = new HashMap<>();

Then you can add and retrieve amounts like this:

int idDoSabãoEmPó = 5;
produtosParaQuantidades.put(idDoSabãoEmPó, 100);
int quantidadeDeSabãoEmPó = produtosParaQuantidades.get(idDoSabãoEmPó);
    
22.05.2016 / 21:16