Accessing arraylist in different class?

Go To StackoverFlow.com

0

Member class = super class SeniorMember class = sub class JuniorMember class = sub class

Is it possible to have the same arraylist in different class to store the members?

"Commiittee class" having an arraylist to add and remove only seniormember

"Society class" having an arraylist to store all member, with add and remove seniormember and juniormember.

Can they share the same arraylist? how is it done?

import java.util.*;

public class ManagementCommittee{

    private ArrayList<SeniorMember> smlist;

    public ManagementCommittee(){
        smlist = new ArrayList<SeniorMember>();
    }

    public void addCommitteeMember(SeniorMember sm){
        smlist.add(sm);
    }

    public SeniorMember search(String smName){
        for (SeniorMember sm : smlist){
            if ((sm.getName()).equals(smName)){
                return sm;
            }
        }
        return null;  
    }

    public boolean removeCommitteeMember(String smName){
        SeniorMember tmp = search(smName);
        if (tmp != null)
            return smlist.remove(tmp);
        else
            return false;
    }

    public String toString(){
        for (SeniorMember sm : smlist){
            return "Name : " + sm.getName() + "\nAddress : " + sm.getAddress() + "\nFee : " + sm.getFee();
        }
    }
}

How should I write for the society class to use the same arraylist in this committeemember class?

2012-04-04 07:13
by i-link
What do you mean by "Overall class having an arraylist"? Is this a superclass of Committee - Yuval 2012-04-04 07:16


0

static arraylist in the superclass

2012-04-04 07:18
by asdasd


0

If I understood you correctly, you're implementing a class to wrap around a collection and you'd like to shadow one of the collection handling methods. In that case, it should be something like:

import java.util.ArrayList;

public class HelloWorld {
    static class Member {
    }

    static class SeniorMember extends Member {
    }

    static class Committee {
        private ArrayList<Member> _members = new ArrayList<Member>();

        public void add(Member member) {
            _members.add(member);
        }
    }

    static class ManagementCommittee extends Committee {
        public void add(Member member) {
            if (!(member instanceof SeniorMember)) {
                throw new Error("Only senior members here");
            }
            super.add(member);
        }
    }

    public static void main(String[] args) {
        Member member = new Member();
        SeniorMember senior = new SeniorMember();
        Committee committee = new Committee();
        ManagementCommittee management = new ManagementCommittee(); 

        committee.add(member);
        committee.add(senior);

        management.add(member);
        management.add(senior); // throws a run-time error 
    }
}

Note that if you override the method add to accept a SpecialMember you'd still be exposing the superclass' add(Member) version.

2012-04-04 07:36
by Yuval
hmmm, I guess so, The society class is wrapping up all the member in the senior and junior member, only the committeemember class are wrapping the senior members. SO i need to do in the way that committee extends society - i-link 2012-04-04 07:56
Your phrasing is a bit confusing to me. You have a SocietyCommittee which holds all kinds of Members, and a ManagementCommittee class which holds only SeniorMembers but shares all functionality with the SocietyCommittee class? It sounds like a good reason to use ManagementCommittee extends SocietyCommittee, or at least to have them both extend a common base-class, e.g. Committee - Yuval 2012-04-04 08:09


0

I'd set up your Society class using ArrayList. Then make a Committee class that uses Society the way Society uses ArrayList. To get a list of Committee members, you'd get the full list from Society (which gets it from ArrayList) and keep only the committee members. Adding a committee member means making sure its in the Society instance, adding it if necessary, then getting hold of it and marking it as a committee member. There's a bit of busy work here, but it's straightforward. Also, you might want the Society and Committee classes to implement the same interface so other parts of your code need not know whether they are dealing with committee members, ordinary memebers, or even some other group entirely (but that does implement the interface).

If this is a really large society, you might want to maintain a separate list of committee members, but it needs to be synchronized. A Committee instance would need to have a Listener added to it's Society interface to catch all removals in case one is a committee member--for example. I don't think you want to bother with that just now.

2012-04-05 16:51
by RalphChapin
Ads