package pl.koszela.jan.layer.service.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Component;
import pl.koszela.jan.layer.model.domain.impl.Order;
import pl.koszela.jan.layer.service.dao.OrderDAO;

/**
 * Created on 14.08.2017.
 *
 * @author Jan Koszela
 */
@Component("orderDAO")
public class DefaultOrderDAO implements OrderDAO {

  private List<Order> orders;

  public DefaultOrderDAO() {
    this.orders = new ArrayList<>();
  }

  @Override
  public Order findOrderByName(String productName) {
    for (Iterator<Order> i = orders.iterator(); i.hasNext(); ) {
      Order currentOrder = i.next();
      if (isSameProduct(productName, currentOrder)) {
        return currentOrder;
      }
    }

    return null;
  }

  private boolean isSameProduct(String productName, Order currentOrder) {
    return currentOrder.getItem().getName().equals(productName);
  }

  @Override
  public boolean addOrder(Order order) {
    return orders.add(order);
  }

  @Override
  public int findId(Order order) {
    for (int i = 0; i < orders.size(); i++) {
      if (orders.get(i).equals(order)) {
        return i;
      }
    }

    return -1;
  }

  @Override
  public boolean removeOrder(int id) {
    orders.remove(id);
    return true;
  }

  @Override
  public boolean updateOrder(int orderId, Order order) {
    orders.set(orderId, order);
    return true;
  }
}

@@ -1262,24 +1262,36 @@
 r(int id) %7B%0A
+    try %7B%0A  
     orders.r
@@ -1301,24 +1301,26 @@
 ve(id);%0A    
+  
 return true;
@@ -1316,24 +1316,93 @@
 eturn true;%0A
+    %7D catch (RuntimeException exception) %7B%0A      return false;%0A    %7D%0A
   %7D%0A%0A  @Over
@@ -1459,24 +1459,36 @@
 er order) %7B%0A
+    try %7B%0A  
     orders.s
@@ -1507,24 +1507,26 @@
 order);%0A    
+  
 return true;
@@ -1526,14 +1526,83 @@
 n true;%0A
+    %7D catch (RuntimeException exception) %7B%0A      return false;%0A    %7D%0A
   %7D%0A%7D%0A

Handle Exception for purpose for return type