Example #1
0
 /**
  * Add a new gnomes to the inventory
  *
  * @param currentColor The color of the new gnomes
  * @param currentAmount The amount of the new gnomes
  * @param currentPrice The new price of the gnome (null will keep the old price)
  * @return the updated list of gnomes
  */
 public List<GnomeDTO> addGnome(String currentColor, Integer currentAmount, Integer currentPrice) {
   List<Integer> list =
       em.createNamedQuery("findColorGnomes").setParameter("col", currentColor).getResultList();
   GnomeDTO gnome;
   if (list.isEmpty()) {
     gnome = new Gnome(getLastGnomeID() + 1, currentColor, currentPrice, currentAmount);
     em.persist(gnome);
   } else {
     gnome = em.find(Gnome.class, list.get(0));
     gnome.setAmount(gnome.getAmount() + currentAmount);
     if (currentPrice != null) {
       gnome.setPrice(currentPrice);
     }
     em.merge(gnome);
   }
   return getGnomes();
 }
Example #2
0
 /**
  * Updates the amount of gnomes in the inventory
  *
  * @param color The color of the new gnomes
  * @param amount The amount of the new gnomes
  * @param add True for adding gnomes and vice versa
  * @return if the inventory was updated
  */
 public boolean updateInventory(String color, Integer amount, boolean add) {
   List<Integer> list =
       em.createNamedQuery("findColorGnomes").setParameter("col", color).getResultList();
   if (list.isEmpty()) {
     return false;
   }
   int newAmount;
   GnomeDTO gnomeItem = em.find(Gnome.class, list.get(0));
   if (add == true) {
     newAmount = gnomeItem.getAmount() + amount;
   } else {
     newAmount = gnomeItem.getAmount() - amount;
   }
   if (newAmount < 0) {
     return false;
   } else {
     gnomeItem.setAmount(newAmount);
     em.merge(gnomeItem);
     return true;
   }
 }