*/
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;
* @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
* @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
* @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
* 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
* @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
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
* @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
// 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
* @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
* @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();
}
/**
* @return All categories
*/
@Override
- public Iterable<Category> getCategories () {
+ public Iterator<Category> getCategories () {
throw new UnsupportedOperationException("Needs refacturing ...");
}
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
}