diff --git a/pom.xml b/pom.xml index b2c9ace..dab145f 100644 --- a/pom.xml +++ b/pom.xml @@ -180,6 +180,13 @@ 5.4.0 test + + + + javaee + javaee-api + 5 + 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/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 new file mode 100644 index 0000000..2b4a3d9 --- /dev/null +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -0,0 +1,102 @@ +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; + +import javax.faces.annotation.ManagedProperty; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; +import javax.faces.context.FacesContext; +import java.util.ArrayList; +import java.util.List; + +@ManagedBean +@SessionScoped +public class CartBean { + + @Setter + @ManagedProperty("#{userBean.user}") + private User user; + + public User getUser() { + FacesContext context = FacesContext.getCurrentInstance(); + return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); + } + + public String add(Book book){ + 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"; + } + } + 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(getUser()!=null){ + return db.shoppingCart().get(getUser()); + }else{ + return new ArrayList(); + } + } + + /* + 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 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 += ","; + if (sum % 100 < 10) { + res += "0"; + } + res += sum % 100; + return res; + } + +} 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/java/de/hsel/itech/jsf/UserBean.java b/src/main/java/de/hsel/itech/jsf/UserBean.java index b7190f7..09dac89 100644 --- a/src/main/java/de/hsel/itech/jsf/UserBean.java +++ b/src/main/java/de/hsel/itech/jsf/UserBean.java @@ -35,7 +35,6 @@ public class UserBean implements Serializable { @Getter private boolean error = false; - public String login() { Database db = Database.getInstance(); User user = db.user().get(username); @@ -58,6 +57,7 @@ public class UserBean implements Serializable { public String logout() { user = null; + loggedIn = false; return "index.html"; } diff --git a/src/main/webapp/bookinfo.xhtml b/src/main/webapp/bookinfo.xhtml index c05ee7b..d9acf2f 100644 --- a/src/main/webapp/bookinfo.xhtml +++ b/src/main/webapp/bookinfo.xhtml @@ -10,9 +10,9 @@ - + @@ -49,10 +49,12 @@ - - - in den Warenkorb - + + + + in den Warenkorb + + diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml new file mode 100644 index 0000000..58d205b --- /dev/null +++ b/src/main/webapp/cart.xhtml @@ -0,0 +1,68 @@ + + + + + + + + Warenkorb + + + + + + + + + + + #{item.article.title} + + + + #{item.article.priceString}€ + + + + + + + + + + + Entfernen + + + + + + + + + + + + Summe(#{cartBean.itemCount} Items): #{cartBean.sum}€ + + + + + Der Warenkorb ist Leer + + + + + + Jetzt Kaufen + + + + + + + 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 + + + + + + + + + + + + #{item.article.title} + + + #{item.count} x #{item.article.priceString}€ + + + + + + + + Summe: #{cartBean.sum}€ + + + + + + + Abbrechen + + + + Bestätigen + + + + + + + + diff --git a/src/main/webapp/template.xhtml b/src/main/webapp/template.xhtml index 7828748..b7591d1 100644 --- a/src/main/webapp/template.xhtml +++ b/src/main/webapp/template.xhtml @@ -37,8 +37,8 @@ - Katalog - (1) Warenkorb + Katalog + (#{cartBean.itemCount}) Warenkorb 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! + + + + + + +