将成员对象添加到ArrayList <membership>

时间:2015-10-16 17:24:52

标签: java object arraylist

我正在尝试完成一个程序,以帮助我在课堂上学习之前学习java。但是我被困在试图向俱乐部添加会员,打印出来然后计算会员数量。在将基础知识添加到代码中之后,我不知道从哪里开始。

我是堆叠溢出的新手,不知道如何正确格式化,所以很抱歉。 :) 任何提示和帮助将不胜感激。 谢谢

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.");
}
}

2 个答案:

答案 0 :(得分:1)

你的代码实际上还可以。您真正需要的是开始执行代码的主要方法。创建一个新类,例如以下Demo.java

public class Demo {
    public static void main(String[] args) {
        ClubDemo demo = new ClubDemo();
        demo.demo();
    }
}

输出:

The club has 2 members.

您会在ClubDemo demo()方法中看到通过club.join添加成员的成员,这些成员调用ArrayList.add方法将成员对象添加到{ {1}} members

有关ArrayList方法签名的详细信息,请参阅Java Hello World Tutorial

  

在Java编程语言中,每个应用程序都必须包含一个   签名为:

的主要方法      

public static void main(String [] args)

     

修饰符public和static可以按任意顺序编写(公共   静态或静态公共),但惯例是使用公共静态   如上所示。您可以将参数命名为任何您想要的,但大多数   程序员选择&#34; args&#34;或&#34; argv&#34;。

答案 1 :(得分:0)

main我认为你可以致电Club club = new Club()来创建一个新的Club对象,然后只需致电club.join(member1)即可将新成员添加到俱乐部。

我建议更改方法名称join,因为当您阅读代码时,CLUB加入会员并不是真的正确。它应该是相反的,所以除非你需要,我只是取出方法并只使用ArrayList的添加。

最后,如果您想使用join方法(而不是ArrayList&#39; s add),我会考虑在members中设置Club为是私人的。