Drill

ProblemsJava › inventory

What the shelf is worth

easyinventoryArraysMathJava

Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.

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.

Solve it in Python →

Where you start

int stockValue(List<Row> rows) {
    
}

Worked examples

CallResult
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;
}

The same problem in another language

More inventory problems in Java