tags:

views:

252

answers:

1

ok, im new to this, but basically i have this problem, and im confused on how to go about it, any help would be great, sorry about not posint this before!.

    The compilation was successful
The output should have been:
    The club had 6 members.
    and 4 had joined in March 2008
    There are now just 2 members left.

This is what was actually produced:
    The club had 6 members.
    and 3 had joined in March 2008
    There are now just 3 members left.

import java.util.ArrayList;
/**
 * Store details of club memberships.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Club
{
   private ArrayList<Membership> members;

    /**
     * Constructor for objects of class Club
     */
    public Club()
    {
        members = new ArrayList<Membership>();
    }

    /**
     * Add a new member to the club's list of members.
     * @param member The member object to be added.
     */
    public void join(Membership member)
    {
         members.add(member);
    }

    /**
     * @return The number of members (Membership objects) in
     *         the club.
     */
    public int numberOfMembers()
    {
        return members.size();
    }

    /** Determine the number of members who
 *  joined in a given month
 *  @param month The month we are interested in
 *  @return The number of members joining in month.
 */

public int joinedInMonth(int month)
    { int number;
        number = 0;
        for(int i= 0; i < members.size();i++)
        {
            if (members.get(i).getMonth() == month)
            {
                number= number + 1;
            }
        }
        if (month < 1 || month > 12)
        {
            System.out.println("error");
        }
        return number;
    }

    /** Remove from the club's collection all members who
 *  joined in the given month, and return them 
 *  stored in a separate collection object.
 *  @param month The month of membership
 *  @param year  The year of membership
 *  @return The members who joined in the given month and 
 *  year  */

public  ArrayList<Membership> purge(int month, int year)
    { 
        ArrayList<Membership> purge = new ArrayList<Membership>();
        int i=members.size() -1;
        int numj=0;
        while (i >= 0)
        {
            Membership temp = members.get(i);
            if (temp.getMonth() == month)
            {
                purge.add(temp);
                members.remove(temp);
                i --;
                numj ++;
            }
            else
            {
                i --;
            }
        }
        return purge;
    }
}
A: 

I think for the purge method you would be a lot better off reading up on how to use the for loop with an Iterator. If you use iterators then you can use the .remove() method as you scan the list, so the loop will look something like:

for (Iterator<Membership> iter = members.iterator(); iter.hasNext()) {
  Membership value = iter.next();
  if ( equalYear and Month ) {  // fix this line to real code
    purge.add(value);
    iter.remove();
  }
}

Much clearer and simpler than the counter/index style you are using and it sounds like you have a subtle bug with the counter anyway.

DaveC
any chance of help - to explain about the error that i am recieving?