'Modelling a School in Java

I have been trying to model a school in Java(no main method, just attributes). How do I assign 30 students and a teacher to a classroom?( is it possible to do without a main method?)

public class Person{
  private String name;
  private String surname;
  private String age;
  private String job;
}

public class Teacher extends Person{
  private String job = "teacher";
}

public class Student extends Person{
  private String job = "student";
}

private class Classroom{

  //  1 teacher and max. 30 students per classroom

}

My best guess: Using Arrays?

private Student[] students = new Student[30]; // there can be less students so would this be okay?
private Teacher classroomTeacher;

One last question, is using "extends" to create teacher and student class a good practice? If not, how can I improve it? If so, should Person,Student,Teacher be in the same file or seperated?

public class School{
  private enum schoolTypes{
    ArtSchool,
    ScienceSchool,
    SportsSchool,
  }
  private Classroom classrooms;
  
}

If I were to create a School class and specified the number of students a class can have, would I use ArrayLists or would Arrays be enough?



Solution 1:[1]

You have the basic idea down, but I would consider the following:

You're mixing implementation patterns. I would either use an enum to mark a job (not a string) or use subclasses, but not both simultaneously. I would lean more towards the subclassing myself; just ditch the job field, it's unnecessary.

As for the student list, I would subclass ArrayList and simply override add and addAll to check the size of itself and throw an Exception when trying to add more than the limit.

Your classroom then becomes something like:

public class Classroom {
    private Teacher teacher;
    private List<Student> students;

    public Classroom(Teacher teacher, List<Student> students) {
         this.teacher = teacher;
         this.students = new LimitedList(students, 30); //class you should make
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan