Description of all existing class members from another class

What I want to do I'm a complete newbie to Java, starting yesterday with the basics. I installed Eclipse and I am trying to create a School Principal menu that can list all students and teachers in the school (and also add new ones, but that will come later). So far I want to show only existing students, because I have not created any teachers.

What I have At the moment I have created my class of students: (If you see something wrong or bad form, let me know!)

public class Student {

    String name;
    int age;
    String program;

    public Student(String StudentName){
        this.name = StudentName;
    }

    public void PrintInfo(){
        System.out.println(name + " is a " + age +" year old student in " + program);
    }

    public static void main(String[] args) {
    }
}

And I also have this menu class, which first requires students to settle:

import java.util.Scanner;
public class Menu {

    public static void populate(){
        Student s01 = new Student("David");
        s01.age = 12;
        s01.program = "Elementary School";
        s01.PrintInfo(); //******I would like to remove this part******

        Student s02 = new Student("Alex");
        s02.age = 5;
        s02.program = "Kindergarten";
        s02.PrintInfo(); //******I would like to remove this part******
    }

  public static void main(String[] args) {
      Menu.populate();
    System.out.println("Hello!");
    System.out.println("For a list of students, press 1");
    System.out.println("For a list of teachers, press 2");

    Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.println("Enter a number: ");
    int n = reader.nextInt(); // Scans the next token of the input as an int.

    System.out.println("You Entered: " + n);
    if (n==1){
        System.out.println("Here is a list of the students:");

   //******I would like to move the printing here******

    }else if (n==2){
        System.out.println("Here is a list of the teachers:");
    }
  }
}

, , , . , void populate. , s01.PrintInfo(); main(), s01. ?

David is a 12 year old student in Elementary School  // I want this //
Alex is a 4 year old student in Kindergarten         //   And this  //
Hello!                                                              //
For a list of students, press 1                                     //
For a list of teachers, press 2                                     //
Enter a number:                                                     //
1                                                                   //
You Entered: 1                                                      //
Here is a list of the students:                                     //
               //here <---------------------------------------------//
+4
4

, List

public static List<Student> populate(){
    Student s01 = new Student("David");
    s01.age = 12;
    s01.program = "Elementary School";


    Student s02 = new Student("Alex");
    s02.age = 5;
    s02.program = "Kindergarten";

    List<Student> students = new ArrayList<Student>();
    students.add(s01);
    students.add(s02);
    return students;
}

:

if (n==1){
    System.out.println("Here is a list of the students:");

    //******I would like to move the printing here******
    List<Student> students = Menu.populate();
    for(Student student: students) {
        student.PrintInfo();
    }
} else if (n==2){
    System.out.println("Here is a list of the teachers:");
}
+5

populate Student:

   public static List<Student> populate(){

            List<Student> students = new ArrayList<Student>();
            Student s01 = new Student("David");
            s01.age = 12;
            s01.program = "Elementary School";

            students.add(s01);

            Student s02 = new Student("Alex");
            s02.age = 5;
            s02.program = "Kindergarten";

            students.add(s02);

            return students;
        }

main:

List<Student> allStudents= Menu.populate();
//.... SNIP
System.out.println("Here is a list of the students:");

for(Student student : allStudents){
 student.PrintInfo();
}
+4

:

  • public static void main(String[] args) , .
  • Java , PrintInfo PrintInfo

, , , , ArrayList.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Menu {

    public static List<Student> students = new ArrayList<Student>();

    public static void populate() {
        Student s01 = new Student("David");
        s01.age = 12;
        s01.program = "Elementary School";
        students.add(s01) // Adds student 01 to the list

        Student s02 = new Student("Alex");
        s02.age = 5;
        s02.program = "Kindergarten";
        students.add(s01) // Adds student 02 to the list
    }

    public static void main(String[] args) {
        Menu.populate();
        System.out.println("Hello!");
        System.out.println("For a list of students, press 1");
        System.out.println("For a list of teachers, press 2");

        Scanner reader = new Scanner(System.in);  // Reading from System.in
        System.out.println("Enter a number: ");
        int n = reader.nextInt(); // Scans the next token of the input as an int.

        System.out.println("You Entered: " + n);
        if (n==1) {
            System.out.println("Here is a list of the students:");

            for (Student student : students) { // iterate over all the students
                student.printInfo(); // prints the info of every individual student
            }
        } else if (n==2) {
            System.out.println("Here is a list of the teachers:");
        }
    }
}

, .

+3

s01 main(), Student , . populate().

, :

= > . :

  import java.util.Scanner;  
  public class Menu {

        public static Student s01;
        public static Student S02;

        public static void populate(){
            s01 = new Student("David");
            s01.age = 12;
            s01.program = "Elementary School";
            s01.PrintInfo(); //******I would like to remove this part******

            s02 = new Student("Alex");
            s02.age = 5;
            s02.program = "Kindergarten";
            s02.PrintInfo(); //******I would like to remove this part******
      }

      public static void main(String[] args) {
          Menu.populate();
          // call s01 and s02 in you main method
      }
}

= > populate() , , ( , ).

+3

All Articles