Imports ProductiePitching.configs
Public Class EmployeeManager
Implements IEmployeeManager
Private ReadOnly _employees As ICollection(Of IEmployee)
'''
Public ReadOnly Property Employees As ICollection(Of IEmployee) Implements IEmployeeManager.Employees
Get
Return New HashSet(Of IEmployee)(_employees)
End Get
End Property
'''
Public ReadOnly Property AvailableEmployees As ICollection(Of IEmployee) Implements IEmployeeManager.AvailableEmployees
Get
'Select the employees from the collection of all employees, which are not selected yet.
'The employees are compared on their initials in lowercase.
Return New HashSet(Of IEmployee)(_employees.Where(Function(employee) _
Not _selectedEmployeesDictionary.Values.Any(Function(selectedEmployee) _
employee.Equals(selectedEmployee))))
End Get
End Property
Private ReadOnly _selectedEmployeesDictionary As new Dictionary(Of Integer, IEmployee)
'''
Public ReadOnly Property SelectedEmployees As ICollection(Of IEmployee) Implements IEmployeeManager.SelectedEmployees
Get
Return New HashSet(Of IEmployee)(_selectedEmployeesDictionary.Values)
End Get
End Property
Sub New()
'Initial fetching of employees, then stored.
_employees = EmployeesDatabaseMapper.FetchEmployees()
'Filling the selected employees dictionary with the employee number (an index) and 'Nothing' as value. Indicating no selected employee.
For i = 0 To ConfigsLoader.NumberOfEmployees - 1
_selectedEmployeesDictionary.Add(i, Nothing)
Next
End Sub
'''
Public Function GetSelectedEmployee(employeeNumber As Integer) As IEmployee Implements IEmployeeManager.GetSelectedEmployee
If not _selectedEmployeesDictionary.ContainsKey(employeeNumber) Then
Throw New InternalException($"Employee number for getting the selected value is invalid, key value: {employeeNumber}.")
End If
'Get the value.
Return _selectedEmployeesDictionary.Item(employeeNumber)
End Function
'''
Public Sub SelectEmployee(employeeNumber As Integer, employee As IEmployee) Implements IEmployeeManager.SelectEmployee
'Employee number must be valid: exist in the dictionary.
If not _selectedEmployeesDictionary.ContainsKey(employeeNumber) Then
Throw New InternalException($"Employee number for selecting an employee is invalid, key value: {employeeNumber}.")
End If
'Employee can't be nothing.
If employee Is Nothing Then
Throw New InternalException($"The soon-to-be selected Employee can't be nothing.")
End If
'Check whether or not that employee is already picked. The selected employees can be 'Nothing'.
If _selectedEmployeesDictionary.Values.Any(Function(selectedEmployee) Not IsNothing(selectedEmployee) AndAlso selectedEmployee.Equals(employee)) Then
'Employee was picked already.
Throw New Exception("Employee was already chosen.")
End If
'No employee can already be selected for that number.
If _selectedEmployeesDictionary.Item(employeeNumber) IsNot Nothing Then
Throw New Exception($"An employee was already chosen for number (index): {employeeNumber}.")
End If
'Update the employeeNumber in the employee.
employee.Number = employeeNumber
'Add employee to dictionary of selected employees -> set the value according to an employee number.
_selectedEmployeesDictionary.Item(employeeNumber) = employee
End Sub
'''
Public Sub RemoveEmployeeSelection(employeeNumber As Integer) Implements IEmployeeManager.RemoveEmployeeSelection
'Find the employee to remove.
Dim employeeToRemove = _selectedEmployeesDictionary.Item(employeeNumber)
If employeeToRemove Is Nothing Then
'The employee was not selected, thus can't be deselected.
Throw New Exception("Cannot remove the selection of a non-selected employee.")
End If
'Update the employee number of the employee.
_selectedEmployeesDictionary.Item(employeeNumber).Number = -1
'Remove the employee -> set the value back to 'Nothing'.
_selectedEmployeesDictionary.Item(employeeNumber) = Nothing
End Sub
End Class