Dynamic object creation from user input

Milan C.

I have a two classes, Student containing some basic info, and Course, containing some basic info and an ArrayList of Student objects. I want to dynamically (from user input) populate new instances of both classes. For example, user is prompted for course name, teacher and asked if they want to add students to the course. When done with the course and all the students' information, the loop goes back to asking to add another course.
What I do is, create a Course object and then add students. I manage Student objects by just creating them after collecting user unput with courseInstance.addStudent(new Student(name, age, phone)). It all works well for one course, but how do I manage multiple Course object, dynamically created from input?
Here is a code example:

    public static void main(String[] args) {
    // TODO code application logic here
    Course course = new Course();
    Scanner in = new Scanner(System.in);
    String ans;
    String name;
    String gender;
    String phone;
    int age;

    System.out.print("Enter course name: ");
    ans = in.nextLine();
    course.setName(ans);

    System.out.print("Enter teacher name: ");
    ans = in.nextLine();
    course.setTeacher(ans);

    while (true) {
        System.out.print("Add student (yes or exit): ");
        ans = in.nextLine();

        if (ans.equals("exit")) {
            break;
        }

        System.out.print("Enter name: ");
        name = in.nextLine();
        System.out.print("Enter age: ");
        age = in.nextInt();
        in.nextLine();
        System.out.print("Enter gender: ");
        gender = in.nextLine();
        System.out.print("Enter phone number: ");
        phone = in.nextLine();

        course.addStudent(new Student(age, name, phone, gender));
    }

    in.close();
    System.out.print(course);
}
Miquel

How about having a List of Course objects, and asking (in your while(true) loop) what course each student is registering to?

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

reading input from user in a loop

분류에서Dev

select attribute of object according what the user input?

분류에서Dev

how to fetch result from core data sorted by the object creation date?

분류에서Dev

How to return dynamic object from operator function?

분류에서Dev

how to extract rows of dataframe from user input

분류에서Dev

Script to call either from file or user input

분류에서Dev

I need to get from the user input random

분류에서Dev

Save and retrieve user input from database with javascript?

분류에서Dev

batch variable from user input treated differently

분류에서Dev

Dynamic creation of variables and modification of globals()

분류에서Dev

Producing dynamic/multiple input boxes to collect data depending on user selection in Shiny R

분류에서Dev

Create a excel from a dataframe if user puts certain input

분류에서Dev

Count the total number of names in a 'while loop' from user input, in Python

분류에서Dev

How add hosts from user's input in a ansible playbook?

분류에서Dev

edit a file line by line interactively from user input in python

분류에서Dev

jquery : How to bind change event when the input is not from the user

분류에서Dev

Debug assertion fail from initializing an array of a class by user input

분류에서Dev

Accessing a local variable whose name comes from user input

분류에서Dev

Issues with structure arrays and obtaining array string from user integer input

분류에서Dev

Create a program that takes input from the user and writes it into the output file

분류에서Dev

How to input value from user and save in variable wxDev-C++

분류에서Dev

Dynamic creation of element and class implementation in javascript

분류에서Dev

jq: generate JSON: dynamic key creation possible?

분류에서Dev

Uncaught TypeError: object is not a function on object creation

분류에서Dev

Meteor dynamic input fields

분류에서Dev

Get user input from input field in react similar to getElementById in react native using props

분류에서Dev

Using select() to receive from multiple peers using UDP sockets while taking user input from STDIN

분류에서Dev

Allowing a Devise / OmniAuth-Facebook user to complete their registration on user creation

분류에서Dev

django user login after successful user creation failed

Related 관련 기사

  1. 1

    reading input from user in a loop

  2. 2

    select attribute of object according what the user input?

  3. 3

    how to fetch result from core data sorted by the object creation date?

  4. 4

    How to return dynamic object from operator function?

  5. 5

    how to extract rows of dataframe from user input

  6. 6

    Script to call either from file or user input

  7. 7

    I need to get from the user input random

  8. 8

    Save and retrieve user input from database with javascript?

  9. 9

    batch variable from user input treated differently

  10. 10

    Dynamic creation of variables and modification of globals()

  11. 11

    Producing dynamic/multiple input boxes to collect data depending on user selection in Shiny R

  12. 12

    Create a excel from a dataframe if user puts certain input

  13. 13

    Count the total number of names in a 'while loop' from user input, in Python

  14. 14

    How add hosts from user's input in a ansible playbook?

  15. 15

    edit a file line by line interactively from user input in python

  16. 16

    jquery : How to bind change event when the input is not from the user

  17. 17

    Debug assertion fail from initializing an array of a class by user input

  18. 18

    Accessing a local variable whose name comes from user input

  19. 19

    Issues with structure arrays and obtaining array string from user integer input

  20. 20

    Create a program that takes input from the user and writes it into the output file

  21. 21

    How to input value from user and save in variable wxDev-C++

  22. 22

    Dynamic creation of element and class implementation in javascript

  23. 23

    jq: generate JSON: dynamic key creation possible?

  24. 24

    Uncaught TypeError: object is not a function on object creation

  25. 25

    Meteor dynamic input fields

  26. 26

    Get user input from input field in react similar to getElementById in react native using props

  27. 27

    Using select() to receive from multiple peers using UDP sockets while taking user input from STDIN

  28. 28

    Allowing a Devise / OmniAuth-Facebook user to complete their registration on user creation

  29. 29

    django user login after successful user creation failed

뜨겁다태그

보관