重新计算链接对象列表

时间:2018-11-21 15:00:53

标签: c# .net linked-list reference

我有某种财务报告,其中每一行都是某个对象,取决于上一个对象。我需要获取这些对象的列表。在计算每一行时,我需要分析其值并在需要时进行一些修复。

这是我的ReportRow类:

public class ReportRow 
{
    public ReportRow (ReportRow  previousRow)
    {
        PreviousRow = previousRow;
    }
    public ReportRow PreviousRow;

    private decimal? _bankRest;
    public decimal BankRest
    {
        get
        {
            if (!_bankRest.HasValue)
                _bankRest = PreviousRow.BankRest - CurrentInvestments;
            return _bankRest.Value;

        }
        set
        {
            _bankRest = value;
        }
    }
    public decimal CurrentInvestments => Sum1 + Sum3;
    public decimal Sum1 { get; set; }//here is some formula
    public decimal Sum3 { get; set; }//here is some other formula
}

该课程非常简化,但是我认为它可以帮助您理解问题。 在这种情况下,CurrentInvestments可能会变得太大而BankRest会变成负数。 (CurrentInvestments的公式更复杂,并且可能变得更大)。

这是我收集报告数据的方式。

public void GetReport()
{
    ReportRow firstRaw = GetFirstRow();//here i somehow get first row to start calculation
    List<ReportRow> report = new List<ReportRow>(); //the full report
    ReportRow previous = firstRaw;
    for (int i = 0; i < 100000; i++)
    {
        ReportRow next = new ReportRow(previous);
        AnalizeAndFix(next);//Here is where I have a problem
        report.Add(next);
        previous = next; //current becomes previous for next iteration 
    }
}

我在AnalizeAndFix(next)函数中遇到问题。 如果当前期间的BankRest为负(<0),则需要取消上一个期间的CurrentInvestments(使CurrentInvestments = 0,这会使先前的BankRest变大)。如果这样做没有帮助,我需要向上两步并取消该行的投资。然后检查currentRawBankRest。我需要重复6次。如果将CurrentInvestments设置为先前六个原始值中的0,则无济于事-引发异常。在总报告中,涉及到的所有期间都需要更新。我只修复了前一行,但是我不知道该如何修复5次以上。 这是我的AnalizeAndFix的代码

private void AnalizeAndFix(ref ReportRow current)
    {
        if (current.BankRest < 0)
        {
            int step = 0;
            while (current.BankRest < 0 || step < 6)
            {
                step++;
                var previous = current.PreviousRow;
                previous.CancellInvestments(); //I set CurrentInvestments to 0
                current = new ReportRow(previous); // create a new report based on recalculated previous
            }
        }
    }

如果我使用此方法,则最终报告将显示我重新计算的当前行和先前行(我认为)。但是,我不知道该如何再执行5次,因此所有受影响的行都会在最终报告中显示更改。 还是我需要重新考虑整个系统并做其他事情需要我做些其他事情?

1 个答案:

答案 0 :(得分:2)

您应从ReportRow类中删除链接列表功能,而应使用the LinkedList class。此类提供了用于添加/删除节点的内置方法(a node是一个对象,其中包含您的行之一以及指向上一个和下一个的指针),并导航到上一个和下一个节点。

相关问题