Reputation: 39
I am trying to complete a program to help me learn java before we do it in class. But I am stuck trying to add a Member to the club, print it out and then count the number of members. I do not know where to start after I have added the basics into the code.
I am new to stack overflow and do not know how to format correctly so I am sorry. :) Any tips and help would be greatly appreciated. Thanks
package lab8.club;
import java.util.ArrayList;
public class Club
{
ArrayList<Membership> members;
/**
* Constructor for objects of class Club
*/
public Club()
{
members = new ArrayList<Membership>();
// Initialise any fields here ...
}
/**
* 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();
}
public static void main(String args[]){
Membership member1 = new Membership("test", 1, 2011);
System.out.println();
}
}
package lab8.club;
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;
public Membership(String name, int month, int year)
throws IllegalArgumentException
{
if(month < 1 || month > 12) {
throw new IllegalArgumentException(
"Month " + month + " out of range. Must be in the range 1 ... 12");
}
this.name = name;
this.month = month;
this.year = year;
}
public String getName()
{
return name;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public String toString()
{
return "Name: " + name +
" joined in month " +
month + " of " + year;
}
}
package lab8.club;
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("David", 2, 2004));
club.join(new Membership("Michael", 1, 2004));
System.out.println("The club has " +
club.numberOfMembers() +
" members.");
}
}
Upvotes: 0
Views: 3035
Reputation: 1375
In main
I think you can just call Club club = new Club()
to create a new Club object, then simply call club.join(member1)
to add your new member into the club.
I would suggest changing the method name join
because when you're reading the code, it's not really right that the CLUB is joining a MEMBER. It should be the other way around so unless you need to, I'd just take out the method and just use the ArrayList's add.
And lastly, if you want to use your join
method (and not the ArrayList's add
), I would consider setting members
in Club
to be private.
Upvotes: 0
Reputation: 3656
Your code is actually ok. What you really need is a main method to start the execution of your code. Create a new class such as the following Demo.java
:
public class Demo {
public static void main(String[] args) {
ClubDemo demo = new ClubDemo();
demo.demo();
}
}
Output:
The club has 2 members.
You will see that in ClubDemo
's demo()
method that members are added via club.join
which calls the ArrayList.add
method to add member objects to the members
ArrayList
.
For more information about the main
method signature, see the Java Hello World Tutorial:
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
Upvotes: 1