]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Added more thrown exceptions + implemented getProducts()
authorRoland Haeder <roland@mxchange.org>
Thu, 13 Aug 2015 07:56:33 +0000 (09:56 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 13 Aug 2015 13:35:40 +0000 (15:35 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java
src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java

index 3e2a6a3951afbbe50917802f299fe9f9df60ff80..38487981be35699f260359758ccf5369afd0fb0d 100644 (file)
  */
 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<Product> getProducts ();
+       public Iterator<Product> 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<Category> getCategories ();
+       public Iterator<Category> getCategories () throws IOException, BadTokenException;
 
        /**
         * Checks if given Product instance is available and returns a printable
index 5b8c210ff656d7e00fb45e77bca73a6e0a5a21f7..0c8914f8e493bfb5ad81bf4fb685e94ca9904794 100644 (file)
@@ -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<Product> 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<Product> 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<Product> getProducts () {
-               throw new UnsupportedOperationException("Needs refacturing ...");
+       public Iterator<Product> 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<Category> getCategories () {
+       public Iterator<Category> getCategories () {
                throw new UnsupportedOperationException("Needs refacturing ...");
        }
 
@@ -1040,8 +1063,21 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                        this.abortProgramWithException(ex);
                }
 
+               // Init instance
+               Iterator<Product> 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
                }