1

I'm almost at the end of my final project in the university, I'm trying to control my inStock with my current Stock of articles.

My stock contains the following attributes:

int idMercaderia;
int cantidad;
int idSucursal;

I have two lists that contains Inventario type POJOs:

List <Inventario> stock = new InventarioDAO().findAll();
List <Inventario> inStock = new ArrayList <Inventario>();

Before persist in the database, I want to compare the attribute idMercaderia in both of 'em, if they're the same don't create another entry and just add the quantity cantidad to the current stock and do a saveOrUpdate() if not just create another entry.

I'm really stuck in this part and currently run out of ideas after trying iterate both Lists but I can't figure out the stock thingy.

Any help/code will be really appreciated.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Have you implemented an equals method in Inventario. If so please show the logic of it. – shazin Oct 23 '13 at 07:04
  • `Inventario` has been generated using Reverse Engineering with MyEclipse, so I only got the Hibernate DAO's (`save()`, `delete()`, `findById()`, etc.) – Felipe Rodriguez Oct 23 '13 at 07:09
  • 1
    org.apache.commons.beanutils [check this ](http://stackoverflow.com/questions/6099040/find-out-the-differences-between-two-java-beans-for-version-tracking/6099386#6099386) used for comparing beans – jos Oct 23 '13 at 13:52
  • @user2334391 thanks for sharing this, i'm enconding this right now inside my iteration. – Felipe Rodriguez Oct 23 '13 at 14:50

2 Answers2

1

you need to implement an equals() method if not already happened in the Inventario to compare idMercaderia in there. then

for(Inventario item: stock){
   if(inStock.contains(item)){
       item.cantidad++;  
   }
}
Roman K
  • 3,309
  • 1
  • 33
  • 49
  • This is one possible solution but makes the `equals` very context specific and hides a bit the logic in my opinion. It is also a possible source of future bugs, because someone might change the `equals` method (if we think of this as an enterprise application). So good unit testing is necessary here. – Kai Oct 23 '13 at 07:15
  • Gonna try this, but trying to `item.getCantidad()` `++` will be my problem, thank you. – Felipe Rodriguez Oct 23 '13 at 07:29
  • @user714965 yes it hides the logic a bit, but if the `idMercaderia` is the unique identifier, there should be no problem. it would be better if java would have an Comparator-like approach for this or you have to use java 8 ;) – Roman K Oct 23 '13 at 07:30
1

Create a Map<Integer, Inventario> of your first list to map an idMercaderia to one Inventario. Then iterate your second list and check for each item the corresponding one in the map.

Map<Integer, Inventario> map = new HashMap<Integer, Inventario>();
for (Inventario item : stock) {
   map.put(item.getIdMercaderia(), item);
}

and

for (Inventario item : inStock) {
   int idMercaderia = map.getIdMercaderia();
   Inventario inventario = map.get(idMercaderia);
   if (inventario == null) {
      // error handling
      continue;
   }
   if (item.getCantidad() == inventario.getCantidad() {
      // handle 
   }
}
Kai
  • 38,985
  • 14
  • 88
  • 103
  • This makes senses to my toughs, I changed the `map.put(item.getIdMercaderia(), item)` to `map.put(item.getMercaderia().getId(), item)` but in the Iteration of my second List I can't afford to do `map.getIdMercaderia()`. only map methods are shown, any clue on this? – Felipe Rodriguez Oct 23 '13 at 07:43
  • That's how a [Map](http://docs.oracle.com/javase/7/docs/api/java/util/Map.html) works. You `put` a key/value pair in and call `get(key)` to retrieve the value. So in your case `map.get(item.getMercaderia().getId())` will return the `item`. – Kai Oct 23 '13 at 07:48