IST 311 Modeling Association Relationships

Total Page:16

File Type:pdf, Size:1020Kb

IST 311 Modeling Association Relationships

IST 311 Modeling 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.

Public Class Employee Private Name As String Private Salary As Double Private Dno As Department

Public Sub SetName(ByVal aName As String) 'data validation goes here.... Name = aName End Sub Public Sub SetSalary(ByVal aSalary As Double) 'data validation goes here.... Salary = aSalary End Sub

Public Function GetName() As String Return Name End Function

Public Function GetSalary() As Double Return Salary End Function

Public Function TellAboutSelf() As String

V. Matos IST311. Employee-Department Association 1 Return " Emp name: " & GetName() & _ " Salary: " & GetSalary() & _ " Dept: " & Dno.GetDname End Function

Public Sub New(ByVal aName As String, ByVal aSalary As Double) SetName(aName) SetSalary(aSalary) End Sub

Public Sub New(ByVal aName As String, ByVal aSalary As Double, ByVal aDept As Department) SetName(aName) SetSalary(aSalary) Dno = aDept Dno.AddNewEmployee(Me) End Sub End Class

Public Class Department Private Dname As String Private ListOfWorkers As New ArrayList

Public Sub AddNewEmployee(ByVal anEmp As Employee) EmpWorkingFor.Add(anEmp) End Sub

Public Sub SetDname(ByVal aDname As String) 'data validation goes here.... Dname = aDname End Sub

Public Function GetDname() As String Return Dname End Function

Public Sub New(ByVal aDname As String) SetDname(aDname) End Sub

Public Function TellAboutSelf() As String Dim msg As String Dim e As Employee msg = " Dept Name: " & GetDname() & vbCrLf For Each e In ListOfWorkers msg &= vbCrLf & e.TellAboutSelf Next Return msg End Function

End Class

Testing program (code Module of a console application)

Module modTester

Sub Main()

V. Matos IST311. Employee-Department Association 2 '------' 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 As New Department("Marketing") Dim e1 As New Employee("Maria Macarena", 1000000, d1) Dim e2 As New Employee("Lisa Simpson", 1000000, d1) Dim e3 As New Employee("Jennifer Lopez", 2000000, d1)

Console.WriteLine(d1.TellAboutSelf)

Console.ReadLine() End Sub

End Module

NOTE: 1. Only the Works_For association has been implemented. The Manages association must be completed. 2. 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.

V. Matos IST311. Employee-Department Association 3 Extending the Original COMPANY diagram

V. Matos IST311. Employee-Department Association 4 TESTING MODULE

Module Module1

Sub Main() '------' 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 As New Department("Marketing") Dim e1 As New Employee("Maria Macarena", 1000000, d1) Dim e2 As New Employee("Lisa Simpson", 1000000, d1, e1) Dim e3 As New Employee("Jennifer Lopez", 2000000, d1, e1) Dim e4 As New Employee("Dilbert", 1, d1, e3) d1.SetManager(e1)

Console.WriteLine(d1.TellAboutSelf)

Console.ReadLine() End Sub

End Module

V. Matos IST311. Employee-Department Association 5 EMPLOYEE CLASS

Public Class Employee Private Name As String Private Salary As Double Private Dept As Department Private Supervisor As Employee

Public Sub SetSupervisor(ByVal anEmp As Employee) Supervisor = anEmp End Sub

Public Function GetSupervisor() As Employee Return Supervisor End Function

Public Sub SetName(ByVal aName As String) 'data validation goes here.... Name = aName End Sub Public Sub SetSalary(ByVal aSalary As Double) 'data validation goes here.... Salary = aSalary End Sub

Public Function GetName() As String Return Name End Function

Public Function GetSalary() As Double Return Salary End Function

Public Function TellAboutSelf() As String Dim theBoss As String

If (Supervisor Is Nothing) Then theBoss = "No supervisor known" Else theBoss = Supervisor.GetName End If

Return " Emp name: " & GetName() & _ " Salary: " & GetSalary() & _ " Dept: " & Dept.GetDname & _ " Supervisor: " & theBoss End Function

Public Sub New(ByVal aName As String, ByVal aSalary As Double) SetName(aName) SetSalary(aSalary) End Sub

Public Sub New(ByVal aName As String, ByVal aSalary As Double, ByVal aDept As Department) MyClass.New(aName, aSalary) Dept = aDept Dept.AddNewEmployee(Me) End Sub

Public Sub New(ByVal aName As String, ByVal aSalary As Double, ByVal aDept As Department, ByVal aSupervisor As Employee) MyClass.New(aName, aSalary, aDept) Supervisor = aSupervisor End Sub

End Class

V. Matos IST311. Employee-Department Association 6 DEPARTMENT CLASS

Public Class Department Private Dname As String Private Manager As Employee Private EmpWorkingFor As New ArrayList

Public Sub AddNewEmployee(ByVal aEmp As Employee) EmpWorkingFor.Add(aEmp) End Sub

Public Sub SetDname(ByVal aDname As String) 'data validation goes here.... Dname = aDname End Sub

Public Sub SetManager(ByVal anEmp As Employee) Manager = anEmp End Sub

Public Function GetManager() As Employee Return Manager End Function

Public Function GetDname() As String Return Dname End Function

Public Sub New(ByVal aDname As String) SetDname(aDname) End Sub

Public Function TellAboutSelf() As String Dim msg As String Dim e As Employee msg = " Dept Name: " & GetDname() & vbCrLf msg &= " Manager: " & Manager.GetName & vbCrLf For Each e In EmpWorkingFor msg &= vbCrLf & e.TellAboutSelf Next Return msg End Function

End Class

V. Matos IST311. Employee-Department Association 7

Recommended publications