1

I have Course class

public class Course implements Comparable<Course > {
private String nazwa;
private Integer lata;
private Teacher teacher;
private Student[] students;


public Course (String name, int years, int maxStudents, Teacher teacher) {
    this.name = name;
    this.years = years;
    students = new Student[maxStudents];
    this.teacher = teacher;

}

I'm not sure if it's proper way to do it

public void setTeacher(Teacher teacher)
{
    this.teacher = teacher;
}

Then I have some code that allow user to create new Course. I first ask for some basic informations

System.out.print("ask for name");
String name= scan.next();
System.out.print("ask for years");
int years= scan.nextInt();
System.out.print("ask for maxStudents");
int maxStudents = scan.nextInt();

There is my try to add teacher to new course. Teacher have unique ID

Course course;
System.out.print("Choose teacher:\n");

teachers is a list, that containts all teachers

for(Teacher t : teachers)
{
System.out.print(t.getName() + " - " + t.getAcademicDeggre() +"\n");
}

course.setTeacher(Teacher ?);

courses.add(new Course(name, years, maxStudents, teacher?);

@Solution:

int choice= scan.nextInt() - 1;
String ID= teachers.get(choice).getID();
Teacher teacher = getTeacherForCourse(teachers, ID);
G. Dawid
  • 162
  • 1
  • 11
  • use the constructor approach – Lino May 21 '19 at 20:25
  • `setTeacher` is a setter method which will enable you to set teacher anytime after the `Course` object is created. If you want to create an object with teacher every time, make it part of constructor – Albatross May 21 '19 at 20:43
  • Yea, but how to take teacher with specific ID from teachers list and put it into Course constructor `courses.add(new Course(name, years, maxStudents, teacher));` – G. Dawid May 22 '19 at 16:49

2 Answers2

1
  1. Define the relation between teacher and course
    • You add the teacher to the course, but for me the course can't exist without a teacher, so the teacher should have a list of courses (but this maybe is out of scope)
  2. Get the information for the new course from user
  3. Identify the teacher for course
  4. Create the course

Step 3

private Teacher getTeacherForCourse(List<Teacher> teachers, long id) {
    for(Teacher teacher : teachers)
    {
        // Return the teacher if match the criteria
        if(teacher.getId() == id)
        {
            return teacher;
        }
    }
    return null; // or throw exception
}

Or with java 8

private Optional<Teacher> getTeacherForCourse(List<Teacher> teachers, long id) {
    return teachers.stream()
            .filter(teacher -> teacher.getId() == id)
            .findFirst();
}

Step 4

Teacher teacher = getTeacherForCourse(teachers, 9454);
courses.add(new Course(name, years, maxStudents, teacher);

Basically you can set attributes at creation via constructor

courses.add(new Course(name, years, maxStudents, teacher));

Or after creation by setters

Course course = new Course();

course.setName(name);
course.setYears(years);
course.setMaxSturdents(maxStudents);
course.setTeacher(teacher);

courses.add(course);

Or a combination

Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
  • One thing I don't know is, how could I take teacher from teachers list? For example, I'm creating course named Math and I want to set teacher from teachers list, whos ID is 9454 for example and give it as course attribute – G. Dawid May 22 '19 at 15:55
  • 1
    @G.Dawid I've updated the code, just iterate through list and save the teacher that match your selection criteria – Butiri Dan May 22 '19 at 17:47
0

Last question, why .getID is ignored? Teacher have ID attribute thats string.

I'm printing every teacher

for(int i = 0; i < teachers.size(); i++){
   System.out.print(i + teachers.get(i).getName() + " " + teachers.get(i).getSurName() + " - " + teachers.get(i).getAccademicDeggre());
  }

Then user pick teacher, by number, he want to add to course

int choise = scan.nextInt();

And next I want to get ID from teacher that has that index in list

teachers.get(choise).getID();
G. Dawid
  • 162
  • 1
  • 11
  • The code looks good, maybe the ID was not set. You can also print the ID `System.out.print(i + " " + teachers.get(i).getID() + teachers.get(i).getName() + " " + teachers.get(i).getSurName() + " - " + teachers.get(i).getAccademicDeggre());`. If the ID is missing, check where the teacher is created to see if the ID is set properly. – Butiri Dan May 23 '19 at 11:47
  • ID is a string that contains 11 integer fe. String ID = "12345678900"; – G. Dawid May 23 '19 at 20:39
  • You have 2 options: 1) Get the `ID` as `String` then compare it with `equals` (ex: `idFromUser.equals(teachers.get(index).getID())`. 2) Get the `ID` as `long` and then convert the `teachers.get(i).getID()` to long (ex: `long teacherId = Long.parseLong(teachers.get(i).getID())` -> `teacherId == idFromUser`). ***Note*** you can read the following article for a better understanding of [==, .equals(), compareTo(), and compare()](https://www.leepoint.net/data/expressions/22compareobjects.html). Conclusion you must have the same type and know what you compare (primitives, Strings, Objects etc) – Butiri Dan May 24 '19 at 06:41