Drill

ProblemsJava › billing

Sum the invoice line items

easybillingArraysMathJava

An invoice lists items with quantity and unit price. Compute the total across all lines.

invoiceLineTotal(items: list<LineItem>) → 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 invoiceLineTotal(List<LineItem> items) {
    
}

Worked examples

CallResult
invoiceLineTotal(Main.<LineItem>ls(new LineItem("Widget", 3, 500)))1500
invoiceLineTotal(Main.<LineItem>ls(new LineItem("A", 2, 1000), new LineItem("B", 1, 300)))2300
invoiceLineTotal(Main.<LineItem>ls())0
invoiceLineTotal(Main.<LineItem>ls(new LineItem("X", 0, 999)))0

Hint

Walk the list, multiply each line, and add.

Reference solution in Java
int invoiceLineTotal(List<LineItem> items) {
    int total = 0;
    for (LineItem item : items) total += item.qty * item.unitPrice;
    return total;
}

The same problem in another language

More billing problems in Java