From 4061cb4e7dc12f942b699835a3125ba10ade5643 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 13 Oct 2017 23:43:54 +0200 Subject: [PATCH] Continued: - renamed ReceiptItem -> FinancialReceiptItem - added utils class ReceiptItems MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- ...iptItem.java => FinancialReceiptItem.java} | 6 +- .../model/receipt/item/ReceiptItems.java | 122 ++++++++++++++++++ 2 files changed, 125 insertions(+), 3 deletions(-) rename src/org/mxchange/jfinancials/model/receipt/item/{ReceiptItem.java => FinancialReceiptItem.java} (96%) create mode 100644 src/org/mxchange/jfinancials/model/receipt/item/ReceiptItems.java diff --git a/src/org/mxchange/jfinancials/model/receipt/item/ReceiptItem.java b/src/org/mxchange/jfinancials/model/receipt/item/FinancialReceiptItem.java similarity index 96% rename from src/org/mxchange/jfinancials/model/receipt/item/ReceiptItem.java rename to src/org/mxchange/jfinancials/model/receipt/item/FinancialReceiptItem.java index 8d9773d..66a602b 100644 --- a/src/org/mxchange/jfinancials/model/receipt/item/ReceiptItem.java +++ b/src/org/mxchange/jfinancials/model/receipt/item/FinancialReceiptItem.java @@ -50,7 +50,7 @@ import org.mxchange.jproduct.model.product.Product; } ) @SuppressWarnings ("PersistenceUnitPresent") -public class ReceiptItem implements BillableReceiptItem { +public class FinancialReceiptItem implements BillableReceiptItem { /** * Serial number @@ -111,7 +111,7 @@ public class ReceiptItem implements BillableReceiptItem { /** * Default constructor */ - public ReceiptItem () { + public FinancialReceiptItem () { } /** @@ -122,7 +122,7 @@ public class ReceiptItem implements BillableReceiptItem { * @param itemProductQuantity Product quantity * @param itemReceipt FinancialReceipt instance */ - public ReceiptItem (final Product itemProduct, final Float itemProductPrice, final Long itemProductQuantity, final BillableReceipt itemReceipt) { + public FinancialReceiptItem (final Product itemProduct, final Float itemProductPrice, final Long itemProductQuantity, final BillableReceipt itemReceipt) { // Call other constructor this(); diff --git a/src/org/mxchange/jfinancials/model/receipt/item/ReceiptItems.java b/src/org/mxchange/jfinancials/model/receipt/item/ReceiptItems.java new file mode 100644 index 0000000..d7d4fd7 --- /dev/null +++ b/src/org/mxchange/jfinancials/model/receipt/item/ReceiptItems.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jfinancials.model.receipt.item; + +import java.io.Serializable; +import java.text.MessageFormat; +import java.util.Objects; + +/** + * A utilities class for receipts + * + * @author Roland Häder + */ +public class ReceiptItems implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 2_867_938_676_165_402L; + + public static boolean isSameReceipt (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) { + // Pre-compare both as entities (same id) + if (Objects.equals(receiptItem1, receiptItem2)) { + // Same entity (with id number) + return true; + } + + // Validate parameter + if (null == receiptItem1) { + // Throw NPE + throw new NullPointerException("receiptItem1 is null"); + } else if ((receiptItem1.getItemId() instanceof Long) && (receiptItem1.getItemId() < 1)) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receiptItem1.itemId={0} is not valid.", receiptItem1.getItemId())); + } else if (receiptItem1.getItemReceipt()== null) { + // Throw NPE + throw new NullPointerException("receiptItem1.itemReceipt is null"); + } else if (receiptItem1.getItemReceipt().getReceiptId() == null) { + // Throw NPE + throw new NullPointerException("receiptItem1.itemReceipt.receiptId is null"); + } else if (receiptItem1.getItemReceipt().getReceiptId() < 1) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("receiptItem1.itemReceipt.receiptId={0} is not valid", receiptItem1.getItemReceipt().getReceiptId())); + } else if (receiptItem1.getItemProduct() == null) { + // Throw NPE again + throw new NullPointerException("receiptItem1.itemProduct is null."); + } else if (receiptItem1.getItemProduct().getProductId() == null) { + // Throw NPE again + throw new NullPointerException("receiptItem1.itemProduct.productId is null."); + } else if (receiptItem1.getItemProduct().getProductId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receiptItem1.itemProduct.productId={0} is invalid.", receiptItem1.getItemProduct().getProductId())); + } else if (null == receiptItem2) { + // Throw NPE + throw new NullPointerException("receiptItem2 is null"); + } else if ((receiptItem2.getItemId() instanceof Long) && (receiptItem2.getItemId() < 1)) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receiptItem2.itemId={0} is not valid.", receiptItem2.getItemId())); + } else if (receiptItem2.getItemReceipt()== null) { + // Throw NPE + throw new NullPointerException("receiptItem2.itemReceipt is null"); + } else if (receiptItem2.getItemReceipt().getReceiptId() == null) { + // Throw NPE + throw new NullPointerException("receiptItem2.itemReceipt.receiptId is null"); + } else if (receiptItem2.getItemReceipt().getReceiptId() < 1) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("receiptItem2.itemReceipt.receiptId={0} is not valid", receiptItem2.getItemReceipt().getReceiptId())); + } else if (receiptItem2.getItemProduct() == null) { + // Throw NPE again + throw new NullPointerException("receiptItem2.itemProduct is null."); + } else if (receiptItem2.getItemProduct().getProductId() == null) { + // Throw NPE again + throw new NullPointerException("receiptItem2.itemProduct.productId is null."); + } else if (receiptItem2.getItemProduct().getProductId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receiptItem2.itemProduct.productId={0} is invalid.", receiptItem2.getItemProduct().getProductId())); + } else if (null == receiptItem2) { + // Throw NPE + throw new NullPointerException("receiptItem2 is null"); + } + + // Now check all individually + if (!Objects.equals(receiptItem1.getItemReceipt(), receiptItem2.getItemReceipt())) { + // Other item receipt + return false; + } else if (!Objects.equals(receiptItem1.getItemProduct(), receiptItem2.getItemProduct())) { + // Other item product + return false; + } else if (!Objects.equals(receiptItem1.getItemProductPrice(), receiptItem2.getItemProductPrice())) { + // Other product price + return false; + } else if (!Objects.equals(receiptItem1.getItemProductQuantity(), receiptItem2.getItemProductQuantity())) { + // Other item quantity + return false; + } + + // Maybe same receipt! + return true; + } + + /** + * Private default constructor + */ + private ReceiptItems () { + // Utilities don't have instances + } + +} -- 2.39.5