Waddle_Language/examples/example_inventory_class.wdl

103 lines
3 KiB
Plaintext
Raw Permalink Normal View History

2025-11-23 06:58:46 +00:00
use Inventory.Base.InventoryInterface;
use Inventory.Base.PartsInventory;
Inventory/PartsInventory -> InventoryInterface {
String<> partNames.set(<>);
Integer<> partQuantities.set(<>);
Decimal<> partPrices.set(<>);
+fn on_create() -> [Null] {
// inventory initialized
}
+fn on_destroy() -> [Null] {
my.partNames.set(<>);
my.partQuantities.set(<>);
my.partPrices.set(<>);
}
-fn findPartIndex(partName:String) -> [Integer] {
Integer idx.set(-1);
my.partNames.each(name, i -> {
if (name.eq(partName)) {
idx.set(i);
}
});
rtn idx;
}
+fn addPart(partName:String, quantity:Integer, price:Decimal) -> [Boolean] {
Integer i.set(my.findPartIndex(partName));
if (i.gte(0)) {
Integer current.set(my.partQuantities.get(i));
my.partQuantities.remove(i);
my.partQuantities.add(current.add(quantity));
my.partPrices.remove(i);
my.partPrices.add(price);
rtn True;
} else {
my.partNames.add(partName);
my.partQuantities.add(quantity);
my.partPrices.add(price);
rtn True;
}
}
+fn removePart(partName:String, quantity:Integer) -> [Boolean] {
Integer i.set(my.findPartIndex(partName));
if (i.lt(0)) { rtn False; }
Integer current.set(my.partQuantities.get(i));
if (current.lt(quantity)) { rtn False; }
if (current.eq(quantity)) {
my.partNames.remove(i);
my.partQuantities.remove(i);
my.partPrices.remove(i);
} else {
my.partQuantities.remove(i);
my.partQuantities.add(current.subtract(quantity));
}
rtn True;
}
+fn getPartPrice(partName:String) -> [Decimal] {
Integer i.set(my.findPartIndex(partName));
if (i.lt(0)) { rtn 0; }
rtn my.partPrices.get(i);
}
+fn getTotalInventoryValue() -> [Decimal] {
Decimal total.set(0);
my.partNames.each(name, i -> {
Decimal value.set(my.partQuantities.get(i).multiply(my.partPrices.get(i)));
total.set(total.add(value));
});
rtn total;
}
+fn getPartDetails(partName:String) -> [String] {
Integer i.set(my.findPartIndex(partName));
if (i.lt(0)) { rtn ["Part not found"]; }
String details.set(
partName
.concat(" — Qty: ")
.concat(my.partQuantities.get(i).format())
.concat(", Price: $")
.concat(my.partPrices.get(i).format())
);
rtn [details];
}
-fn ensureNonNegative(quantity:Integer) -> [Boolean] {
if (quantity.lt(0)) { rtn False; }
rtn True;
}
fn audit_sample() {
// protected (no prefix) example: internal audit routine callable within package
String report.set("Inventory count: ".concat(my.partNames.length().format()));
rtn report;
}
}