From 1423eba2a9e6d36124aeb54bfb607dfe4f0ab755 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Tue, 27 Feb 2024 01:20:39 +0800 Subject: [PATCH] improve transaction handling --- .../tech/iBeans/POSware/Lite/transact.java | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 src/main/java/tech/iBeans/POSware/Lite/transact.java diff --git a/src/main/java/tech/iBeans/POSware/Lite/transact.java b/src/main/java/tech/iBeans/POSware/Lite/transact.java new file mode 100644 index 0000000..553e0bf --- /dev/null +++ b/src/main/java/tech/iBeans/POSware/Lite/transact.java @@ -0,0 +1,206 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package tech.iBeans.POSware.Lite; + +// Import local modules. +import tech.iBeans.POSware.Lite.inventory.*; + +// Import global modules. +import java.util.*; + +/** + * + * @author eleven + */ +public class transact { + public static Dictionary items = new Hashtable<>(); + public static Dictionary price = new Hashtable<>(); + + /* + {item: { + qty: + price: { + "tax": + "subtotal": + "total": + "discount": + } + } + } + */ + + + public static void init() { + /* Initialize the variables, setting them with default values. + */ + + items = new Hashtable<>(); + price = new Hashtable<>(); + + // Set default values for prices. + price.put("total", 0.00); + price.put("tax", 0.00); + price.put("subtotal", 0.00); + price.put("discount", 0.00); + }; + + public static Dictionary calculate() { + /* Calculate the subtotal, total, and VAT. + + Returns: (Dictionary) the contents of the price variable + */ + + // Prepare to enumerate each item. + Enumeration items_list = items.keys(); + + // Set default values for prices. + price.put("total", 0.00); + price.put("subtotal", 0.00); + price.put("discount", 0.00); + price.put("tax", 0.00); + + while (items_list.hasMoreElements()) { + String item = items_list.nextElement(); + + if (((Dictionary) items.get(item).get("price")).get("tax") != null) { + price.put("tax", (double) price.get("tax") + (double) ((Dictionary) items.get(item).get("price")).get("tax")); + }; + if (((Dictionary) items.get(item).get("price")).get("subtotal") != null) { + price.put("subtotal", (double) price.get("subtotal") + (double) ((Dictionary) items.get(item).get("price")).get("subtotal")); + }; + if (((Dictionary) items.get(item).get("price")).get("total") != null) { + price.put("total", (double) price.get("total") + (double) ((Dictionary) items.get(item).get("price")).get("total")); + }; + if (((Dictionary) items.get(item).get("price")).get("discount") != null) { + price.put("discount", (double) price.get("discount") + (double) ((Dictionary) items.get(item).get("price")).get("discount")); + }; + }; + + if (price.get("tip") != null) { + if (Double.parseDouble(price.get("tip").toString()) >= 0) { + price.put("total", (double) price.get("total") + (double) price.get("tip")); + }; + }; + + // Return the dictionary. + return(price); + }; + + public static Dictionary calculate(String SKU, int QUANTITY) { + inventory.refresh(); + Dictionary item_details = inventory.items.get(SKU); + + // Create an output dictionary. + Dictionary item_pricing = new Hashtable<>(); + + // Fill in with default data, especially useful when the product is not found. + item_pricing.put("subtotal", 0.0); + item_pricing.put("total", 0.0); + + if (item_details != null) { + if (item_details.get("Price") != null) { + item_pricing.put("subtotal", Double.valueOf(item_details.get("Price").toString()) * QUANTITY); + item_pricing.put("total", Double.valueOf( item_pricing.get("subtotal"))); + + if (item_details.get("Tax") != null) { + item_pricing.put("tax", Double.valueOf(item_details.get("Tax").toString()) * QUANTITY); + item_pricing.put("total", Double.valueOf(item_pricing.get("total").toString()) + Double.valueOf(item_pricing.get("tax").toString())); + }; + if (item_details.get("Discount") != null) { + if (Integer.parseInt(item_details.get("Discount").toString()) > 0) { + item_pricing.put("discount", + QUANTITY * Double.parseDouble(item_details.get("Price").toString()) * (1 - (Double.parseDouble(item_details.get("Discount").toString()) / 100)) + ); + } else if (Integer.parseInt(item_details.get("Discount").toString()) < 0) { + item_pricing.put("discount", + QUANTITY * Double.parseDouble(item_details.get("Price").toString()) * (0 - (Double.parseDouble(item_details.get("Discount").toString()) / 100)) + ); + } else { + item_pricing.put("discount", (double) 0); + }; + + item_pricing.put("total", Double.valueOf(item_pricing.get("total").toString()) - Double.valueOf(item_pricing.get("discount").toString())); + }; + }; + }; + + // Return the data. + return item_pricing; + + }; + + public static boolean add(String SKU, int QUANTITY) { + /* Add an item to the shopping cart. + Parameters: + SKU (String): the SKU of the item + QUANTITY (int): the quantity + Returns: (boolean) the calculation state + */ + + if (QUANTITY == 0) {return(false);} + else if (QUANTITY < 0) {return(remove(SKU, QUANTITY));}; + + // containing the current data's information + Dictionary ITEM_CURRENT_DATA = items.get(SKU); + + // check if the item exists + if (ITEM_CURRENT_DATA != null) { + ITEM_CURRENT_DATA.put("qty", ((int) ITEM_CURRENT_DATA.get("qty")) + QUANTITY); + } else { + // Initialize the dictionary. + ITEM_CURRENT_DATA = new Hashtable<>(); + + // Add the quantity. + ITEM_CURRENT_DATA.put("qty", QUANTITY); + }; + + // Calculate these items' price. + ITEM_CURRENT_DATA.put("price", calculate(SKU, ((int) ITEM_CURRENT_DATA.get("qty")))); + + // Add to the shopping cart's items. + items.put(SKU, ITEM_CURRENT_DATA); + + // Calculate all prices. + calculate(); + + // Return the success state. + return((items.get(SKU) != null)); + }; + + public static boolean remove(String SKU, int QUANTITY) { + /* Remove an item to the shopping items. + + Parameters: + SKU (String): the SKU of the item + QUANTITY (int): the quantity + Returns: (boolean) the removal state + */ + + if (QUANTITY == 0) {return(false);} + else if (QUANTITY < 0) {return(add(SKU, QUANTITY));}; + + // containing the current data's information + 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) { + if (QUANTITY >= (int) ITEM_CURRENT_DATA.get("qty")) { + items.remove(SKU); + } else { + ITEM_CURRENT_DATA.put("qty", ((int) ITEM_CURRENT_DATA.get("qty")) - QUANTITY); + ITEM_CURRENT_DATA.put("price", calculate(SKU, ((int) ITEM_CURRENT_DATA.get("qty")))); + + // Add to the shopping cart's items. + items.put(SKU, ITEM_CURRENT_DATA); + }; + }; + + // Calculate all prices. + calculate(); + + // Return the state. + return(true); + }; +}