I'm trying to output a list of all the clubs along with the people entered for each club, but have it display each club individually (i.e. there is one club and the list of people for that club, then the second club entered and the people for that one and so on)
I wanted to make sure the adding of objects to each array was correct and figure out what my toString()
method should look like.
Here's my code so far:
public class app {
public static Club[] clubArray = new Club[5];
public static int clubCount=0;
public static int personCount=0;
public static void main(String[] args) {
//inside a add method
//prompt user for club
clubArray[clubCount++] = new Club(clubName);
//prompt user for name, then prompt for Male or Female
if (x.equals("M")) {
Male newPerson = new Male(name);
clubArray[clubCount-1].addPerson(newPerson,personCount);
personCount++;
}
}
//data definition class
public class Club { //extend app?
public static Person[] personArray = new Person[200];
public void addPerson(Person newPerson, int personCount){
personArray[personCount] = newPerson;
}
}
The personArray should not be static. If it is static all the Club instances will share the same array of people. It is better to use List instead of static arrays.
Class Person :
Class App :
You can use a nested for loop
I am not sure if your classes are set up this way, but you get the idea. Just iterate through 1 club at a time in the outer for loop, and for the inner loop iterate through each person in that club.