From eee0848dffd19904aad8e2826a5b56f7bcf4815e Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz_lightsnack_2007@users.noreply.github.com> Date: Wed, 28 Feb 2024 10:45:31 +0800 Subject: [PATCH] updated code enable finalization store transaction status checking --- .../tech/iBeans/POSware/Lite/transact.java | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main/java/tech/iBeans/POSware/Lite/transact.java b/src/main/java/tech/iBeans/POSware/Lite/transact.java index c9db10c..315454b 100644 --- a/src/main/java/tech/iBeans/POSware/Lite/transact.java +++ b/src/main/java/tech/iBeans/POSware/Lite/transact.java @@ -4,9 +4,6 @@ */ package tech.iBeans.POSware.Lite; -// Import local modules. -import tech.iBeans.POSware.Lite.inventory.*; -import tech.iBeans.POSware.Lite.receipt.*; import java.text.DecimalFormat; // Import global modules. @@ -17,9 +14,10 @@ import java.util.*; * @author eleven */ public class transact { - public static Dictionary items = new Hashtable<>(); + public static Dictionary> items = new Hashtable<>(); public static Dictionary price = new Hashtable<>(); public static Boolean state = false; + public static Boolean progress = false; /* {item: { @@ -41,6 +39,7 @@ public class transact { /* Initialize the variables, setting them with default values. */ + progress = true; items = new Hashtable<>(); price = new Hashtable<>(); @@ -66,7 +65,8 @@ public class transact { return(state); } - public static Dictionary calculate() { + @SuppressWarnings("unchecked") + public static Dictionary calculate() { /* Calculate the subtotal, total, and VAT. Returns: (Dictionary) the contents of the price variable @@ -84,17 +84,17 @@ public class transact { while (items_list.hasMoreElements()) { String item = items_list.nextElement(); - if (((Dictionary) items.get(item).get("price")).get("tax") != null) { - price.put("tax", (float) price.get("tax") + (float) ((Dictionary) items.get(item).get("price")).get("tax")); + if (((Dictionary) items.get(item).get("price")).get("tax") != null) { + price.put("tax", (float) price.get("tax") + (float) ((Dictionary) items.get(item).get("price")).get("tax")); }; - if (((Dictionary) items.get(item).get("price")).get("subtotal") != null) { - price.put("subtotal", (float) price.get("subtotal") + (float) ((Dictionary) items.get(item).get("price")).get("subtotal")); + if (((Dictionary) items.get(item).get("price")).get("subtotal") != null) { + price.put("subtotal", (float) price.get("subtotal") + (float) ((Dictionary) items.get(item).get("price")).get("subtotal")); }; - if (((Dictionary) items.get(item).get("price")).get("total") != null) { - price.put("total", (float) price.get("total") + (float) ((Dictionary) items.get(item).get("price")).get("total")); + if (((Dictionary) items.get(item).get("price")).get("total") != null) { + price.put("total", (float) price.get("total") + (float) ((Dictionary) items.get(item).get("price")).get("total")); }; - if (((Dictionary) items.get(item).get("price")).get("discount") != null) { - price.put("discount", (float) price.get("discount") + (float) ((Dictionary) items.get(item).get("price")).get("discount")); + if (((Dictionary) items.get(item).get("price")).get("discount") != null) { + price.put("discount", (float) price.get("discount") + (float) ((Dictionary) items.get(item).get("price")).get("discount")); }; }; @@ -108,9 +108,10 @@ public class transact { return(price); }; + @SuppressWarnings("unchecked") public static Dictionary calculate(String SKU, int QUANTITY) { inventory.refresh(); - Dictionary item_details = inventory.items.get(SKU); + Dictionary item_details = inventory.items.get(SKU); // Create an output dictionary. Dictionary item_pricing = new Hashtable<>(); @@ -174,11 +175,11 @@ public class transact { Returns: (boolean) the calculation state */ - if (QUANTITY == 0) {return(false);} + if (QUANTITY == 0 || !progress) {return(false);} else if (QUANTITY < 0) {return(remove(SKU, QUANTITY));}; // containing the current data's information - Dictionary ITEM_CURRENT_DATA = items.get(SKU); + Dictionary ITEM_CURRENT_DATA = items.get(SKU); // check if the item exists if (ITEM_CURRENT_DATA != null) { @@ -218,11 +219,11 @@ public class transact { Returns: (boolean) the removal state */ - if (QUANTITY == 0) {return(false);} + if (QUANTITY == 0 || !progress) {return(false);} else if (QUANTITY < 0) {return(add(SKU, QUANTITY));}; // containing the current data's information - Dictionary ITEM_CURRENT_DATA = items.get(SKU); + Dictionary ITEM_CURRENT_DATA = items.get(SKU); // check if the item exists. There's no need to remove an item that's already been removed. if (ITEM_CURRENT_DATA != null) { @@ -252,15 +253,24 @@ public class transact { public static void pay() { /* Initialize the payment process of the transaction. */ - payment.init(Float.valueOf(FORMAT_DECIMAL.format(Float.valueOf(price.get("total").toString())))); + if (state && progress) { + // Can only run when the transaction is active (you don't want to pay again right after you've paid, right?) + payment.init(Float.valueOf(FORMAT_DECIMAL.format(Float.valueOf(price.get("total").toString())))); + }; }; public static void finalise() { /* Finalize the transaction. */ - // TODO Replace this section with wrapping up the payment, "saving" the receipt, and then initializing it. - - // receipt.finalise(); - }; + // Can only be run if the payment has been confirmed + if (payment.state != null) { + if (payment.OK && payment.state) { + progress = false; // no more modifications + price.put("change", payment.change); + receipt.payment(price); + }; + }; -} + receipt.finish(); + }; +};