Drill

ProblemsC# › billing

Sum the invoice line items

easybillingArraysMathC#

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

InvoiceLineTotal(items: list<LineItem>) → int

C# 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

public int InvoiceLineTotal(List<LineItem> items) {
    
}

Worked examples

CallResult
InvoiceLineTotal(new List<LineItem> { new LineItem("Widget", 3, 500) })1500
InvoiceLineTotal(new List<LineItem> { new LineItem("A", 2, 1000), new LineItem("B", 1, 300) })2300
InvoiceLineTotal(new List<LineItem> { })0
InvoiceLineTotal(new List<LineItem> { new LineItem("X", 0, 999) })0

Hint

Walk the list, multiply each line, and add.

Reference solution in C#
public int InvoiceLineTotal(List<LineItem> items) {
    int total = 0;
    foreach (var item in items) total += item.Qty * item.UnitPrice;
    return total;
}

The same problem in another language

More billing problems in C#