/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.th.wildau.webapp.buchladen.sessions;

import de.th.wildau.webapp.buchladen.entities.BookEntity;
import de.th.wildau.webapp.buchladen.facades.BookEntityFacadeRemote;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateful;

/**
 *
 * @author Jan
 */
@Stateful
public class CartSessionBean implements CartSessionBeanRemote {
    @EJB
    private BookEntityFacadeRemote bookEntityFacade;

    private List<BookEntity> cart;

    public CartSessionBean() {
        this.cart = new ArrayList<>();
    }

    @Override
    public void addBook(int id) {
        BookEntity bookEntity = this.bookEntityFacade.find(id);
        if(bookEntity != null) {
            this.cart.add(bookEntity);
        }
    }

    @Override
    public void removeBook(int id) {
        Iterator<BookEntity> iteratorBookEntity = this.cart.iterator();
        while(iteratorBookEntity.hasNext()) {
            if(id == iteratorBookEntity.next().getId()) {
                iteratorBookEntity.remove();
                break;
            }
        }
    }

    @Override
    public List<BookEntity> getContent() {
        return this.cart;
    }

    @Override
    public int count() {
        return this.cart.size();
    }

    @Override
    public double getTotal() {
        Iterator<BookEntity> iteratorBookEntity = this.cart.iterator();
        double total = 0.0;
        while(iteratorBookEntity.hasNext()) {
            total += iteratorBookEntity.next().getPrice();
        }
        return total;
    }
    
}

@@ -1410,24 +1410,109 @@
 t count() %7B%0A
+        if(this.cart.isEmpty()) %7B%0A            return 0;%0A        %7D%0A        else %7B%0A    
         retu
@@ -1528,24 +1528,34 @@
 art.size();%0A
+        %7D%0A
     %7D%0A%0A    @

update count method in cartSessionBean