From 1402e0fbf5baef810b6fb505c2ff7fccaea6b4d6 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 4 Jun 2019 20:13:57 +0200 Subject: [PATCH 1/6] +CartBean --- src/main/java/de/hsel/itech/jsf/CartBean.java | 40 +++++++++++++++++++ src/main/webapp/cart.xhtml | 29 ++++++++++++++ src/main/webapp/template.xhtml | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/hsel/itech/jsf/CartBean.java create mode 100644 src/main/webapp/cart.xhtml diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java new file mode 100644 index 0000000..32a52b0 --- /dev/null +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -0,0 +1,40 @@ +package de.hsel.itech.jsf; + +import de.hsel.itech.db.Database; +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; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; +import java.util.ArrayList; +import java.util.List; + +@ManagedBean +@RequestScoped +public class CartBean { + + @Getter + @Setter + @ManagedProperty("#{userBean.user}") + private User user; + + public List getItems(){ + Database db = Database.getInstance(); + if(user==null){ + return new ArrayList(); + } + return db.shoppingCart().get(user); + } + + public void setItems(List list) { + + } + + public int getItemCount(){ + return getItems().size(); + } + +} diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml new file mode 100644 index 0000000..90d4bc6 --- /dev/null +++ b/src/main/webapp/cart.xhtml @@ -0,0 +1,29 @@ + + + + + + + + +
  • + +
  • +
    + + #{userBean.user.name} + #{cartBean.user.name} + +
    + +
    +
    +
    +
    + diff --git a/src/main/webapp/template.xhtml b/src/main/webapp/template.xhtml index 28d705f..0d3f8f2 100644 --- a/src/main/webapp/template.xhtml +++ b/src/main/webapp/template.xhtml @@ -38,7 +38,7 @@
    1. Katalog
    2. -
    3. (1) Warenkorb
    4. +
    5. (#{cartBean.itemCount}) Warenkorb
      From 7412c45ecac4054cba7acee945bcc9982aca9a30 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 4 Jun 2019 21:58:44 +0200 Subject: [PATCH 2/6] nothing is working!!! --- pom.xml | 7 ++++++ src/main/java/de/hsel/itech/jsf/CartBean.java | 23 ++++++++++++++++++- src/main/webapp/bookinfo.xhtml | 18 +++++++++++---- src/main/webapp/cart.xhtml | 6 ++--- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 79f2417..cf8e714 100644 --- a/pom.xml +++ b/pom.xml @@ -174,6 +174,13 @@ 5.4.0 test + + + + javaee + javaee-api + 5 + diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java index 32a52b0..6a38aa0 100644 --- a/src/main/java/de/hsel/itech/jsf/CartBean.java +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -1,6 +1,7 @@ package de.hsel.itech.jsf; 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; @@ -9,6 +10,7 @@ import lombok.Setter; import javax.faces.annotation.ManagedProperty; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; +import javax.faces.context.FacesContext; import java.util.ArrayList; import java.util.List; @@ -16,11 +18,30 @@ import java.util.List; @RequestScoped public class CartBean { - @Getter @Setter @ManagedProperty("#{userBean.user}") private User user; + @Setter + @Getter + private long isbn; + + public User getUser() { + FacesContext context = FacesContext.getCurrentInstance(); + return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); + } + + public String add(){ + System.out.println("isbn: " + isbn); + if(Database.getInstance().book().exists(isbn)){ + Book book = Database.getInstance().book().get(isbn); + Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1)); + }else{ + return "index.jsf"; + } + return "cart.jsf"; + } + public List getItems(){ Database db = Database.getInstance(); if(user==null){ diff --git a/src/main/webapp/bookinfo.xhtml b/src/main/webapp/bookinfo.xhtml index c05ee7b..971be90 100644 --- a/src/main/webapp/bookinfo.xhtml +++ b/src/main/webapp/bookinfo.xhtml @@ -13,6 +13,12 @@ +
      + + + + in den Warenkorb +
      @@ -49,10 +55,14 @@

      - - - in den Warenkorb - + + + + + in den Warenkorb + +
    diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml index 90d4bc6..a8bc1ef 100644 --- a/src/main/webapp/cart.xhtml +++ b/src/main/webapp/cart.xhtml @@ -9,20 +9,20 @@ + #{cartBean.isbn} +
  • - #{userBean.user.name} - #{cartBean.user.name} - +
    From 1a115e00a9a9a70ea7acdbb0fc7c398efdbc71a5 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 4 Jun 2019 23:39:40 +0200 Subject: [PATCH 3/6] +shopping cart --- src/main/java/de/hsel/itech/jsf/BookBean.java | 4 +-- src/main/java/de/hsel/itech/jsf/CartBean.java | 24 +++++++++------- .../java/de/hsel/itech/jsf/CategoryBean.java | 4 +-- src/main/webapp/bookinfo.xhtml | 10 +------ src/main/webapp/cart.xhtml | 28 +++++++++++++++++-- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/hsel/itech/jsf/BookBean.java b/src/main/java/de/hsel/itech/jsf/BookBean.java index e135261..b087d8c 100644 --- a/src/main/java/de/hsel/itech/jsf/BookBean.java +++ b/src/main/java/de/hsel/itech/jsf/BookBean.java @@ -7,12 +7,12 @@ import lombok.Setter; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; -import javax.faces.bean.RequestScoped; +import javax.faces.bean.ViewScoped; import java.util.ArrayList; import java.util.List; @ManagedBean -@RequestScoped +@ViewScoped public class BookBean { @Getter diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java index 6a38aa0..f278dfd 100644 --- a/src/main/java/de/hsel/itech/jsf/CartBean.java +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -9,35 +9,35 @@ import lombok.Setter; import javax.faces.annotation.ManagedProperty; import javax.faces.bean.ManagedBean; -import javax.faces.bean.RequestScoped; +import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import java.util.ArrayList; import java.util.List; @ManagedBean -@RequestScoped +@SessionScoped public class CartBean { @Setter @ManagedProperty("#{userBean.user}") private User user; - @Setter @Getter - private long isbn; + private ArrayList bookList; public User getUser() { FacesContext context = FacesContext.getCurrentInstance(); return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); } - public String add(){ - System.out.println("isbn: " + isbn); - if(Database.getInstance().book().exists(isbn)){ - Book book = Database.getInstance().book().get(isbn); + public String add(Book book){ + if(user != null){ Database.getInstance().shoppingCart().insert(new ShoppingCartItem(user,book,1)); }else{ - return "index.jsf"; + if(bookList == null){ + bookList = new ArrayList(); + } + bookList.add(book); } return "cart.jsf"; } @@ -55,7 +55,11 @@ public class CartBean { } public int getItemCount(){ - return getItems().size(); + int count = getItems().size(); + if(bookList != null){ + count += bookList.size(); + } + return count; } } diff --git a/src/main/java/de/hsel/itech/jsf/CategoryBean.java b/src/main/java/de/hsel/itech/jsf/CategoryBean.java index e61b49e..7a5d15b 100644 --- a/src/main/java/de/hsel/itech/jsf/CategoryBean.java +++ b/src/main/java/de/hsel/itech/jsf/CategoryBean.java @@ -7,12 +7,12 @@ import lombok.Getter; import lombok.Setter; import javax.faces.bean.ManagedBean; -import javax.faces.bean.ViewScoped; +import javax.faces.bean.SessionScoped; import java.io.Serializable; import java.util.List; @ManagedBean -@ViewScoped +@SessionScoped public class CategoryBean implements Serializable { @Getter diff --git a/src/main/webapp/bookinfo.xhtml b/src/main/webapp/bookinfo.xhtml index 971be90..d9acf2f 100644 --- a/src/main/webapp/bookinfo.xhtml +++ b/src/main/webapp/bookinfo.xhtml @@ -10,15 +10,9 @@ -
    - - - - in den Warenkorb -
    @@ -56,9 +50,7 @@
    - - + in den Warenkorb diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml index a8bc1ef..2105538 100644 --- a/src/main/webapp/cart.xhtml +++ b/src/main/webapp/cart.xhtml @@ -9,14 +9,38 @@ - #{cartBean.isbn} -
  • + +
    +
    + Buchcover +
    +
    +

    #{book.title}

    +
    +
    +
    +

    #{book.price} (in cent)€

    +
    +
    +
    + +
    +
    +
    +
    + Entfernen +
    +
    +
    +
    +
    +
    Jetzt Kaufen From d1a45681a2239e2144425d6ee2f4cffb6874bf76 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 5 Jun 2019 13:11:36 +0200 Subject: [PATCH 4/6] +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

    +
    + +
    - From a53cccc52e91cdd532d34924f528b74635ada83c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 5 Jun 2019 13:22:52 +0200 Subject: [PATCH 5/6] +logout working --- src/main/java/de/hsel/itech/jsf/UserBean.java | 2 +- src/main/webapp/template.xhtml | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hsel/itech/jsf/UserBean.java b/src/main/java/de/hsel/itech/jsf/UserBean.java index 203a2f1..a0446cf 100644 --- a/src/main/java/de/hsel/itech/jsf/UserBean.java +++ b/src/main/java/de/hsel/itech/jsf/UserBean.java @@ -33,7 +33,6 @@ public class UserBean { @Getter private boolean error = false; - public String login() { Database db = Database.getInstance(); User user = db.user().get(username); @@ -56,6 +55,7 @@ public class UserBean { public String logout() { user = null; + loggedIn = false; return "index.html"; } diff --git a/src/main/webapp/template.xhtml b/src/main/webapp/template.xhtml index 0d3f8f2..13107e8 100644 --- a/src/main/webapp/template.xhtml +++ b/src/main/webapp/template.xhtml @@ -47,7 +47,13 @@
    1. Meine Bestellungen
    2. Adressen und Konten
    3. -
    4. Abmelden
    5. + + Abmelden + +
    6. Abmelden
    7. +
      +
      +
    From 0f763413e96ce6dccd72403d8546aa2860743622 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 5 Jun 2019 14:05:00 +0200 Subject: [PATCH 6/6] +confirmation and thank-you --- src/main/java/de/hsel/itech/jsf/CartBean.java | 21 +++++- src/main/webapp/cart.xhtml | 2 +- src/main/webapp/confirmation.xhtml | 67 +++++++++++++++++++ src/main/webapp/thanks.xhtml | 21 ++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/main/webapp/confirmation.xhtml create mode 100644 src/main/webapp/thanks.xhtml diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java index 342113a..2b4a3d9 100644 --- a/src/main/java/de/hsel/itech/jsf/CartBean.java +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -2,6 +2,7 @@ package de.hsel.itech.jsf; import de.hsel.itech.db.Database; import de.hsel.itech.db.pojo.Book; +import de.hsel.itech.db.pojo.OrderItem; import de.hsel.itech.db.pojo.ShoppingCartItem; import de.hsel.itech.db.pojo.User; import lombok.Setter; @@ -57,19 +58,37 @@ public class CartBean { } } + /* public void setItems(List list) { + }*/ + + public String buy(){ + ArrayList orderList = new ArrayList<>(); + for(ShoppingCartItem item : getItems()){ + orderList.add(new OrderItem(item.getArticle(),item.getCount())); + } + //Database.getInstance().order().insert(new Order(getUser(),orderList, new Date(234235235),sum(),null,null)); + for(ShoppingCartItem item : getItems()) { + Database.getInstance().shoppingCart().delete(item); + } + return "thanks.jsp"; } public int getItemCount(){ return getItems().size(); } - public String getSum(){ + public long sum(){ int sum = 0; for(ShoppingCartItem item : getItems()){ sum += item.getCount() * item.getArticle().getPrice(); } + return sum; + } + + public String getSum(){ + long sum = sum(); String res = ""; res += sum / 100; res += ","; diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml index 30385c7..58d205b 100644 --- a/src/main/webapp/cart.xhtml +++ b/src/main/webapp/cart.xhtml @@ -58,7 +58,7 @@ diff --git a/src/main/webapp/confirmation.xhtml b/src/main/webapp/confirmation.xhtml new file mode 100644 index 0000000..9801818 --- /dev/null +++ b/src/main/webapp/confirmation.xhtml @@ -0,0 +1,67 @@ + + + + + + +
    +

    Bestellbestätigung

    +
    + +
    +
    +

    Addresse:

    + Max Mustermann
    + Musterstraße 42
    + 12345 Musterstadt +
    +
    +

    Konto

    + IBAN: 12345••••••••••••••••••••••••111
    + Inhaber: Max Mustermann
    +
    +
    + + + + +
    +
    + Buchcover +
    +
    +

    #{item.article.title}

    +
    +
    +

    #{item.count} x #{item.article.priceString}€

    +
    +
    +
    + + + +
    +

    Summe: #{cartBean.sum}€

    +
    +
    + + +
    +
    + Abbrechen +
    +
    + + Bestätigen + +
    +
    + +
    +
    +
    + diff --git a/src/main/webapp/thanks.xhtml b/src/main/webapp/thanks.xhtml new file mode 100644 index 0000000..d533914 --- /dev/null +++ b/src/main/webapp/thanks.xhtml @@ -0,0 +1,21 @@ + + + + + + +
    +
    +

    + Vielen Dank für Ihren Einkauf! +

    +
    +
    +
    +
    +
    +