Ibra
Ibra

Reputation: 33

Need explanation of this UML model

enter image description here Please explain what "MANAGER static employee" means. I did everything except changeManager, how can I implement it ?

 public  Employee(){
    this.id=-1;
    this.year=-1;
    salary=-1; 
    name="NA";
    department="NA";

}
public  Employee(int id ,String name ,String depatment, int year,double salary){
    Employee Employee=new Employee();

    Employee.setid(id); Employee.setname(name); Employee.setdepartment(depatment); Employee.setyear(year); Employee.setsalary(salary);
}

Upvotes: 1

Views: 78

Answers (1)

bruno
bruno

Reputation: 32586

As in Java a static attribute in UML is an attribute of the class itself, rather than an attribute of instance, that means there is only one manager 'shared' by all the instances. Of course its type is Employee, so in Java :

private static Employee MANAGER;

by default the class has no manager because MANAGER is null.

Note the presence of 'static' in the diagram is abnormal and does not follow the norm, like for the operation changeManager the line for the attribute is underlined, that is the way to say an attribute/operation is static in UML

The definition of isManager is trivial :

public boolean isManager() { return this == MANAGER; }

Note may be the class does not have yet a manager, in that case MANAGER is null, and to call isManager(null) is the way to check if the class has a manager

The definition of changeManager is also trivial :

public static void changeManager(Employee newManager) { MANAGER = newManager; }

Note that allows to have no manager calling that operation with null


Example with few methods/attributes :

Employee.java

class Employee {
  private String name;
  private int id;

  private static Employee MANAGER;

  public Employee(String n, int i) {
    name = n;
    id = i;
  }

  public boolean isManager() {
    return this == MANAGER;
  }

  public static void changeManager(Employee e) {
    MANAGER = e;
  }
}

Main.java

class Main {
  public static void main(String[] args) {
    Employee e1 = new Employee("e1", 1);
    Employee e2 = new Employee("e2", 2);

    System.out.print("e1.isManager() = ");
    System.out.println(e1.isManager());

    System.out.print("e2.isManager() = ");
    System.out.println(e2.isManager());

    System.out.println("do 'Employee.changeManager(e1)'");
    Employee.changeManager(e1);

    System.out.print("e1.isManager() = ");
    System.out.println(e1.isManager());

    System.out.print("e2.isManager() = ");
    System.out.println(e2.isManager());

    System.out.println("do 'Employee.changeManager(e2)'");
    Employee.changeManager(e2);

    System.out.print("e1.isManager() = ");
    System.out.println(e1.isManager());

    System.out.print("e2.isManager() = ");
    System.out.println(e2.isManager());

    System.out.println("do 'Employee.changeManager(null)'");
    Employee.changeManager(null);

    System.out.print("e1.isManager() = ");
    System.out.println(e1.isManager());

    System.out.print("e2.isManager() = ");
    System.out.println(e2.isManager());
  }
}

Compilation, execution :

pi@raspberrypi:/tmp $ javac Main.java Employee.java 
pi@raspberrypi:/tmp $ java Main
e1.isManager() = false
e2.isManager() = false
do 'Employee.changeManager(e1)'
e1.isManager() = true
e2.isManager() = false
do 'Employee.changeManager(e2)'
e1.isManager() = false
e2.isManager() = true
do 'Employee.changeManager(null)'
e1.isManager() = false
e2.isManager() = false
pi@raspberrypi:/tmp $ 

Upvotes: 1

Related Questions