updated currency and items handling
Currency is now stored as a float, but this doesn't resolve the long number issue.
This commit is contained in:
parent
a2d30d0586
commit
b077cbf9ed
1 changed files with 46 additions and 31 deletions
|
@ -17,7 +17,8 @@ import java.util.*;
|
|||
*/
|
||||
public class transact {
|
||||
public static Dictionary<String, Dictionary> items = new Hashtable<>();
|
||||
public static Dictionary<String, Double> price = new Hashtable<>();
|
||||
public static Dictionary<String, Float> price = new Hashtable<>();
|
||||
public static Boolean state = false;
|
||||
|
||||
/*
|
||||
{item: {
|
||||
|
@ -41,16 +42,27 @@ public class transact {
|
|||
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);
|
||||
price.put("total", (float) 0.00);
|
||||
price.put("tax", (float) 0.00);
|
||||
price.put("subtotal", (float) 0.00);
|
||||
price.put("discount", (float) 0.00);
|
||||
|
||||
// Initialize as well the receipt.
|
||||
receipt.init();
|
||||
receipt.create();
|
||||
};
|
||||
|
||||
public static Boolean check() {
|
||||
/* Check the status of the transaction.
|
||||
|
||||
Returns: (Boolean) the transaction state */
|
||||
|
||||
state = !items.isEmpty();
|
||||
|
||||
// Return the state.
|
||||
return(state);
|
||||
}
|
||||
|
||||
public static Dictionary calculate() {
|
||||
/* Calculate the subtotal, total, and VAT.
|
||||
|
||||
|
@ -61,31 +73,31 @@ public class transact {
|
|||
Enumeration<String> 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);
|
||||
price.put("total", (float) 0.00);
|
||||
price.put("subtotal", (float) 0.00);
|
||||
price.put("discount", (float) 0.00);
|
||||
price.put("tax", (float) 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"));
|
||||
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", (double) price.get("subtotal") + (double) ((Dictionary) items.get(item).get("price")).get("subtotal"));
|
||||
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", (double) price.get("total") + (double) ((Dictionary) items.get(item).get("price")).get("total"));
|
||||
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", (double) price.get("discount") + (double) ((Dictionary) items.get(item).get("price")).get("discount"));
|
||||
price.put("discount", (float) price.get("discount") + (float) ((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"));
|
||||
if (Float.parseFloat(price.get("tip").toString()) >= 0) {
|
||||
price.put("total", (float) price.get("total") + (float) price.get("tip"));
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -93,41 +105,41 @@ public class transact {
|
|||
return(price);
|
||||
};
|
||||
|
||||
public static Dictionary<String, Double> calculate(String SKU, int QUANTITY) {
|
||||
public static Dictionary<String, Float> calculate(String SKU, int QUANTITY) {
|
||||
inventory.refresh();
|
||||
Dictionary item_details = inventory.items.get(SKU);
|
||||
|
||||
// Create an output dictionary.
|
||||
Dictionary<String, Double> item_pricing = new Hashtable<>();
|
||||
Dictionary<String, Float> 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);
|
||||
item_pricing.put("subtotal", (float) 0.00);
|
||||
item_pricing.put("total", (float) 0.00);
|
||||
|
||||
if (item_details != null) {
|
||||
if (item_details.get("Price") != null) {
|
||||
item_pricing.put("unit", Double.valueOf(item_details.get("Price").toString()));
|
||||
item_pricing.put("subtotal", Double.valueOf(item_details.get("Price").toString()) * QUANTITY);
|
||||
item_pricing.put("total", Double.valueOf( item_pricing.get("subtotal")));
|
||||
item_pricing.put("unit", Float.valueOf(item_details.get("Price").toString()));
|
||||
item_pricing.put("subtotal", Float.valueOf(item_details.get("Price").toString()) * QUANTITY);
|
||||
item_pricing.put("total", Float.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()));
|
||||
item_pricing.put("tax", Float.valueOf(item_details.get("Tax").toString()) * QUANTITY);
|
||||
item_pricing.put("total", Float.valueOf(item_pricing.get("total").toString()) + Float.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))
|
||||
QUANTITY * Float.parseFloat(item_details.get("Price").toString()) * (1 - (Float.parseFloat(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))
|
||||
QUANTITY * Float.parseFloat(item_details.get("Price").toString()) * (0 - (Float.parseFloat(item_details.get("Discount").toString()) / 100))
|
||||
);
|
||||
} else {
|
||||
item_pricing.put("discount", (double) 0);
|
||||
item_pricing.put("discount", (float) 0);
|
||||
};
|
||||
|
||||
item_pricing.put("total", Double.valueOf(item_pricing.get("total").toString()) - Double.valueOf(item_pricing.get("discount").toString()));
|
||||
item_pricing.put("total", Float.valueOf(item_pricing.get("total").toString()) - Float.valueOf(item_pricing.get("discount").toString()));
|
||||
};
|
||||
};
|
||||
};
|
||||
|
@ -211,17 +223,20 @@ public class transact {
|
|||
if (ITEM_CURRENT_DATA != null) {
|
||||
if (QUANTITY >= (int) ITEM_CURRENT_DATA.get("qty")) {
|
||||
items.remove(SKU);
|
||||
|
||||
// Log the details.
|
||||
log(SKU, -((int) ITEM_CURRENT_DATA.get("qty")));
|
||||
} 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);
|
||||
|
||||
// Log the details.
|
||||
log(SKU, -QUANTITY);
|
||||
};
|
||||
|
||||
// Log the details.
|
||||
log(SKU, -QUANTITY);
|
||||
|
||||
// Calculate all prices.
|
||||
calculate();
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue