In the class of Professor should be the probability of a roll call of several groups from other classes. I want to ask how this can be done correctly? I posted the code of two classes, although there are three of them. Hope this is enough.

public class Professor extends Group { Professor(Students[] studentsList) { super(studentsList); } @Override public Group process() { System.out.println("Проверка присутствия студентов:"); for (Students st : getStudentsInGroup()) { switch (st.getCall()) { case " ": System.out.println("Студент " + st.getStudentName() + " - отсутствует"); break; case "V": System.out.println("Студент " + st.getStudentName() + " - присустствует"); break; } } return null; } } public class Students{ private String StudentName; private String CandidatName; private String Call; public String getCall() { return Call; } public String getStudentName() { return StudentName; } public String getCandidatName() { return CandidatName; } Students(String studentName, String candidatName, String call) { this.StudentName = studentName; this.CandidatName = candidatName; this.Call = call; } static Students[] students = { new Students("Ануфриев Владимир", "Бевський Максим", "V"), new Students("Бевский Максим", "Ануфриев Владимир", "V"), new Students("Войтович Степан", "Бевский Максим", " "), new Students("Годун Александр", "Ануфриев Владимир", "V"), new Students("Дмитренко Степан", "Ануфриев Владимир", "V"), new Students("Ермолаев Иван", "Ануфриев Владимир", " "), new Students("Евсиков Игорь", "Бевский Максим", " "), new Students("Жебрак Алексей", "Бевский Максим", "V"), new Students("Забой Максим", "Ануфриев Владимир", "V"), new Students("Записоцкий Дмитрий", "Ануфриев Владимир", "V") }; } 
  • one
    Please add the code. It is not clear what you want. - Ares
  • Such a question, why do you need a static array of students? As I understand it, you have three entities: a student, a group and a professor. Can a professor check the presence of students from several groups? Right? - Ares
  • Yes. The point was that the professor's class method checks the array, not the group. And theoretically he should check several groups. static may not be needed, but the Group and Professor classes use an array of the Students class, but I don’t yet know how to make it publicly available. - Nikita
  • And how did it happen that a professor inherits a group?) Purely according to logic, it should be like this: there can be a lot of students in a group, the professor has a function to check presence, which accepts a variable number of groups. In this function, you enumerate all students from all groups. - Ares
  • Well, at this stage, I understand that the Professor can inherit only one class, which does not allow checking several groups ... And how can you implement a variable number of groups for the function of checking presence? How does it even approximately look like? - Nikita

1 answer 1

Change the architecture of your application, for example, so.

 public class Program { public static void main(String[] args) { Student student = new Student("Петров Иван Иванович", "Петров Юрий Петрович", true); Group group = new Group("101-M", new Student[] { student }); Lesson myLesson = new Lesson(new Group[] { group }); Professor professor = new Professor(); professor.checkPresence(myLesson); } } public class Student { private String _studentName; private String _candidatName; private boolean _isPresent; public boolean isPresent() { return _isPresent; } public String getStudentName() { return _studentName; } public String getCandidatName() { return _candidatName; } Student(String studentName, String candidatName, boolean isPresent) { this._studentName = studentName; this._candidatName = candidatName; this._isPresent = isPresent; } } public class Group { private String _groupNumber; private Student[] _students; public String getGroupNumber() { return _groupNumber; } public Student[] getStudents() { return _students; } Group(String groupNumber, Student[] students) { this._groupNumber = groupNumber; this._students = students; } } public class Professor { Professor() { } public void checkPresence(Lesson lesson) { System.out.println("Проверка присутствия студентов:"); for(Group group : lesson.getGroups()) { for(Student student : group.getStudents()) { if(student.isPresent()) System.out.println("Студент " + student.getStudentName() + " из группы " + group.getGroupNumber() + " присутствует"); else System.out.println("Студент " + student.getStudentName() + " из группы " + group.getGroupNumber() + " отсутствует. К сожалению:("); } } } } public class Lesson { private Group[] _groups; public Group[] getGroups() { return _groups; } Lesson(Group[] groups) { this._groups = groups; } } 
  • Thank. Everything works only in the method main you need to register Group group = new Group ("101-M", Students.students); Otherwise swears - Nikita
  • @Nikita, an array is also transferred there (though only 1 student). Everything should be great. Student class should not know how much, which students are created, and what’s more, it should not be a container for students :) - Ares
  • Nevertheless, curly braces in that line IntelijIdea does not accept, and everything else is generally excellent. It remains only to understand the meaning of this line. Lesson myLesson = new Lesson (new Group [] {group}); For me, this is something new - Nikita
  • @ Nikita, here I can only advise to teach :) - Ares
  • Not, I understand that a Group object with an array is created in the Lesson object. I just do not understand how this is related to the rest of the code. But nothing, experiment and understand) Thanks again. - Nikita