Recalculate list of linked objects












1















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?










share|improve this question



























    1















    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?










    share|improve this question

























      1












      1








      1








      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?










      share|improve this question














      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?







      c# .net linked-list reference






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 15:00









      daewooshdaewoosh

      38110




      38110
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414844%2frecalculate-list-of-linked-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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.






            share|improve this answer




























              2














              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.






              share|improve this answer


























                2












                2








                2







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 15:12









                KonamimanKonamiman

                42.8k1598127




                42.8k1598127
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414844%2frecalculate-list-of-linked-objects%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    Npm cannot find a required file even through it is in the searched directory

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith