Ejemplo n.º 1
0
 /**
  * Get the total price of all items in the basket
  *
  * @param basket A list of basktet items
  * @return total
  */
 public Integer getTotal(List<BasketDTO> basket) {
   int total = 0;
   for (BasketDTO basketDTO : basket) {
     total = total + basketDTO.getTotalPrice();
   }
   return total;
 }
Ejemplo n.º 2
0
 /**
  * Add a new gnome to the customers basket
  *
  * @param username The name of the user
  * @param currentAmount Amount of the new gnome
  * @param currentColor Color of the new gnome
  * @return a updated list with all basket items
  */
 public List<BasketDTO> addToBasket(String username, Integer currentAmount, String currentColor) {
   if (updateInventory(currentColor, currentAmount, false) == true) {
     List<Integer> list =
         em.createNamedQuery("findBasketGnome")
             .setParameter("cus", username)
             .setParameter("col", currentColor)
             .getResultList();
     Integer aprice =
         (Integer)
             em.createNamedQuery("findGnomePrice")
                 .setParameter("col", currentColor)
                 .getSingleResult();
     aprice = aprice * currentAmount;
     BasketDTO basketitem;
     if (!list.isEmpty()) {
       basketitem = em.find(Basket.class, list.get(0));
       basketitem.setAmount(basketitem.getAmount() + currentAmount);
       basketitem.setTotalPrice(basketitem.getTotalPrice() + aprice);
       em.merge(basketitem);
     } else {
       List<Integer> id = em.createNamedQuery("countRows").getResultList();
       if (id.isEmpty()) {
         basketitem = new Basket(username, currentColor, currentAmount, 1, aprice);
       } else {
         int size = id.size();
         size++;
         basketitem = new Basket(username, currentColor, currentAmount, size, aprice);
       }
       em.persist(basketitem);
     }
   }
   return getBasket(username);
 }
Ejemplo n.º 3
0
 /**
  * Returns the basket items to the inventory
  *
  * @param username The name of the user
  * @return a null basket
  */
 public List<BasketDTO> returnBasket(String username) {
   List<Integer> list =
       em.createNamedQuery("findBasketGnomes").setParameter("cus", username).getResultList();
   BasketDTO basketgnome;
   for (int i = 0; i < list.size(); i++) {
     basketgnome = em.find(Basket.class, list.get(i));
     updateInventory(basketgnome.getColor(), basketgnome.getAmount(), true);
   }
   emptyBasket(username);
   return null;
 }