From: Roland Haeder Date: Thu, 13 Aug 2015 07:56:33 +0000 (+0200) Subject: Added more thrown exceptions + implemented getProducts() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=45b1ad2ec5cbad45ec73d98b76833d0fc9c7bb73;p=pizzaservice-war.git Added more thrown exceptions + implemented getProducts() Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index 3e2a6a39..38487981 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -16,9 +16,13 @@ */ package org.mxchange.pizzaapplication.application; +import java.io.IOException; +import java.util.Iterator; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.mxchange.jcore.application.Application; +import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.product.Product; @@ -112,8 +116,9 @@ public interface PizzaApplication extends Application { * @param request Request instance * @param session Session instance * @return Total price of all choosen products + * @throws javax.servlet.ServletException If something unexpected happened */ - public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session); + public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException; /** * Calculates total amount of all choosen products @@ -121,8 +126,9 @@ public interface PizzaApplication extends Application { * @param request Request instance * @param session Session instance * @return Total amount of all choosen products + * @throws javax.servlet.ServletException If something unexpected happened */ - public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session); + public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException; /** * Some "getter" for HTML code 'checked="checked"' if the product is choosen @@ -140,8 +146,9 @@ public interface PizzaApplication extends Application { * @param request Request instance * @param session Session instance * @return Whether the product is choosen + * @throws javax.servlet.ServletException If something unexpected happened */ - public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session); + public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException; /** * Marks given product as ordered in session @@ -197,15 +204,19 @@ public interface PizzaApplication extends Application { * Some "getter" for a an array of all products * * @return All products + * @throws java.io.IOException If an IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found */ - public Iterable getProducts (); + public Iterator getProducts () throws IOException, BadTokenException; /** * Some "getter" for a an array of all categories * * @return All categories + * @throws java.io.IOException If an IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found */ - public Iterable getCategories (); + public Iterator getCategories () throws IOException, BadTokenException; /** * Checks if given Product instance is available and returns a printable diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index 5b8c210f..0c8914f8 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -141,7 +141,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return Total amount of all choosen products */ @Override - public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) { + public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException { // Trace message this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N @@ -154,11 +154,22 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new NullPointerException("session is null"); //NOI18N } - // Init total price + // Init/declare total price and iterator int totalAmount = 0; + Iterator iterator; + + try { + // Get iterator + iterator = this.getProducts(); + } catch (final IOException | BadTokenException ex) { + throw new ServletException(ex); + } // "Walk" over all products - for (final Product product : this.getProducts()) { + while (iterator.hasNext()) { + // Get next product + Product product = iterator.next(); + // Is this choosen? if (this.isProductChoosen(product, request, session)) { // Then add ordered amount @@ -189,7 +200,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return Total price of all choosen products */ @Override - public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) { + public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException { // Trace message this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N @@ -205,8 +216,19 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Init total price float totalPrice = 0.00f; + // Get iterator + Iterator iterator; + try { + iterator = this.getProducts(); + } catch (final IOException | BadTokenException ex) { + throw new ServletException(ex); + } + // "Walk" over all products - for (final Product product : this.getProducts()) { + while (iterator.hasNext()) { + // Get next product + Product product = iterator.next(); + // Is this choosen? if (this.isProductChoosen(product, request, session)) { // Then add product's total price @@ -365,7 +387,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return Whether the product is choosen */ @Override - public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) { + public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException { // Trace message this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N @@ -502,8 +524,9 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return All products */ @Override - public Iterable getProducts () { - throw new UnsupportedOperationException("Needs refacturing ..."); + public Iterator getProducts () throws IOException, BadTokenException { + // Ask frontend for a list of products + return this.productFrontend.getProducts(); } /** @@ -512,7 +535,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return All categories */ @Override - public Iterable getCategories () { + public Iterator getCategories () { throw new UnsupportedOperationException("Needs refacturing ..."); } @@ -1040,8 +1063,21 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P this.abortProgramWithException(ex); } + // Init instance + Iterator iterator = null; + + try { + // Get iterator + iterator = this.getProducts(); + } catch (final IOException | BadTokenException ex) { + this.abortProgramWithException(ex); + } + // "Walk" over all products - for (final Product product : this.getProducts()) { + while ((iterator instanceof Iterator) && (iterator.hasNext())) { + // Get next product + Product product = iterator.next(); + // Output data this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N }