I am stuck on these questions (I am a beginner at Java):
In class ClubDemo, include membership number data where you add the records in the demo() method and In class Club write a new method printAllMembers(). This should use a for-each loop to access each Membership object in turn. Print individual member's records using the toString() method. Add a call to this method in the demo() method in ClubDemo.
This is the code I have so far
Code :
/** * Provide a demonstration of the Club and Membership * classes. */ public class ClubDemo { // instance variables - replace the example below with your own private Club club; /** * Constructor for objects of class ClubDemo */ public ClubDemo() { club = new Club(); } /** * Add some members to the club, and then * show how many there are. * Further example calls could be added if more functionality * is added to the Club class. */ public void demo() { club.join(new Membership("Dave", 2, 2004, 34)); club.join(new Membership("Michael", 1, 2004 , 35)); club.join(new Membership("India", 2, 2004 , 36)); club.join(new Membership("Kim", 3, 2005 , 37)); club.join(new Membership("Eliisa", 2, 2004 , 38)); club.join(new Membership("Robeto", 1, 2004 , 39)); System.out.println("The club has " + club.numberOfMembers() + " members."); ; } }
Code :
/** * Store details of club memberships. */ public class Club { // Define any necessary fields here ... ArrayList<Membership> members; /** * Constructor for objects of class Club */ public Club() { // Initialise any fields here ... 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(); } }
Code :
/** * Store details of a club membership. */ public class Membership { // The name of the member. private String name; // The month in which the membership was taken out. private int month; // The year in which the membership was taken out. private int year; private int membershipNumber; /** * Constructor for objects of class Membership. * @param name The name of the member. * @param month The month in which they joined. (1 ... 12) * @param year The year in which they joined. */ Membership(String name, int month, int year , int membershipNumber) { if(month >= 1 && month <= 12) { this.name = name; this.month = month; this.year = year; this.membershipNumber = membershipNumber; } else { System.out.println ("Invalid month (1 ... 12): " + month); } } /** * @return The member's name. */ public String getName() { return name; } /** * @return The month in which the member joined. * A value in the range 1 ... 12 */ public int getMonth() { return month; } /** * @return The year in which the member joined. */ public int getYear() { return year; } public int getmembershipNumber() { return membershipNumber; } /** * @return A string representation of this membership. */ public String toString() { return "Name: " + name + " joined in month " + month + " of " + year + membershipNumber; } }