SeatingArrangement_Namespace.vb

Click To View Text-Only Version


CODE
 

Namespace SeatingArrangment

   Public Class ClassroomSeating

       Inherits System.Collections.CollectionBase

 

       Private _totalSeatCount As Integer

 

       Public Class CollectionItems

           Private _seatName As String

           Private _row As Integer

           Private _column As Integer

 

           Friend Sub New(ByVal row As Integer, _

                          ByVal column As Integer)

 

               Try

                  If row <= 0 Then

                       Throw New ArgumentOutOfRangeException("Seat Row Assignment", "Must be a positive value." & vbCrLf)

 

                   ElseIf column <= 0 Then

                       Throw New ArgumentOutOfRangeException("Seat Column Assignment", "Must be a positive value." & vbCrLf)

 

                   Else

                       _seatName = String.Format("Seat R{0:00}C{1:00}", row, column)

                       _row = row

                       _column = column

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

 

           End Sub

 

           Public ReadOnly Property Column As Integer

               Get

                   Return _column

               End Get

           End Property

 

           Public ReadOnly Property Row As Integer

               Get

                   Return _row

               End Get

           End Property

 

           Public ReadOnly Property SeatName As String

               Get

                   Return _seatName

               End Get

           End Property

       End Class

 

 

 

       Friend Sub Add(ByVal row As Integer, _

                      ByVal column As Integer)

 

           Try

               If List.Count > 0 Then

                   Dim ci As CollectionItems = Item(row, column, False)

 

                   If ci IsNot Nothing Then

                       Throw New ArgumentException("This seat has already been assigned.")

                   Else

                       List.Add(New CollectionItems(row, column))

                       UpdateSeatCount()

                   End If

               Else

                   List.Add(New CollectionItems(row, column))

                   UpdateSeatCount()

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub Remove(ByVal index As Integer)

 

           Try

               If index > Count - 1 OrElse index < 0 Then

                   Throw New ArgumentOutOfRangeException("Index", "The collection does not contain this index." & vbCrLf)

               Else

                   List.RemoveAt(index)

                   UpdateSeatCount()

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub Remove(ByVal seatName As String)

 

           Try

               Dim ci As CollectionItems = Item(seatName)

 

               If ci IsNot Nothing Then

                   List.Remove(ci)

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub Remove(ByVal row As Integer, _

                         ByVal column As Integer)

 

           Try

               Dim ci As CollectionItems = Item(row, column)

 

               If ci IsNot Nothing Then

                   List.Remove(ci)

                   UpdateSeatCount()

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub CreateSeats(ByVal seatRows As Integer, _

                              ByVal seatColumns As Integer)

 

           Try

               If seatRows <= 0 Then

                   Throw New ArgumentOutOfRangeException("Seat Rows", "Must be a positive value." & vbCrLf)

 

               ElseIf seatColumns <= 0 Then

                   Throw New ArgumentOutOfRangeException("Seat Columns", "Must be a positive value." & vbCrLf)

 

               ElseIf seatRows > 99 Then

                   Throw New ArgumentOutOfRangeException("Seat Rows", "Cannot exceed 99." & vbCrLf)

 

               ElseIf seatColumns > 99 Then

                   Throw New ArgumentOutOfRangeException("Seat Columns", "Cannot exceed 99." & vbCrLf)

 

               Else

                   For r As Integer = 1 To seatRows

                       For c As Integer = 1 To seatColumns

                           Add(r, c)

                       Next

                   Next

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public ReadOnly Property Item(ByVal index As Integer) As CollectionItems

           Get

               Try

                   If index > Count - 1 OrElse index < 0 Then

                       Throw New ArgumentOutOfRangeException("Index", "The collection does not contain this index." & vbCrLf)

                   Else

                       Return DirectCast(List(index), CollectionItems)

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

           End Get

       End Property

 

       Public ReadOnly Property Item(ByVal seatName As String) As CollectionItems

           Get

               Try

                   If String.IsNullOrWhiteSpace(seatName) Then

                       Throw New ArgumentException("The name of the seat cannot be null or empty.")

 

                   Else

                       If List.Count > 0 Then

                           Dim qry As System.Collections.Generic.IEnumerable(Of CollectionItems) = _

                               From ci As CollectionItems In List.Cast(Of CollectionItems)() _

                                   Where ci.SeatName.ToLower.Replace(" "c, "") = seatName.ToLower.Replace(" "c, "")

 

                           If qry.Count <> 1 Then

                               Throw New ArgumentException("The seat specified by name is not in the collection.")

 

                           Else

                               Return qry.First

                           End If

                       Else

                           Return Nothing

                       End If

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

           End Get

       End Property

 

       Public ReadOnly Property Item(ByVal row As Integer, _

                                     ByVal column As Integer, _

                                     Optional ByVal throwIfNotFound As Boolean = True) As CollectionItems

           Get

               Try

                   If List.Count > 0 Then

                       Dim qry As System.Collections.Generic.IEnumerable(Of CollectionItems) = _

                           From ci As CollectionItems In List.Cast(Of CollectionItems)() _

                               Where ci.Row = row AndAlso ci.Column = column

 

                       If qry.Count <> 1 Then

                           If throwIfNotFound Then

                               Throw New ArgumentException("The seat specified by the row and column" & vbCrLf & _

                                                           "numbers is not in the collection.")

                           Else

                               Return Nothing

                           End If

                       Else

                           Return qry.First

                       End If

                   Else

                       Return Nothing

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

           End Get

       End Property

 

       Public ReadOnly Property TotalSeatCount As Integer

           Get

               Return _totalSeatCount

           End Get

       End Property

 

       Private Sub UpdateSeatCount()

           _totalSeatCount = List.Count

       End Sub

   End Class

 

 

 

 

 

   Public Class Students

       Inherits System.Collections.CollectionBase

 

       Private _rand As Random

 

       Public Sub New()

           _rand = New Random

       End Sub

 

       Public Class CollectionItems

           Private _studentName As String

           Private _seatName As String

 

           Friend Sub New(ByVal studentName As String, _

                          ByVal seatName As String)

 

               Try

                   If String.IsNullOrWhiteSpace(studentName) Then

                       Throw New ArgumentException("The student's name cannot be null or empty.")

 

                   ElseIf String.IsNullOrWhiteSpace(seatName) Then

                       Throw New ArgumentException("The name of the seat cannot be null or empty.")

 

                   Else

                       _studentName = studentName.Trim

                       _seatName = seatName.Trim

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

 

           End Sub

 

           Public ReadOnly Property SeatName As String

               Get

                   Return _seatName

               End Get

           End Property

 

           Public ReadOnly Property StudentName As String

               Get

                   Return _studentName

               End Get

           End Property

       End Class

 

 

 

       Friend Sub Add(ByVal studentName As String, _

                      ByVal seatName As String)

 

           Try

               If List.Count > 0 Then

                   Dim ci As CollectionItems = Item(studentName, False)

 

                   If ci IsNot Nothing Then

                       Throw New ArgumentException("This student is already in the collection.")

 

                   Else

                       List.Add(New CollectionItems(studentName, seatName))

                   End If

               Else

                   List.Add(New CollectionItems(studentName, seatName))

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub Remove(ByVal index As Integer)

 

           Try

               If index > Count - 1 OrElse index < 0 Then

                   Throw New ArgumentOutOfRangeException("Index", "The collection does not contain this index." & vbCrLf)

               Else

                   List.RemoveAt(index)

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub Remove(ByVal studentName As String)

 

           Try

               Dim ci As CollectionItems = Item(studentName)

 

               If ci IsNot Nothing Then

                   List.Remove(ci)

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Sub AssignStudentSeats(ByVal studentCount As Integer, _

                                     ByVal classroom As ClassroomSeating)

 

           Try

               If studentCount <= 0 Then

                   Throw New ArgumentOutOfRangeException("Student Count", "Must be a positive value." & vbCrLf)

 

               ElseIf classroom Is Nothing Then

                   Throw New ArgumentNullException("Instance Of ClassroomSeating", "Cannot be null." & vbCrLf)

 

               ElseIf classroom.TotalSeatCount = 0 Then

                   Throw New ArgumentOutOfRangeException("Classroom Total Seat Count", "Must be a positive value." & vbCrLf)

 

               ElseIf studentCount > classroom.TotalSeatCount Then

                   Throw New ArgumentOutOfRangeException("Student Count", "Exceeds the classroom seating count." & vbCrLf)

 

               Else

                   Dim seatsAssigned As New List(Of String)

 

                   For i As Integer = 1 To studentCount

                       Dim studentName As String = "Student" & i.ToString("0000")

 

                       Dim seat As ClassroomSeating.CollectionItems = _

                           classroom.Item(_rand.Next(0, classroom.TotalSeatCount))

 

                       Do While seatsAssigned.Contains(seat.SeatName)

                           seat = classroom.Item(_rand.Next(0, classroom.TotalSeatCount))

                       Loop

 

                       Add(studentName, seat.SeatName)

                       seatsAssigned.Add(seat.SeatName)

                   Next

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

       End Sub

 

       Public Function GetStudentSeatingInfo(ByVal studentName As String, _

                                             ByVal classroom As ClassroomSeating) As ClassroomSeating.CollectionItems

 

           Dim retVal As ClassroomSeating.CollectionItems = Nothing

 

           Try

               If String.IsNullOrWhiteSpace(studentName) Then

                   Throw New ArgumentException("The student's name cannot be null or empty.")

 

               ElseIf classroom Is Nothing Then

                   Throw New ArgumentNullException("Instance Of ClassroomSeating", "Cannot be null." & vbCrLf)

 

               ElseIf classroom.TotalSeatCount = 0 Then

                   Throw New ArgumentOutOfRangeException("Classroom Total Seat Count", "Must be a positive value." & vbCrLf)

 

               Else

                   Dim ci As CollectionItems = Item(studentName)

 

                   If ci IsNot Nothing Then

                       retVal = classroom.Item(ci.SeatName)

                   End If

               End If

 

           Catch ex As Exception

               Throw

           End Try

 

           Return retVal

 

       End Function

 

       Public ReadOnly Property Item(ByVal index As Integer) As CollectionItems

           Get

               Try

                   If index > Count - 1 OrElse index < 0 Then

                       Throw New ArgumentOutOfRangeException("Index", "The collection does not contain this index." & vbCrLf)

                   Else

                       Return DirectCast(List(index), CollectionItems)

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

           End Get

       End Property

 

       Public ReadOnly Property Item(ByVal studentName As String, _

                                     Optional ByVal throwIfNotFound As Boolean = True) As CollectionItems

           Get

               Try

                   If String.IsNullOrWhiteSpace(studentName) Then

                       Throw New ArgumentException("The student's name cannot be null or empty.")

 

                   Else

                       If List.Count > 0 Then

                           Dim qry As System.Collections.Generic.IEnumerable(Of CollectionItems) = _

                               From ci As CollectionItems In List.Cast(Of CollectionItems)() _

                                   Where ci.StudentName.ToLower.Replace(" "c, "") = studentName.ToLower.Replace(" "c, "")

 

                           If qry.Count <> 1 Then

                               If throwIfNotFound Then

                                   Throw New ArgumentException("The student specified by name is not in the collection.")

 

                               Else

                                   Return Nothing

                               End If

                           Else

                               Return qry.First

                           End If

                       Else

                           Return Nothing

                       End If

                   End If

 

               Catch ex As Exception

                   Throw

               End Try

           End Get

       End Property

   End Class

End Namespace