From d1a45681a2239e2144425d6ee2f4cffb6874bf76 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 5 Jun 2019 13:11:36 +0200 Subject: [PATCH] +working shopping-cart --- src/main/java/de/hsel/itech/db/pojo/Book.java | 11 +++- src/main/java/de/hsel/itech/jsf/CartBean.java | 50 +++++++++++++------ .../java/de/hsel/itech/jsf/SettingsBean.java | 2 +- src/main/webapp/cart.xhtml | 37 ++++++++++---- 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/hsel/itech/db/pojo/Book.java b/src/main/java/de/hsel/itech/db/pojo/Book.java index 0d29601..def5539 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Book.java +++ b/src/main/java/de/hsel/itech/db/pojo/Book.java @@ -36,6 +36,15 @@ public class Book { @NonNull private String description; @NonNull private String image; - + public String getPriceString() { + String res = ""; + res += getPrice() / 100; + res += ","; + if (getPrice() % 100 < 10) { + res += "0"; + } + res += getPrice() % 100; + return res; + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java index f278dfd..342113a 100644 --- a/src/main/java/de/hsel/itech/jsf/CartBean.java +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -4,7 +4,6 @@ import de.hsel.itech.db.Database; import de.hsel.itech.db.pojo.Book; import de.hsel.itech.db.pojo.ShoppingCartItem; import de.hsel.itech.db.pojo.User; -import lombok.Getter; import lombok.Setter; import javax.faces.annotation.ManagedProperty; @@ -22,32 +21,40 @@ public class CartBean { @ManagedProperty("#{userBean.user}") private User user; - @Getter - private ArrayList bookList; - public User getUser() { FacesContext context = FacesContext.getCurrentInstance(); return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); } public String add(Book book){ - if(user != null){ - Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1)); - }else{ - if(bookList == null){ - bookList = new ArrayList(); + if(getUser() != null){ + for(ShoppingCartItem item : getItems()){ + if(item.getArticle().getId() == book.getId()){ + Database.getInstance().shoppingCart().delete(item); + item.setCount(item.getCount() + 1); + Database.getInstance().shoppingCart().insert(item); + return "cart.jsf"; + } } - bookList.add(book); + Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(),book,1)); + } + return "cart.jsf"; + } + + public String remove(ShoppingCartItem item){ + if(getUser() != null){ + Database.getInstance().shoppingCart().delete(item); } return "cart.jsf"; } public List getItems(){ Database db = Database.getInstance(); - if(user==null){ + if(getUser()!=null){ + return db.shoppingCart().get(getUser()); + }else{ return new ArrayList(); } - return db.shoppingCart().get(user); } public void setItems(List list) { @@ -55,11 +62,22 @@ public class CartBean { } public int getItemCount(){ - int count = getItems().size(); - if(bookList != null){ - count += bookList.size(); + return getItems().size(); + } + + public String getSum(){ + int sum = 0; + for(ShoppingCartItem item : getItems()){ + sum += item.getCount() * item.getArticle().getPrice(); } - return count; + String res = ""; + res += sum / 100; + res += ","; + if (sum % 100 < 10) { + res += "0"; + } + res += sum % 100; + return res; } } diff --git a/src/main/java/de/hsel/itech/jsf/SettingsBean.java b/src/main/java/de/hsel/itech/jsf/SettingsBean.java index 1fc50a6..c7e27c8 100644 --- a/src/main/java/de/hsel/itech/jsf/SettingsBean.java +++ b/src/main/java/de/hsel/itech/jsf/SettingsBean.java @@ -19,6 +19,6 @@ public class SettingsBean { @Getter @Setter - private boolean darkTheme = false; + private boolean darkTheme = true; } \ No newline at end of file diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml index 2105538..30385c7 100644 --- a/src/main/webapp/cart.xhtml +++ b/src/main/webapp/cart.xhtml @@ -8,41 +8,56 @@ +
+

Warenkorb

+
-
  • - -
  • -
    - +
    - Buchcover + Buchcover
    -

    #{book.title}

    +

    #{item.article.title}

    -

    #{book.price} (in cent)€

    +

    #{item.article.priceString}€

    - +
    - Entfernen + + + Entfernen + +
    + + +
    +

    Summe(#{cartBean.itemCount} Items): #{cartBean.sum}€

    +
    +
    + + +

    Der Warenkorb ist Leer

    +
    + +