Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:48:44+00:00 2026-05-30T14:48:44+00:00

Consider the following code: CLASS AuditProgressReport : public class AuditProgressReport { private List<AuditProgressReport> audit_progress_reports

  • 0

Consider the following code:

CLASS AuditProgressReport:

public class AuditProgressReport
{
    private List<AuditProgressReport> audit_progress_reports = null;

    private String name = null;
    private String description = null;

    private int compliant;
    private int non_compliant;
    private int not_completed ;

    /**
     * 
     */
    public AuditProgressReport()
    {
        super();
    }

    public AuditProgressReport(
        String name_param,
        int compliant_param,
        int non_compliant_param,
        int not_completed_param)
    {
        super();

        this.name = name_param;
        this.compliant = compliant_param;
        this.non_compliant = non_compliant_param;
        this.not_completed = not_completed_param;
    }

    public void addToCompliant(int compl_to_add_param)
    {
        this.compliant += compl_to_add_param;
    }

    public void addToNonCompliant(int non_compl_to_add_param)
    {
        this.non_compliant += non_compl_to_add_param;
    }

    public void addToNotCompleted(int not_compl_param)
    {
        this.not_completed += not_compl_param;
    }

    public void setAuditProgressReports(List<AuditProgressReport> report_category_nodes_param)
    {
        this.audit_progress_reports = report_category_nodes_param;
    }

    public List<AuditProgressReport> getAuditProgressReports()
    {
        return this.audit_progress_reports;
    }

    public void setCompliant(int compliantParam)
    {
        this.compliant = compliantParam;
    }

    public int getCompliant()
    {
        return this.compliant;
    }

    public void setNonCompliant(int nonCompliantParam)
    {
        this.non_compliant = nonCompliantParam;
    }

    public int getNonCompliant()
    {
        return this.non_compliant;
    }

    public void setNotCompleted(int notCompletedParam)
    {
        this.not_completed = notCompletedParam;
    }

    public int getNotCompleted()
    {
        return this.not_completed;
    }

    public void setName(String name_param)
    {
        this.name = name_param;
    }

    public String getName()
    {
        return this.name;
    }

    public void setDescription(String description_param)
    {
        this.description = description_param;
    }

    public String getDescription()
    {
        return this.description;
    }

    @Override
    public String toString()
    {
        return ("Compliant["+this.compliant+
            "] Non-Compliant["+this.non_compliant+
            "] Not-Completed["+this.not_completed+"]");
    }
}

And CLASS Tester:

public class Tester
{
    public static void main(String[] args)
    {

        List<AuditProgressReport> main_level = new ArrayList<AuditProgressReport>();

        AuditProgressReport ar_1_1 = new AuditProgressReport("ar_1_1",0,0,0);
        AuditProgressReport ar_1_2 = new AuditProgressReport("ar_1_2",0,0,0);

        AuditProgressReport ar_1_1_1 = new AuditProgressReport("ar_1_1_1",0,0,0);
        AuditProgressReport ar_1_1_2 = new AuditProgressReport("ar_1_1_2",15,65,20);
        AuditProgressReport ar_1_1_3 = new AuditProgressReport("ar_1_1_3",20,30,50);

        AuditProgressReport ar_1_1_1_1 = new AuditProgressReport("ar_1_1_1_1",5,5,90);
        AuditProgressReport ar_1_1_1_2 = new AuditProgressReport("ar_1_1_1_2",55,5,40);
        AuditProgressReport ar_1_1_1_3 = new AuditProgressReport("ar_1_1_1_3",35,35,30);

        List<AuditProgressReport> arl_1_1_1 = new ArrayList<AuditProgressReport>();
        arl_1_1_1.add(ar_1_1_1_1);
        arl_1_1_1.add(ar_1_1_1_2);
        arl_1_1_1.add(ar_1_1_1_3);

        ar_1_1_1.setAuditProgressReports(arl_1_1_1);

        List<AuditProgressReport> arl_1_1 = new ArrayList<AuditProgressReport>();
        arl_1_1.add(ar_1_1_1);
        arl_1_1.add(ar_1_1_2);
        arl_1_1.add(ar_1_1_3);

        AuditProgressReport ar_1_2_1 = new AuditProgressReport("ar_1_2_1",10,30,60);
        AuditProgressReport ar_1_2_2 = new AuditProgressReport("ar_1_2_2",20,20,60);



        List<AuditProgressReport> arl_1_2 = new ArrayList<AuditProgressReport>();
        arl_1_2.add(ar_1_2_1);
        arl_1_2.add(ar_1_2_2);

        ar_1_1.setAuditProgressReports(arl_1_1);

        ar_1_2.setAuditProgressReports(arl_1_2);

        main_level.add(ar_1_1);
        main_level.add(ar_1_2);


        Tester tester = new Tester();

        for(AuditProgressReport prog_rep : main_level)
        {
            tester.populateParents(prog_rep, null);
        }

        //TODO Now check the values...
    }

    private void populateParents(
        AuditProgressReport audit_progress_param,
        AuditProgressReport parent_param)
    {
        List<AuditProgressReport> audit_progress = 
            audit_progress_param.getAuditProgressReports();

        System.out.println("name["+audit_progress_param.getName()+"]");

        if(parent_param != null)
        {
            int compl = audit_progress_param.getCompliant();
            int nonCompl = audit_progress_param.getNonCompliant();
            int notCompleted = audit_progress_param.getNotCompleted();

            parent_param.addToCompliant(compl);
            parent_param.addToNonCompliant(nonCompl);
            parent_param.addToNotCompleted(notCompleted);
        }

        if(audit_progress != null && ! audit_progress.isEmpty())
        {
            for(AuditProgressReport prog_rep : audit_progress)
            {
                this.populateParents(prog_rep,audit_progress_param);
            }
        }
    }
}

When you run this, you will note that the values of the parent elements in the list is updated with the sum of the values in the child list.

The problem I am facing is that I want to have it updated all the way through the tree instead of just the immediate parent.

Is there a pattern that would help me achieve this?

See illustration below:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:48:46+00:00Added an answer on May 30, 2026 at 2:48 pm

    Like others suggested I would use the Observer pattern. Each parent node listens for changes on the childrens.

    But my solution differs from that of @zmf because if you have a a big tree with lot of children node and at each update you have to sum each value, you would spend a lot of processing time.

    What if you send only the difference between the old value and the new value each time you update a child node. Let’s make an example. You start with this tree:

    [12]--+--[10]-----[10]
          |
          +--[ 2]--+--[  ]
                   |
                   +--[ 2]
    

    and you update a children like this

    [12]--+--[10]-----[10]
          |
          +--[ 2]--+--[ 3]
                   |
                   +--[ 2]
    

    the node that gets updated with the value “3” send its change to the parent with the method call parent.updateNode(3). The parent have only to sum its current value (in this example “2”) with the value it receives from the child node. So it will update to the value “5”

    [12]--+--[10]-----[10]
          |
          +--[ 5]--+--[ 3]
                   |
                   +--[ 2]
    

    the node with the new value “5” will call parent.updateNode(3) and the final solution will be

    [15]--+--[10]-----[10]
          |
          +--[ 5]--+--[ 3]
                   |
                   +--[ 2]
    

    IMHO this solution is better because each updateNode() method have only to sum its own current value with the change received from its child node and call its parent with the same value received. You do not have to get the value from each one of your children and sum all the values. This will save you a lot of time if you have a big tree. So in this example when you change the value from 0 to 3. You will get 2 call to parent.updateNode(3) and each parent will get updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code:- class Name { {System.out.println(hi);} public static void main(String[] args) {
Consider following code public class City { public string Name { get { return
Consider the following piece of code: case class User(id: Int, name: String) object User{
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following block of code: class CheckStore { private String displayText; private boolean
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following code: class CustomClass { public CustomClass(string value) { m_value = value;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.