Is this implementation of the UML diagram correct?
Im preparing for exams, our professor did not give us a solution to these tasks because we are supposed to be active ourselves but I'd still like to have feedback.
My Code:
public abstract class Employee {
protected String name;
protected int salery;
public Employee(){
name="";
salery=0;
}
public String toString(){
return "name: " name "salery: " salery;
}
}
import java.util.ArrayList;
public class EmployeeList {
protected ArrayList<Employee> members;
public EmployeeList(){
members=new ArrayList<Employee>();
}
public String toString(){
String ret="";
for(int i=0;i<members.size();i ){
ret =members.get(i).toString() " ";
}
return ret;
}
}
public class Manager extends Employee{
protected String department;
public String toString(){
return department;
}
}
The Code seems to work in practice because I've tested it with:
EmployeeList employeeList=new EmployeeList();
Manager m1=new Manager();
Manager m2=new Manager();
Manager m3=new Manager();
m1.department="McDonalds";
m2.department="BigHouse";
m3.department="Heart";
System.out.println(m1);
employeeList.members.add(m1);
employeeList.members.add(m2);
employeeList.members.add(m3);
System.out.println(employeeList);
CodePudding user response:
You (should) lose some marks for a significant number of Java style errors and for misspelling salary
as salery
. Yes, these things do matter.
It is also debatable whether you should have declared the fields as private
and provided protected
getters and setters. It is not normal practice to put getters and setters into UML diagrams. They are kind of assumed. But it is (normally) bad practice in Java to declare non-private
fields.
Finally, Employee
should not be abstract
. (It doesn't even make sense that you can only create Manager
objects and not Employee
objects.)
In practice, the Java classes (or whatever) do not need to be literal translations of the UML. UML is a design notation ... not a specification language.
CodePudding user response:
Normally, italicized text is used to indicate that a class should be abstract. Is there any reason why Employee is abstract?
Otherwise, this seems like a fair implementation.