Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Describing all existing elements of a class from another class

Tags:

java

class

What I want to do I am a complete beginner in Java, having started yesterday with the basics. I installed Eclipse and I'm trying to make a School Principal menu that can give a list of all students and teachers in the school (and add new ones as well, but that's for later). For now I only want to show the existing students because I haven't created any teachers.

What I have For now I've created my student class: (If you see anything 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 have this menu class as well, that calls for student population first:

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:");
    }
  }
}

My Question Right now, the output is obviously that the students are printed BEFORE the Menu starts writing. That is because I am printing right in the populate void. The problem is that if I switch s01.PrintInfo(); to the main(), it doesn't recognize s01. How can I get the program to recognize it?

The current and desired outputs

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 <---------------------------------------------//
like image 766
David G Avatar asked Dec 06 '22 18:12

David G


1 Answers

Change your populate method to return a List of the students

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;
}

And in your main:

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:");
}
like image 134
pleft Avatar answered Dec 08 '22 07:12

pleft