wisenhiemer
wisenhiemer

Reputation: 155

How to extract 'for' loop Method?

I am attempting to extract my 'for' loop in order to call it in printPartyBreakdownInHouse() method. Basically removing any chance of duplicate code or "code smell" as well as my printdetails() method, but when doing this I'm getting '2' as a result. Any ideas?

public void printPartyBreakdownInSenate() {
    int numDemocrats = 0;
    int numRepblican = 0;
    int numIndepent = 0;

    String senateData;
    ArrayList<MemberOfCongress> members;


    senateData = CongressDataFetcher.fetchSenateData(congressNum);
    members = parseMembersOfCongress(senateData);

    for (MemberOfCongress party : members){
            if (party.getParty().equals("D")){
                numDemocrats++;
            }
            else if (party.getParty().equals("R")){
                numRepblican++;
            }
            else if (party.getParty().equals("I")){
                numIndepent++;
           }
   }
   pintDetails(numIndepent, numIndepent, numIndepent, numIndepent, members);
}

/**
 * printDetails method
 */
void pintDetails(int congressNum, int numDemocrats, int numRepblican, int numIndepent, ArrayList<MemberOfCongress> members){
    System.out.println("Number of Members of the Senate of the " +  congressNum + " Congress : " + members.size() );
    System.out.println("# of Democrats: " + numDemocrats);
    System.out.println("# of Republican: " + numRepblican);
    System.out.println("# of Indep: " + numIndepent);
}

/**
 * Calculate and print the number of Democrats, Republicans, and Independents in this House
 */
public void printPartyBreakdownInHouse(){
    String houseData;
    ArrayList<MemberOfCongress> members;
    houseData = CongressDataFetcher.fetchHouseData(congressNum);
    members = parseMembersOfCongress(houseData);
}

Upvotes: 1

Views: 511

Answers (3)

tomse
tomse

Reputation: 501

You could use a method which accepts the members list as parameter and returns the member counts via an additional helper class as follows:

class MemberCounter {
  int numDemocrats;
  int numRepblican;
  int numIndepent;
};

MemberCounter countMembers(List<MemberOfCongress> members) {
  MemberCounter counter = new MemberCounter();

  for (MemberOfCongress party : members) {
    if (party.getParty().equals("D")) {
      counter.numDemocrats++;
    }
    else if (party.getParty().equals("R")){
      counter.numRepblican++;
    }
    else if (party.getParty().equals("I")){
      counter.numIndepent++;
    }
  }

  return counter;
}

Upvotes: 0

Jeremy Barnes
Jeremy Barnes

Reputation: 677

The line

pintDetails(numIndepent, numIndepent, numIndepent, numIndepent, members);

should read

pintDetails( [VARIABLE NOT IN CODE], numDemocrats, numRepblican, numIndepent, members);

Upvotes: 1

Ron Kuper
Ron Kuper

Reputation: 847

Your error is here:

pintDetails(numIndepent, numIndepent, numIndepent, numIndepent, members);

You are printing numIndepent 4 times...?

Upvotes: 1

Related Questions