From 12e4576463b596c77af5703773211c63ed60a982 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 25 Feb 2024 16:20:12 +0800 Subject: [PATCH] import and modify inventory handling --- .../tech/iBeans/POSware/Lite/inventory.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/main/java/tech/iBeans/POSware/Lite/inventory.java diff --git a/src/main/java/tech/iBeans/POSware/Lite/inventory.java b/src/main/java/tech/iBeans/POSware/Lite/inventory.java new file mode 100644 index 0000000..3932550 --- /dev/null +++ b/src/main/java/tech/iBeans/POSware/Lite/inventory.java @@ -0,0 +1,124 @@ +/* + * 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 tech.iBeans.POSware.Lite.data_test.*; + +import java.util.*; + +/** + * + * @author eleven + */ +public class inventory { + + public static Dictionary items = new Hashtable<>(); + + public static boolean refresh() { + /* Refresh the inventory. + + Returns: (bool) the refresh state + */ + // Pull the sample items. + items = data_test.fake_items; + return (true); + }; + + + public static Object get(String SKU, String PROPERTY) { + /* Get the information of a particular SKU. + + Parameters: + (String) SKU: the SKU of the product to look for + (String) PROPERTY: the property in search + Returns: (Object) the corresponding value + */ + + Dictionary PROPERTY_EXISTS = new Hashtable<>(); + + // Find the SKU. This isn't the most efficient method but will do for the purposes of lab. + Enumeration items_SKU = items.keys(); + PROPERTY_EXISTS.put("SKU", false); + do { + String item_SKU = items_SKU.nextElement(); + PROPERTY_EXISTS.put("SKU", item_SKU.equals(SKU)); + } while (items_SKU.hasMoreElements() && (!(PROPERTY_EXISTS.get("SKU")))); + + // Stop when the SKU is not found. + if (!PROPERTY_EXISTS.get("SKU")) { + return (null); + }; + + // Find the property within the element. + Enumeration item_properties = items.get(SKU).keys(); + do { + String item_property = item_properties.nextElement(); + PROPERTY_EXISTS.put("Property", item_property.equals(PROPERTY)); + if (PROPERTY_EXISTS.get("Property")) { + break; + }; + } while (item_properties.hasMoreElements()); + + if (PROPERTY_EXISTS.get("Property")) { + return (items.get(SKU)).get(PROPERTY); + }; + + return (null); + }; + + public static ArrayList list() { + /* List all inventory items. + + Returns: (ArrayList) The array list + */ + + ArrayList items_SKU = new ArrayList<>(); + Enumeration items_SKU_raw = items.keys(); + do { + String item_SKU = items_SKU_raw.nextElement(); + items_SKU.add(item_SKU); + } while (items_SKU_raw.hasMoreElements()); + + return(items_SKU); + }; + + public static ArrayList list(String PROPERTY) { + ArrayList items_data = new ArrayList<>(); + ArrayList items_SKU = list(); + + for (String item_SKU : items_SKU) { + Object item_data = ""; + try { + item_data = items.get(item_SKU).get(PROPERTY); + } catch (Exception e) {System.out.println("Exception occurred: " + e.getMessage());}; + + if (item_data == null) {item_data = "";}; + items_data.add(item_data); + }; + + return(items_data); + }; + + public static ArrayList collate() { + /* Collate the names, replacing unknown names with their SKU. + + Returns: (ArrayList) the array list of names + */ + + ArrayList items_names_display = list("Name"); + ArrayList items_SKU = list(); + for (int item_rank = 0; item_rank < items_names_display.size(); item_rank++) { + if (items_names_display.get(item_rank).isBlank()) { + items_names_display.set(item_rank, items_SKU.get(item_rank)); + }; + }; + + return(items_names_display); + + } + + static { + refresh(); + } +};