Collection for stock use

Asked

Viewed 101 times

-1

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

My scenario is this::
A system of stock management of a supermarket, this collection would make the database Pepel of the products, keeping the amount of items that the stock still has of certain product. You cannot have duplicate items. As for ordering, I do not know if it is necessary or not.

  • We need you to exemplify a scenario to better propose you the possibilities of working with Collections. For example, if you use ordered, unordered collections, those that don’t support duplicate items, etc.

  • I edited by adding more information.

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 interface Map. Its simplest implementation is the class HashMap. In this type of Collections, keys are unique and cannot be repeated.

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

It would look like this then:

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

or else:

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

Then you can add and recover quantities like this:

int idDoSabãoEmPó = 5;
produtosParaQuantidades.put(idDoSabãoEmPó, 100);
int quantidadeDeSabãoEmPó = produtosParaQuantidades.get(idDoSabãoEmPó);
  • I thought of using a Product class, which would encapsulate the data of a product, as name and value.

  • 1

    Might as well be. In this case, so that two equal products (same name and value) can be treated as a single key and not as different keys, you will need to overwrite the methods equals() and hashcode() class Produto.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.