I have two classes Person and Company, where the associations specify like:
(1) person work for a company
(2) person(boss) manages person(worker) by rating.
Details is provided in this image:
In case of first association i did this:
class Person {
Company co;
String name, ssn, addr;
}
class Company {
String name, addr;
}
class WorksFor {
Person person;
Company com;
int sal;
String title;
}
Is it correct implementation? also i am confused about second association. Please help me with your valuable advise.
It is not.
You should know that if class have associetion with something it is like it has attribute that type with given cardinality.
So for example Person would have given fields (omitting association classes):
String name[1]
String ssn[1]
String addr[1]
Company company[1]
Person boss[0..*]
Person worker[1..*]
Then how to change those to java:
String name;
String ssn;
String addr;
Company company;
List<Person> bosses;
List<Person> worker;
But remember if there is required number of given type you should pass those elements in constructor. One thing important to mention: if in UML value is omitted it means [1]. So we need to have constructor that takes worker as argument.
With association classes things getting more complicated: You should create classeslike
class Performance {
Person boss;
Person worker;
int performanceRating;
public Performance(Person boss, Person worker, int performanceRating){
this.boss = boss;
this.worker = worker;
this.performanceRating = performanceRating;
}
}
And in person change those list of Persons to list of Performance.
Still it's invalid UML diagram so I'm not perfectly sure if my answer can helps.