From 3c1c8f4d5ee4f33bfa9f800c0488129c99a1bafe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 1 Nov 2017 13:23:48 +0100 Subject: [PATCH] Continued: - the constructor is now first invoking other (default) constructor and then validates all parameters on a low level so the data is basically valid (not out-of-range or NULL) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../model/product/GenericProduct.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/org/mxchange/jproduct/model/product/GenericProduct.java b/src/org/mxchange/jproduct/model/product/GenericProduct.java index 8480c63..dd9f99b 100644 --- a/src/org/mxchange/jproduct/model/product/GenericProduct.java +++ b/src/org/mxchange/jproduct/model/product/GenericProduct.java @@ -16,6 +16,7 @@ */ package org.mxchange.jproduct.model.product; +import java.text.MessageFormat; import java.util.Date; import java.util.Objects; import javax.persistence.Basic; @@ -154,15 +155,53 @@ public class GenericProduct implements Product { *

* @param productI18nKey I18n key of product * @param productGrossPrice Product's gross price - * @param productCurrencyCode code for both prices + * @param productCurrencyCode Currency code for both prices * @param productCategory Category instance * @param productAvailability Availability (selectable by customer) + * @throws NullPointerException If a parameter is null + * @throws IllegalArgumentException If a parameter is empty (string) or out of bounds */ public GenericProduct (final String productI18nKey, final Float productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability) { + // Call other constructor first + this(); + + // Validate all parameters + if (null == productAvailability) { + // Throw NPE + throw new NullPointerException("productAvailability is null"); //NOI18N + } else if (null == productCategory) { + // Throw it again + throw new NullPointerException("productCategory is null"); //NOI18N + } else if (productCategory.getCategoryId() == null) { + // Throw it again + throw new NullPointerException("productCategory.categoryId is null"); //NOI18N + } else if (productCategory.getCategoryId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("productCategory.categoryId={0} is invalid", productCategory.getCategoryId())); //NOI18N + } else if (null == productCurrencyCode) { + // Throw it again + throw new NullPointerException("productCurrencyCode is null"); //NOI18N + } else if (productCurrencyCode.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("productCurrencyCode is empty"); //NOI18N + } else if (null == productGrossPrice) { + // Throw it again + throw new NullPointerException("productGrossPrice is null"); //NOI18N + } else if (productGrossPrice < 0) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("productGrossPrice={0} is invalid. Do not enter discounts as products.", productGrossPrice)); //NOI18N + } else if (null == productI18nKey) { + // Throw NPE + throw new NullPointerException("productI18nKey is null"); //NOI18N + } else if (productI18nKey.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("productI18nKey is empty"); //NOI18N + } + // Set all here this.productI18nKey = productI18nKey; this.productGrossPrice = productGrossPrice; - this.productCurrencyCode = productCurrencyCode; + this.productCurrencyCode = productCurrencyCode.toUpperCase(); this.productCategory = productCategory; this.productAvailability = productAvailability; } -- 2.39.5