在这个生成的发票合计实现处理客户输入前,对它进行审查。
解析十进制单价,要求数量非空,精确计算以分为单位的合计,并拒绝任何超出支持范围的输入或总额。
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;
}
}
生成代码仅作示例,不代表任何特定模型