Reputation: 1012
The question is to design a structure for the employee/manager system.
1) An employee belongs to a manager.
2) A manager has several employees.
3) Manager may also belong to a manager.
4) Employee has only one manager
Design the data structure so that it supports the following operation: delete an employee/manager, promotion(an employee becomes a manager).
Any programming language would suffice.
Note: I do not want code. Just the verbal explanation would be ok.
Upvotes: 3
Views: 4704
Reputation: 744
My first idea is to use employee as baseclass with a property manager as pointer to the manager-object (and other necessary properties like 'id') and a class manager, inheriting from employee with a list of pointers to employees.
This could be wrapped in a model containing a list of all employees/manager and methods like 'promote (employee)'. Employee could have also a method like 'promote', which returns a manager-object. The manager-class could have a constructor with employee-parameter...
Update (C# code):
public class Employee
{
public string ID;
public Manager Manager;
public Manager promote() { return new Manager(this); }
public Employee() { }
}
public class Manager : Employee
{
public List<Employee> Employees;
public Manager() { }
public Manager(Employee employee) {...}
}
public class EmployeeModel
{
private List<Employee> employees;
public List<Employee> Employees { get { return this.employees; } }
public void addEmployee(...) {...}
public void removeEmployee(...) {...}
public void promoteEmployee(...) {...}
}
Somehow like this...
Upvotes: 2
Reputation: 771
I would say first find out the entities (which you already have, Employee and Manager). Now find out the relationship based on your constraint :
Manager to Employee (one to many)
Employee to Manager (one to one)
An employee can be a manager.
Options
Class
Employee which 'has' (association) List
of Employee
Employee reportsTo
) in Employee itself.Area of pain in the above two options : Option#1 If you delete the manager then List of Employee would go orphan (Employee without manager) Option#2 if you want to see all employee under a single Manager then you need to iterate through all EMployee for the managers employeeID.
Looking forward for other answers.
Upvotes: 1
Reputation: 431
from desgin perspective i would use the composite design pattern. [composite digaram]: http://en.wikipedia.org/wiki/File:Composite_UML_class_diagram_(fixed).svg . each employee ( which could be a manager ) holds pointer to his manager and each manager holds list of pointers to his employees. but there is only one class ( Employee ).
Upvotes: 1
Reputation: 1597
You need just one table:
CREATE TABLE employees (
id integer NOT NULL,
name varchar(50) DEFAULT NULL,
manager_id integer DEFAULT NULL,
PRIMARY KEY (id)
)
This table has a reference to itself (manager_id->id).
If the record has NULL value in "manager_id" field, than it's a top manager. All other records should have some value in "manager_id" field, which is a reference to PrimaryKey field "id"
Upvotes: 1