]> git.mxchange.org Git - jproduct-core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Wed, 1 Nov 2017 12:23:48 +0000 (13:23 +0100)
committerRoland Häder <roland@mxchange.org>
Wed, 1 Nov 2017 12:23:48 +0000 (13:23 +0100)
- 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)

Signed-off-by: Roland Häder <roland@mxchange.org>
src/org/mxchange/jproduct/model/product/GenericProduct.java

index 8480c63152ea7a8746ac82d028873ad705d537de..dd9f99b8fbeaa0f88ef81de8198645cf1542f138 100644 (file)
@@ -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 {
         * <p>
         * @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;
        }