IST 311Modeling Association Relationships:
COMPANY data model
V. Matos – Spring 2005
The following fragment of a COMPANY diagram depicts the EMPLOYEE and DEPARTMENT classes.
- The EMPLOYEE class holds the name and Social Security Number of each employee, together with a reference to his/her Department.
- Each DEPARTMENT has a name, and a reference to its manager. In addition each department has a list of the employees working for the unit (the variable ListOfEmployees is an ArrayList, each entry is an EMPLOYEE reference).
- The association Manages indicates that an employee could act as the manager of at most one department. Each department must have an manager.
- A department could have many employees; bit an employee is part of only one department.
The portion of the diagram illustrating those facts is given below
The following is a VB.NET implementation of the two classes and their two associations.
PublicClassEmployee
Private Name AsString
Private Salary AsDouble
Private Dno As Department
PublicSub SetName(ByVal aName AsString)
'data validation goes here....
Name = aName
EndSub
PublicSub SetSalary(ByVal aSalary AsDouble)
'data validation goes here....
Salary = aSalary
EndSub
PublicFunction GetName() AsString
Return Name
EndFunction
PublicFunction GetSalary() AsDouble
Return Salary
EndFunction
PublicFunction TellAboutSelf() AsString
Return " Emp name: " & GetName() & _
" Salary: " & GetSalary() & _
" Dept: " & Dno.GetDname
EndFunction
PublicSubNew(ByVal aName AsString, ByVal aSalary AsDouble)
SetName(aName)
SetSalary(aSalary)
EndSub
PublicSubNew(ByVal aName AsString, ByVal aSalary AsDouble, ByVal aDept As Department)
SetName(aName)
SetSalary(aSalary)
Dno = aDept
Dno.AddNewEmployee(Me)
EndSub
EndClass
PublicClassDepartmentPrivate Dname AsString
Private ListOfWorkers AsNew ArrayList
PublicSub AddNewEmployee(ByVal anEmp As Employee)
EmpWorkingFor.Add(anEmp)
EndSub
PublicSub SetDname(ByVal aDname AsString)
'data validation goes here....
Dname = aDname
EndSub
PublicFunction GetDname() AsString
Return Dname
EndFunction
PublicSubNew(ByVal aDname AsString)
SetDname(aDname)
EndSub
PublicFunction TellAboutSelf() AsString
Dim msg AsString
Dim e As Employee
msg = " Dept Name: " & GetDname() & vbCrLf
ForEach e In ListOfWorkers
msg &= vbCrLf & e.TellAboutSelf
Next
Return msg
EndFunction
EndClass
Testing program (code Module of a console application)
Module modTester
SubMain()
'------
' COMPANY model
'------
'Implementing the EMPLOYEE and DEPARTMENT classes, as
'well as the two following relationships
'Works_For: (1:many) Each employee works for only one
' Department, however a dept. may have
' many employees.
'Manages: (1:1) Each dept. has one manager, who at most
' is in charge of one department.
'------
Dim d1 AsNew Department("Marketing")
Dim e1 AsNew Employee("Maria Macarena", 1000000, d1)
Dim e2 AsNew Employee("Lisa Simpson", 1000000, d1)
Dim e3 AsNew Employee("Jennifer Lopez", 2000000, d1)
Console.WriteLine(d1.TellAboutSelf)
Console.ReadLine()
EndSub
EndModule
NOTE:
- Only the Works_For association has been implemented. The Manages association must be completed.
- Consider adding two additional summary classes ALL_EMPLOYEES, and ALL_DEPARTMENTS. Each time the constructor of EMPLOYEE and DEPARTMENT objects is called, a reference to the newly created object is appropriately added to the corresponding summary class.
Extending the Original COMPANY diagram
TESTING MODULE
Module Module1
SubMain()
'------
' COMPANY model
'------
'Implementing the EMPLOYEE and DEPARTMENT classes, as
'well as the two following relationships
'Works_For: (1:many) Each employee works for only one
' Department, however a dept. may have
' many employees.
'Manages: (1:1) Each dept. has one manager, who at most
' is in charge of one department.
'------
Dim d1 AsNew Department("Marketing")
Dim e1 AsNew Employee("Maria Macarena", 1000000, d1)
Dim e2 AsNew Employee("Lisa Simpson", 1000000, d1, e1)
Dim e3 AsNew Employee("Jennifer Lopez", 2000000, d1, e1)
Dim e4 AsNew Employee("Dilbert", 1, d1, e3)
d1.SetManager(e1)
Console.WriteLine(d1.TellAboutSelf)
Console.ReadLine()
EndSub
EndModule
EMPLOYEE CLASS
PublicClass Employee
Private Name AsString
Private Salary AsDouble
Private Dept As Department
Private Supervisor As Employee
PublicSub SetSupervisor(ByVal anEmp As Employee)
Supervisor = anEmp
EndSub
PublicFunction GetSupervisor() As Employee
Return Supervisor
EndFunction
PublicSub SetName(ByVal aName AsString)
'data validation goes here....
Name = aName
EndSub
PublicSub SetSalary(ByVal aSalary AsDouble)
'data validation goes here....
Salary = aSalary
EndSub
PublicFunction GetName() AsString
Return Name
EndFunction
PublicFunction GetSalary() AsDouble
Return Salary
EndFunction
PublicFunction TellAboutSelf() AsString
Dim theBoss AsString
If (Supervisor IsNothing) Then
theBoss = "No supervisor known"
Else
theBoss = Supervisor.GetName
EndIf
Return " Emp name: " & GetName() & _
" Salary: " & GetSalary() & _
" Dept: " & Dept.GetDname & _
" Supervisor: " & theBoss
EndFunction
PublicSubNew(ByVal aName AsString, ByVal aSalary AsDouble)
SetName(aName)
SetSalary(aSalary)
EndSub
PublicSubNew(ByVal aName AsString, ByVal aSalary AsDouble, ByVal aDept As Department)
MyClass.New(aName, aSalary)
Dept = aDept
Dept.AddNewEmployee(Me)
EndSub
PublicSubNew(ByVal aName AsString, ByVal aSalary AsDouble, ByVal aDept As Department, ByVal aSupervisor As Employee)
MyClass.New(aName, aSalary, aDept)
Supervisor = aSupervisor
EndSub
EndClass
DEPARTMENT CLASS
PublicClass Department
Private Dname AsString
Private Manager As Employee
Private EmpWorkingFor AsNew ArrayList
PublicSub AddNewEmployee(ByVal aEmp As Employee)
EmpWorkingFor.Add(aEmp)
EndSub
PublicSub SetDname(ByVal aDname AsString)
'data validation goes here....
Dname = aDname
EndSub
PublicSub SetManager(ByVal anEmp As Employee)
Manager = anEmp
EndSub
PublicFunction GetManager() As Employee
Return Manager
EndFunction
PublicFunction GetDname() AsString
Return Dname
EndFunction
PublicSubNew(ByVal aDname AsString)
SetDname(aDname)
EndSub
PublicFunction TellAboutSelf() AsString
Dim msg AsString
Dim e As Employee
msg = " Dept Name: " & GetDname() & vbCrLf
msg &= " Manager: " & Manager.GetName & vbCrLf
ForEach e In EmpWorkingFor
msg &= vbCrLf & e.TellAboutSelf
Next
Return msg
EndFunction
EndClass
V. Matos IST311. Employee-Department Association 1