jeudi 20 octobre 2016

Can a composite pattern be implemented using list in java?

I want a composite pattern to be implemented where the object emp will be added to tl and then tl added to pm. My list should only contain only the pm object, in which the rest of the objects stored sequentially.

But when I iterate and try to print this it displays only the values of pm and not the other two object values.

In this, under one pm object I need two tl objects and under one tl I need three emp objects.

List<Entity> ls = new ArrayList<Entity>();
    Entity pm = new Entity();
    Entity tl = new Entity();
    Entity emp = new Entity();
    int i = 0, j = 0, k = 0;
    for (i = 1; i <= 3; i++) {
        emp = new Entity("Associate"  + i, "Salem", 7563214, 3);
        // ls.add(obj);
        for (j = 1; j <= 2; j++) {
            tl = new Entity("TL" + j, "Chennai", 4756921, 2);
            tl.adds(emp);
            // ls.add(obj);

            for (k = 1; k <= 3; k++) {
                pm = new Entity("PM"+ k, "Erode",  5693214, 1);
                pm.adds(tl);
                ls.add(pm);                 
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire