IST 311 Modeling Association Relationships

IST 311 Modeling Association Relationships

<p>IST 311 Modeling Association Relationships: COMPANY data model V. Matos – Spring 2005</p><p>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.</p><p>The portion of the diagram illustrating those facts is given below</p><p>The following is a VB.NET implementation of the two classes and their two associations.</p><p>Public Class Employee Private Name As String Private Salary As Double Private Dno As Department</p><p>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</p><p>Public Function GetName() As String Return Name End Function</p><p>Public Function GetSalary() As Double Return Salary End Function</p><p>Public Function TellAboutSelf() As String</p><p>V. Matos IST311. Employee-Department Association 1 Return " Emp name: " & GetName() & _ " Salary: " & GetSalary() & _ " Dept: " & Dno.GetDname End Function</p><p>Public Sub New(ByVal aName As String, ByVal aSalary As Double) SetName(aName) SetSalary(aSalary) End Sub</p><p>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</p><p>Public Class Department Private Dname As String Private ListOfWorkers As New ArrayList</p><p>Public Sub AddNewEmployee(ByVal anEmp As Employee) EmpWorkingFor.Add(anEmp) End Sub</p><p>Public Sub SetDname(ByVal aDname As String) 'data validation goes here.... Dname = aDname End Sub</p><p>Public Function GetDname() As String Return Dname End Function</p><p>Public Sub New(ByVal aDname As String) SetDname(aDname) End Sub</p><p>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</p><p>End Class</p><p>Testing program (code Module of a console application)</p><p>Module modTester</p><p>Sub Main()</p><p>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. '------</p><p>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)</p><p>Console.WriteLine(d1.TellAboutSelf)</p><p>Console.ReadLine() End Sub</p><p>End Module</p><p>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.</p><p>V. Matos IST311. Employee-Department Association 3 Extending the Original COMPANY diagram</p><p>V. Matos IST311. Employee-Department Association 4 TESTING MODULE</p><p>Module Module1</p><p>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. '------</p><p>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)</p><p>Console.WriteLine(d1.TellAboutSelf)</p><p>Console.ReadLine() End Sub</p><p>End Module</p><p>V. Matos IST311. Employee-Department Association 5 EMPLOYEE CLASS</p><p>Public Class Employee Private Name As String Private Salary As Double Private Dept As Department Private Supervisor As Employee</p><p>Public Sub SetSupervisor(ByVal anEmp As Employee) Supervisor = anEmp End Sub</p><p>Public Function GetSupervisor() As Employee Return Supervisor End Function</p><p>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</p><p>Public Function GetName() As String Return Name End Function</p><p>Public Function GetSalary() As Double Return Salary End Function</p><p>Public Function TellAboutSelf() As String Dim theBoss As String</p><p>If (Supervisor Is Nothing) Then theBoss = "No supervisor known" Else theBoss = Supervisor.GetName End If</p><p>Return " Emp name: " & GetName() & _ " Salary: " & GetSalary() & _ " Dept: " & Dept.GetDname & _ " Supervisor: " & theBoss End Function</p><p>Public Sub New(ByVal aName As String, ByVal aSalary As Double) SetName(aName) SetSalary(aSalary) End Sub</p><p>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</p><p>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</p><p>End Class</p><p>V. Matos IST311. Employee-Department Association 6 DEPARTMENT CLASS</p><p>Public Class Department Private Dname As String Private Manager As Employee Private EmpWorkingFor As New ArrayList</p><p>Public Sub AddNewEmployee(ByVal aEmp As Employee) EmpWorkingFor.Add(aEmp) End Sub</p><p>Public Sub SetDname(ByVal aDname As String) 'data validation goes here.... Dname = aDname End Sub</p><p>Public Sub SetManager(ByVal anEmp As Employee) Manager = anEmp End Sub</p><p>Public Function GetManager() As Employee Return Manager End Function</p><p>Public Function GetDname() As String Return Dname End Function</p><p>Public Sub New(ByVal aDname As String) SetDname(aDname) End Sub</p><p>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</p><p>End Class</p><p>V. Matos IST311. Employee-Department Association 7</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us