rtya
rtya

Reputation: 21

How do I create new objects in a switch

I'm new to Java and I am trying to create a program where you can sign up members to a club and i've hit a wall when trying to add / create people.

I can't seem to figure out how to keep creating new objects of a class inside an if statement or switch. (Like lets say going through the switch and adding 10 new members).

public static void main(String[] args) throws NoSuchMethodException {
        System.out.println("Opties: \n" +
                           "1) Add a member to the club.");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        scanner.nextLine();

    do{
        switch(choice){
        case 1:
            System.out.println("New member name: ");
            String newMemberName = scanner.nextLine();

            System.out.println("New member age: ");
            int newMemberAge = scanner.nextInt();
            scanner.nextLine();

            System.out.println("""
                    New member category:\s
                    A) Player
                    B) Coach""");
            String categoryChoice = scanner.nextLine();

            if (categoryChoice.equalsIgnoreCase("a")){


            } else if(categoryChoice.equalsIgnoreCase("b")){


            } else break;

        case 2:
            ///temp
        }
      } while(choice != 3);

And this is the class + subclass

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

class Player extends Person {

    public Player(String name, int age) {
            super(name, age);
        }
    }

class Trainer extends Person {

     public Trainer(String name, int age) {
            super(name, age);
        }
}

Upvotes: 0

Views: 369

Answers (1)

Amir MB
Amir MB

Reputation: 3418

You should not create the variable inside an if statement, you should create the variable before hand with a default value null then change the variable's value inside each if statement with the proper object:

public static void main(String[] args) throws NoSuchMethodException {
    System.out.println("Opties: \n" +
                           "1) Add a member to the club.");
    Scanner scanner = new Scanner(System.in);
    int choice = scanner.nextInt();
    scanner.nextLine();

    Person person = null; //needs to be the super class

    do{
        switch(choice){
        case 1:
            System.out.println("New member name: ");
            String newMemberName = scanner.nextLine();

            System.out.println("New member age: ");
            int newMemberAge = scanner.nextInt();
            scanner.nextLine();

            System.out.println("""
                    New member category:\s
                    A) Player
                    B) Coach""");
            String categoryChoice = scanner.nextLine();

            if (categoryChoice.equalsIgnoreCase("a")){
                person = new Player(newMemberName, newMemberAge);
            } else if(categoryChoice.equalsIgnoreCase("b")){
                person = new Trainer(newMemberName, newMemberAge);
            } else break;

        case 2:
            ///temp
        }
    } while(choice != 3);
}

Since you are creating objects inside a loop it would make more sense to create an ArrayList<Person> instead of a single variable and during each iteration you add a new object to it:

    ArrayList<Person> people = new ArrayList<Person>(); //needs to be the super class - an empty list

   do { ...
       if (categoryChoice.equalsIgnoreCase("a")){
                people.add(new Player(newMemberName, newMemberAge));
       } else if(categoryChoice.equalsIgnoreCase("b")){
                people.add(new Trainer(newMemberName, newMemberAge));
       } else break;
   } while(choice != 3);

Upvotes: 1

Related Questions