From: Roland Häder Date: Fri, 29 Dec 2017 13:16:57 +0000 (+0100) Subject: Need to switch to BigDecimal (and now correctly) as floats are not precise X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=cb620092b483765a12429f644e91ba446706dd23;p=jproduct-core.git Need to switch to BigDecimal (and now correctly) as floats are not precise enough (they are "guessed") when it comes to database systems. Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jproduct/model/product/GenericProduct.java b/src/org/mxchange/jproduct/model/product/GenericProduct.java index e9e6ecf..cc8f71d 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.math.BigDecimal; import java.text.MessageFormat; import java.util.Date; import java.util.Objects; @@ -115,8 +116,8 @@ public class GenericProduct implements Product { * Gross price of product */ @Basic (optional = false) - @Column (name = "product_gross_price", nullable = false) - private Float productGrossPrice; + @Column (name = "product_gross_price", nullable = false, precision = 10, scale = 4) + private BigDecimal productGrossPrice; /** * I18n key of product @@ -143,8 +144,8 @@ public class GenericProduct implements Product { /** * Net price of product */ - @Column (name = "product_net_price") - private Float productNetPrice; + @Column (name = "product_net_price", precision = 10, scale = 4) + private BigDecimal productNetPrice; /** * Number of product @@ -161,15 +162,15 @@ public class GenericProduct implements Product { /** * Tax rate (0-1, by 1=100%) */ - @Column (name = "product_tax_rate") - private Float productTaxRate; + @Column (name = "product_tax_rate", precision = 3, scale = 1) + private BigDecimal productTaxRate; /** * Amount of this product (for example 1 for 1 liter) */ @Basic (optional = false) - @Column (name = "product_unit_amount", nullable = false) - private Float productUnitAmount; + @Column (name = "product_unit_amount", nullable = false, precision = 10, scale = 2) + private BigDecimal productUnitAmount; /** * Unit type (for example liter) @@ -199,7 +200,7 @@ public class GenericProduct implements Product { * @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, final Float productUnitAmount, final String productUnitI18nKey) { + public GenericProduct (final String productI18nKey, final BigDecimal productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability, final BigDecimal productUnitAmount , final String productUnitI18nKey) { // Call other constructor first this(); @@ -225,7 +226,7 @@ public class GenericProduct implements Product { } else if (null == productGrossPrice) { // Throw it again throw new NullPointerException("productGrossPrice is null"); //NOI18N - } else if (productGrossPrice < 0) { + } else if (productGrossPrice.longValue() < 0) { // Throw IAE throw new IllegalArgumentException(MessageFormat.format("productGrossPrice={0} is invalid. Do not enter discounts as products.", productGrossPrice)); //NOI18N } else if (null == productI18nKey) { @@ -237,7 +238,7 @@ public class GenericProduct implements Product { } else if (null == productUnitAmount) { // Throw it again throw new NullPointerException("productUnitAmount is null"); //NOI18N - } else if (productUnitAmount < 0) { + } else if (productUnitAmount.longValue() < 0) { // Throw IAE throw new IllegalArgumentException(MessageFormat.format("productUnitAmount={0} is invalid. Do not enter discounts as products.", productUnitAmount)); //NOI18N } else if (null == productUnitI18nKey) { @@ -270,7 +271,9 @@ public class GenericProduct implements Product { final Product product = (Product) object; - if (!Objects.equals(this.getProductId(), product.getProductId())) { + if (!Objects.equals(this.getProductGrossPrice(), product.getProductGrossPrice())) { + return false; + } else if (!Objects.equals(this.getProductId(), product.getProductId())) { return false; } else if (!Objects.equals(this.getProductI18nKey(), product.getProductI18nKey())) { return false; @@ -336,12 +339,12 @@ public class GenericProduct implements Product { } @Override - public Float getProductGrossPrice () { + public BigDecimal getProductGrossPrice () { return this.productGrossPrice; } @Override - public void setProductGrossPrice (final Float productGrossPrice) { + public void setProductGrossPrice (final BigDecimal productGrossPrice) { this.productGrossPrice = productGrossPrice; } @@ -376,12 +379,12 @@ public class GenericProduct implements Product { } @Override - public Float getProductNetPrice () { + public BigDecimal getProductNetPrice () { return this.productNetPrice; } @Override - public void setProductNetPrice (final Float productNetPrice) { + public void setProductNetPrice (final BigDecimal productNetPrice) { this.productNetPrice = productNetPrice; } @@ -406,22 +409,22 @@ public class GenericProduct implements Product { } @Override - public Float getProductTaxRate () { + public BigDecimal getProductTaxRate () { return this.productTaxRate; } @Override - public void setProductTaxRate (final Float productTaxRate) { + public void setProductTaxRate (final BigDecimal productTaxRate) { this.productTaxRate = productTaxRate; } @Override - public Float getProductUnitAmount () { + public BigDecimal getProductUnitAmount () { return this.productUnitAmount; } @Override - public void setProductUnitAmount (final Float productUnitAmount) { + public void setProductUnitAmount (final BigDecimal productUnitAmount) { this.productUnitAmount = productUnitAmount; } @@ -439,6 +442,7 @@ public class GenericProduct implements Product { public int hashCode () { int hash = 7; + hash = 23 * hash + Objects.hashCode(this.getProductGrossPrice()); hash = 23 * hash + Objects.hashCode(this.getProductId()); hash = 23 * hash + Objects.hashCode(this.getProductI18nKey()); hash = 23 * hash + Objects.hashCode(this.getProductAgeGroup()); diff --git a/src/org/mxchange/jproduct/model/product/Product.java b/src/org/mxchange/jproduct/model/product/Product.java index cbd2818..5c331cb 100644 --- a/src/org/mxchange/jproduct/model/product/Product.java +++ b/src/org/mxchange/jproduct/model/product/Product.java @@ -17,6 +17,7 @@ package org.mxchange.jproduct.model.product; import java.io.Serializable; +import java.math.BigDecimal; import java.util.Date; import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; import org.mxchange.jproduct.model.category.Category; @@ -104,42 +105,42 @@ public interface Product extends Serializable { *

* @return Product's net price */ - Float getProductNetPrice (); + BigDecimal getProductNetPrice (); /** * Setter for product's net price *

* @param productNetPrice Product's net price */ - void setProductNetPrice (final Float productNetPrice); + void setProductNetPrice (final BigDecimal productNetPrice); /** * Getter for product's tax rate *

* @return Product's tax rate */ - Float getProductTaxRate (); + BigDecimal getProductTaxRate (); /** * Setter for product's tax rate *

* @param productTaxRate Product's tax rate */ - void setProductTaxRate (final Float productTaxRate); + void setProductTaxRate (final BigDecimal productTaxRate); /** * Getter for product's gross price *

* @return Product's gross price */ - Float getProductGrossPrice (); + BigDecimal getProductGrossPrice (); /** * Setter for product's gross price *

* @param productGrossPrice Product's gross price */ - void setProductGrossPrice (final Float productGrossPrice); + void setProductGrossPrice (final BigDecimal productGrossPrice); /** * Getter for currency code like EUR or USD @@ -174,14 +175,14 @@ public interface Product extends Serializable { *

* @return Product's unit amount */ - Float getProductUnitAmount (); + BigDecimal getProductUnitAmount (); /** * Setter for product's unit amount *

* @param productUnitAmount Product's unit amount */ - void setProductUnitAmount (final Float productUnitAmount); + void setProductUnitAmount (final BigDecimal productUnitAmount); /** * Getter for product's i18n key