Reputation: 323
I have some kind of financial report, where each row is some object that depends on the previous one. I need to get the list of these objects. When I calculate each row, I need to analize its values and make some fixes if needed.
Here is my ReportRow class:
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
}
This class is very simplified but, I think, it can help to understand the problem. Here can be a situation when CurrentInvestments can get too big and BankRest will become negative. (CurrentInvestments has more complex formula and can become extra large).
Here is how I collect report data.
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
}
}
I have a problem in AnalizeAndFix(next) function. If current period's BankRest is negative (<0) I need to cancel the previous period CurrentInvestments (make CurrentInvestments = 0, which will get previous BankRest larger). If this doesn't help, I need to go two steps up and cancel that row investments as well. Then check currentRaw BankRest. I need to repeat this 6 times. If setting CurrentInvestments to 0 of six previous raws didn't help - throw an exceptions. And in total report all periods that were touched need to be updated. I managed to fix only one previous row, but I don't know how to do it 5 more times. Here is code for my 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
}
}
}
If I use this aproach, the final report shows me recalculated current and previous rows as I want (I think). However I don't know how to do this 5 more times, so all affected rows will show changed in final report. Or may be I need to rethink the whole system and do the stuff I need some other way?
Upvotes: 1
Views: 48
Reputation: 50273
You should remove the linked list functionality from your ReportRow
class and use the LinkedList class instead. This class comes with built-in methods for adding/removing nodes (a node is an object that holds one of your rows plus pointers to the previous and next one) and to navigate to the previous and next node.
Upvotes: 2