Search code examples
javacounterenumeration

How to Create a Counter that Counts Enumerated Types?


public class HackHornets_Roster
{
    enum MembershipStat {CoPresident, Secretary, Treasurer, RegMember,Sponser}

    public static void main (String[] args)
    {

        final int CoPresidents = 2;

        // declare members
        MembershipStat ChaseW;
        MembershipStat Saif;
        MembershipStat MrLee;
        MembershipStat Rahil;
        MembershipStat Donovon;
        MembershipStat Elliot; 
        MembershipStat Armaan;
        MembershipStat JacksonM;
        MembershipStat Cyrus;
        MembershipStat Avery;
        MembershipStat PeterM;
        MembershipStat Mahdi;
        MembershipStat GeorgeW; 

        // assign Member Roles
        ChaseW = MembershipStat.CoPresident;
        Saif = MembershipStat.CoPresident;
        MrLee = MembershipStat.Sponser;
        Rahil = MembershipStat.Secretary;
        Donovon = MembershipStat.RegMember;
        Armaan = MembershipStat.RegMember;
        Elliot = MembershipStat.RegMember; // Marching Band 
        JacksonM = MembershipStat.Treasurer;
        Cyrus = MembershipStat.RegMember; // sports
        Avery = MembershipStat.RegMember; // sports 
        PeterM = MembershipStat.RegMember; // sports 
        Mahdi = MembershipStat.RegMember;
        GeorgeW = MembershipStat.RegMember;



        System.out.println("Chase Williams: " + ChaseW);
        System.out.println("Saif Ali: " + Saif);


    }
}

Is there a way that I can create a counter in java that counts the number of people assigned to MembershipStat.RegMember, and MembershipStat.CoPresident, etc? Basically a way to tell me how many Regmembers there are, etc


Solution

  • Adapt this to all your needs.

    public int checkFor(MembershipStat[] ms, MembershipStat check){
        int x = 0;
        for(MembershipStat m : ms){
            if(m.equals(check))x++;
        }
        return x;
    }
    

    I'm not sure how you are setting up the roster names, it may be beneficial to use a Map/HashMap set up. It would look a little like this for your example:

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    
    public class Test1 {
        enum MembershipStat {CoPresident, Secretary, Treasurer, RegMember,Sponser}
        public static void main (String[] args)
        {
    
            final int CoPresidents = 2;
    
    
    
            Map<String, MembershipStat> roster = new HashMap<String, MembershipStat>();
            roster.put("ChaseW", MembershipStat.CoPresident);
            roster.put("Saif", MembershipStat.CoPresident);
            roster.put("MrLee", MembershipStat.Sponser);
            roster.put("Rahil", MembershipStat.Secretary);
            roster.put("Donovon", MembershipStat.RegMember);
            roster.put("Armaan", MembershipStat.RegMember);
            roster.put("Elliot", MembershipStat.RegMember);
            roster.put("JacksonM", MembershipStat.Treasurer);
            roster.put("Cyrus", MembershipStat.RegMember);
            roster.put("Avery", MembershipStat.RegMember);
            roster.put("PeterM", MembershipStat.RegMember);
            roster.put("Mahdi", MembershipStat.RegMember);
            roster.put("GeorgeW", MembershipStat.RegMember);
    
            System.out.println(checkFor(roster, MembershipStat.RegMember));
    
    
        }
        public static int checkFor(Map<String, MembershipStat> roster, MembershipStat check){
            int x = 0;
            Iterator<MembershipStat> it = roster.values().iterator();
            while(it.hasNext()){
                MembershipStat y = it.next();
                System.out.println(y);
                if(y.equals(check))
                    x++;
            }
            return x;
        }
    }
    

    Feel free to use and ask quesions! :D