我如何保存会员?

时间:2016-11-21 17:12:02

标签: java

我正在大学做一个项目,我们要创建一个java程序,将高尔夫球手添加到成员列表中。我无法将它们添加到列表中!

到目前为止,这是我的代码:

*import java.util.*;
public class GolfClubTester {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
            System.out.println("Please enter number for:");
            System.out.println("1: Joining Member:");
            System.out.println("2: Remove an old Member:");
            System.out.println("3: Search for a Member:");
            System.out.println("4: View all");
            //Ask User for input
            Scanner input = new Scanner(System.in);
            int i = input.nextInt();

                if(i == 1){
                    //Clear scanner input buffer
                    input.nextLine();
                    System.out.println("Please enter Members name:");
                    String name = input.nextLine();
                    System.out.println("Please enter day:");
                    int day = input.nextInt();
                    System.out.println("Please enter month");
                    int month = input.nextInt();
                    System.out.println("Please enter year:");
                    int year = input.nextInt();
                    System.out.println("Please enter Handicap:");
                    int Handicap = input.nextInt();
                    System.out.println("Please enter Id:");
                    int MemId = input.nextInt();

                    //Clear scanner input buffer
                    input.nextLine();
                    //Ask user type of Member
                    System.out.println("Please enter type of Member:");
                    System.out.println("1 - Senior:");
                    System.out.println("2 - Junior:");
                    int i1 = input.nextInt();
                        if (i1 == 1){
                           //Create member object
                           Date join1 = new Date(day,month,year);
                           SeniorMembers s1 = new SeniorMembers(name,join1,Handicap,MemId);
                           //Display member details
                           System.out.println("**Senior Member has joined**");
                           System.out.println(s1);
                           return;
                       }
                        else if (i1 == 2){
                            Date join1 = new Date(day,month,year);
                            JuniorMembers j1 = new JuniorMembers(name,join1,Handicap,MemId);
                            //Display member details
                            System.out.println("**Junior Member has joined**");
                            System.out.println(j1);
                        }
                        else{
                    throw new IllegalArgumentException("Invalid input:");

                     }
                }*

所以我可以说会员已加入,但实际上并没有保存会员。任何人都可以帮助并告诉我我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

ArrayList<SeniorMember> seniorMembersList = new ArrayList<>();
ArrayList<JuniorMember> juniorMembersList = new ArrayList<>();

public static void main(String[] args) {
    // TODO code application logic here
        System.out.println("Please enter number for:");
        System.out.println("1: Joining Member:");

        .......

                   if (i1 == 1){
                       //Create member object
                       Date join1 = new Date(day,month,year);
                       SeniorMembers s1 = new SeniorMembers(name,join1,Handicap,MemId);
                       //
                       //Adding here
                       //
                       seniorMembersList.add(s1);
                       //Display member details
                       System.out.println("**Senior Member has joined**");
                       System.out.println(s1);
                       return;
                   }
                    else if (i1 == 2){
                        Date join1 = new Date(day,month,year);
                        JuniorMembers j1 = new JuniorMembers(name,join1,Handicap,MemId);
                       //
                       //Adding here
                       //
                        juniorMembersList.add(j1);
                        //Display member details
                        System.out.println("**Junior Member has joined**");
                        System.out.println(j1);
                    }

然后,您可以使用seniorMembersList.get(position);

访问您需要的成员对象

答案 1 :(得分:0)

此外,我认为您可以从所有评论和答案中获得,您可以将实例存储在列表中(例如ArrayList<String>)。

由于您的程序采用main方法,因此列表应在此方法中实例化。

提示:当您进一步实施时,请使用更面向对象的方法。