为会员显示不同的月份

时间:2018-06-29 11:17:20

标签: java display

我正在努力创建一种方法,当您输入特定月份(从1-12 )时,所有加入协会的会员 与该月的班级一起显示。例如,

我输入:3 (表示“ 3月”)

产出:15 (这表明15个成员在同一月份加入)

到目前为止,我只有一个打印输出方法,如果您在我的(joinedInMonth)方法中输入低于1或高于12的任何内容,都会给出错误消息,一些建议或帮助会很多感谢。

社会阶层:

public class Society
{
   private ArrayList<Member> myMembers;
   private Member member;
   private String societyName;
   public Society(String society)
   {
      myMembers = new ArrayList<Member>();
      this.societyName = society;
   }

   public String getSocietyName()
   {
     return societyName;  
   }

   public void join(Member member)
   {
      myMembers.add(member);
   }

   public void showMember(int listPosition)
   {
      Member member;
      if( listPosition < myMembers.size() )
      {
         member = myMembers.get(listPosition);
         System.out.println("Position " + listPosition  + ": " + member);
      }
   }

   public void joinedInMonth(int joined) {
        if (joined > 12 || joined < 1) {
            System.out.println("Invalid number");       
       }
   }

   public int numberOfMembers()
   {
      return myMembers.size();
   }

   public void printDetails()
   {
       for (Member m : myMembers) {
      System.out.println("Society Name: " + societyName);
      System.out.println(m.toString());
      System.out.println("--------------------------");
     } 

   }

会员等级:

public class Member
{
   private int month;
   private int year;
   private String name;
   private Society society;

   public Member(String name, int month, int year)
   {
      this.name  = name;
      this.month = month;
      this.year = year;
   }

   public int getYear()
   {
      return year;
   }

   public int getMonth()
   {
      return month;
   }

   public String getName()
   {
      return name;
   }

   public String toString()
   {
      return "Name: " + name + ", Month Joined: " + month + ", Year Joined:" + year;
   }
}

1 个答案:

答案 0 :(得分:0)

您可以查看成员并检查每个成员加入的月份:

public void joinedInMonth(int joinedMonth) {
    if (joinedMonth > 12 || joinedMonth < 1) {
         System.out.println("Invalid number");       
    }

    long numJoined = members.stream().filter(m -> m.getMonth() == joinedMonth).count();
    System.out.printf("%d members joined on month %d%n", numJoined, joinedMonth);
}