Review generated invoice total

from Data types
Java 25 LTS advanced 6 min 4 issues to find

Review this generated invoice-total implementation before it handles customer input.

Parse decimal unit prices, require a non-null quantity, calculate an exact cent total, and reject any input or total outside the supported range.

Java
import java.util.List;

record InvoiceLine(String unitPrice, Integer quantity) {}

class InvoiceTotals {
    static int totalCents(List<InvoiceLine> lines) {
        Integer total = 0;
        for (InvoiceLine line : lines) {
            double unitPrice = Double.parseDouble(line.unitPrice());
            int cents = (int) (unitPrice * 100);
            total += cents * line.quantity();
        }
        return total;
    }

    static boolean matches(Integer expected, Integer actual) {
        return expected == actual;
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error