Inventory reorder in Visual Basic
A complete program: filter low stock, price the order, total it.
' Flag stockroom items running low and price up the reorder.
Module InventoryReorder
Structure Item
Public Name As String
Public OnHand As Integer
Public Minimum As Integer
Public UnitCost As Double
End Structure
Sub Main()
Dim shelf = {
New Item With {.Name = "bolts", .OnHand = 40,
.Minimum = 100, .UnitCost = 0.08},
New Item With {.Name = "hinges", .OnHand = 220,
.Minimum = 80, .UnitCost = 1.15},
New Item With {.Name = "handles", .OnHand = 12,
.Minimum = 50, .UnitCost = 2.4}
}
Dim orders = shelf.
Where(Function(i) i.OnHand < i.Minimum).
Select(Function(i) New With {
.Name = i.Name,
.Units = i.Minimum * 2 - i.OnHand,
.Cost = .Units * i.UnitCost
}).ToList()
For Each order In orders
Console.WriteLine(
$"{order.Name,-10}{order.Units,5} {order.Cost,8:F2}")
Next
Console.WriteLine($"total: {orders.Sum(Function(o) o.Cost):F2}")
End Sub
End Module
How it works
- An array literal of
New Item With {...}seeds the stockroom. - The anonymous type's
.Costreuses its own.Unitsmember. Sumwith a lambda totals the order in one call.
Keywords and builtins used here
DimDoubleEachEndForIntegerItemMainModuleNewNextPublicSelectStringStructureSubWith
The run, in numbers
- Lines
- 35
- Characters to type
- 856
- Tokens
- 205
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 164 seconds.
Step 2 of 2 in Encore, step 29 of 29 in Language basics.