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:
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
Your narrative explains that there are different kind of professors with different behaviors and properties:
«interface» Professor
and three classes FullProfessor
, AssociateProfessor
, and AssistantProfessor
each realizing (implementing) the interface.Professor
with an enum Category
that may take the values Full
, Associate
, and Assistant
. Professor
specialized in three classes FullProfessor
, AssociateProfessor
, and AssistantProfessor
. Professor
refers to a current State
, and forwards state-dependend behaviors to the state. Three specializations of of State
: StateFullProfessor
, StateAssociateProfessor
, and StateAssistantProfessor
. This is by the way the state design pattern, which prefers composition over inheritance. Advantage: The professor may change category without loosing the continuity of his/her identity.Also there is a separation of concerns between the behaviors and properties that are invariant for a professor, and those that change during his/her carrer.Professor
refers to a collection of Role
(or contracts?). Three specializations of of Role
: RoleFullProfessor
, RoleAssociateProfessor
, and RoleAssistantProfessor
. For every behavior the Professor forwards the bahavior to all its roles. 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 {}