javaoopinheritanceenumsclass-design

Class design for a scenario: inheritance vs enum


I want to represent the following scenario in java code:

A professor works in an office and can take a paid leave to carry out some scholarly activities outside of the university. A professor has a title and is enrolled in one department. A professor can be a full professor, an associate professor, or an assistant professor. An assistant professor has a minimum number of courses. Full professors, associate professors, and assistant professors can all take an unpaid leave. The unpaid leave of an assistant professor should not last more than three months.

I have used inheritance to represent the classes:

class Professor {
    String title;
    Department department;
}

class AssociateProfessor extends Professor {
}

class AssistantProfessor extends Professor {
}

class FullProfessor extends Professor{
}

An alternative would be to use an enum for professor type like:

enum professor {full, associate}` 

I am not sure which one is the best approach.
Also there are few requirements in the given scenario:

  1. professor can take a paid leave
  2. assistant professor has a minimum number of courses
  3. some professors can take an unpaid leave
  4. unpaid leave of an assistance professor should not last more than three months

I am not sure how to represent them in classes. But I have made an attempt here:

class Professor {
    String title;
    Department department;
    int paidLeaves;
}


class AssistantProfessor extends Professor {
    int numOfCourses;
    int unpaidLeaves;

    AssistantProfessor(int _numOfCourses,...) {
        if(_numOfCourses <10)
            throw new Exception("Need a minimum number of courses");
        if(unpaidLeaves >90)
            throw new Exception("...");
    }
}

In the above, I am not sure if paid or unpaid leaves should be treated as separated as a separate class or interface or enum and if there is a better way to represent the line not last more than three months. Any thoughts would be great


Solution

  • Your narrative explains that there are different kind of professors with different behaviors and properties:

    There are certainly other candidate design as well (I see at least two others). Now up to you to choose what best suits your needs.

    In any of these scenarios, you may document some additional constraints such as "not last more than three months" with a constraint in the diagram. THe constraint an be expressed in natureal language between {}