审查生成的发票导入器

来自 数据类型
C# 14 / .NET 10 高级 6分钟 找出 4处问题

在这个生成的发票导入器处理客户输入前,对它进行审查。

按固定区域格式解析十进制单价,要求数量存在,生成精确且检查溢出的分单位合计,并在返回合计时避免 object 装箱。

csharp
using System;
using System.Collections;
using System.Collections.Generic;
public static class InvoiceImporter
{
    public static object TotalCents(string? unitPrice, int? quantity)
    {
        double price = double.Parse(unitPrice!);
        int cents = (int)(price * 100);
        int total = unchecked(cents * quantity!.Value);
        return total;
    }
    public static ArrayList BuildTotals(
        IEnumerable<(string? Price, int? Quantity)> lines)
    {
        var totals = new ArrayList();
        foreach (var line in lines)
            totals.Add(TotalCents(line.Price, line.Quantity));
        return totals;
    }
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误