What the shelf is worth
Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.
- Quantity and cost are both in whole units; the answer is in minor currency units.
- Negative quantities are counting errors — skip those rows entirely.
- An empty shelf is worth nothing.
stockValue(rows: list<Row>) → int
Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
int stockValue(List<Row> rows) {
}
Worked examples
| Call | Result |
|---|---|
stockValue(Main.<Row>ls(new Row(3, 250), new Row(2, 100))) | 950 |
stockValue(Main.<Row>ls(new Row(-4, 500), new Row(1, 500))) | 500 |
stockValue(Main.<Row>ls(new Row(0, 900))) | 0 |
stockValue(Main.<Row>ls()) | 0 |
Hint
One pass, one accumulator, one guard.
Reference solution in Java
int stockValue(List<Row> rows) {
int total = 0;
for (Row r : rows) if (r.qty > 0) total += r.qty * r.unitCost;
return total;
}