login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Telefoonboek(Set, Get, properties, ...)

Offline Cloudje - 09/01/2012 12:35
Avatar van CloudjeLid Hallo,

Ik ben bezig aan een project waarin een telefoonboek met unieke telefoonnummers moet ontwerpt worden. Ik heb al een deel code maar zou de klasse moeten aanvullen zodanig alle assertions True geven(via Get, Set of de nodige properties).

Kan iemand mij hier een stuk in helpen hoe dit te bekomen?

Vanaf lijn 'Fill In zou dit moeten ingevuld worden.

  1. Option Strict On
  2. Option Explicit On
  3. Class Person
  4. Public Property Name As String
  5. End Class
  6. Class PhonebookEntry
  7. Public Property Phone As String
  8. Public Property Person As Person
  9. End Class
  10. Class Phonebook
  11. Private _Entries(1) As PhonebookEntry
  12. Private _Count As Integer
  13. Public ReadOnly Property Count() As Integer
  14. Get
  15. Count = _Count
  16. End Get
  17. End Property
  18. Public ReadOnly Property Capacity() As Integer
  19. Get
  20. Capacity = _Entries.Length
  21. End Get
  22. End Property
  23. ' Fill In
  24. End Class
  25. Module PhonebookTestFixture
  26. Sub Main()
  27. Dim phone1 As String = "012/34.56.78"
  28. Dim person1 As New Person
  29. person1.Name = "John"
  30. Dim phone2 As String = "123/45.67.89"
  31. Dim person2 As New Person
  32. person2.Name = "Jane"
  33. Dim phone3 As String = "234/56.78.90"
  34. Dim person3 As New Person
  35. person3.Name = "Paul"
  36. Dim phone4 As String = "345/67.89.01"
  37. Dim person4 As New Person
  38. person4.Name = "Mia"
  39. '
  40. ' Test Scenario : Initial State :
  41. Dim target As New Phonebook
  42. Assert.IsTrue(target.Count = 0, "No entries to start with.")
  43. Assert.IsTrue(target.Capacity = 2, "Initial Capacity is 2.")
  44. Assert.IsTrue(target.IndexOf(phone1) = -1, "IndexOf -1 for unfound phone.")
  45. Assert.IsTrue(target.Contains(phone1) = False, "Does not contains unfound phone.")
  46. Assert.IsTrue(target.Item(0) Is Nothing, "Indexer delivers no Entry.")
  47. Assert.IsTrue(target.Item(phone1) Is Nothing, "Quantifier delivers no Person.")
  48. '
  49. ' Test Scenario : Adding with Add Method :
  50. target.Add(phone1, person1) ' Adding a new entry for phone1 with person1.
  51. ' 0 : phone1 -> person1
  52. Assert.IsTrue(target.Count = 1, "Contains 1 element after first Add.")
  53. Assert.IsTrue(target.Capacity = 2, "Capacity is unchanged after first Add.")
  54. Assert.IsTrue(target.IndexOf(phone1) = 0, "IndexOf found phone after first Add.") ' Logical equality.
  55. Assert.IsTrue(target.IndexOf(phone2) = -1, "IndexOf -1 for unfound phone after first Add.")
  56. Assert.IsTrue(target.Contains(phone1) = True, "Contains found phone after first Add.") ' Logical equality.
  57. Assert.IsTrue(target.Contains(phone2) = False, "Does not contain phone after first Add.")
  58. Assert.IsTrue(target.Item(0).Person Is person1, "Indexer delivers Entry after first Add.") ' Physical equality.
  59. Assert.IsTrue(target.Item(0).Phone = phone1, "Indexer delivers Entry after first Add.") ' Logical equality.
  60. Assert.IsTrue(target.Item(1) Is Nothing, "Indexer delivers no Entry after first Add.")
  61. Assert.IsTrue(target.Item(phone1) Is person1, "Quantifier delivers Person after first Add.") ' Physical equality.
  62. Assert.IsTrue(target.Item(phone2) Is Nothing, "Quantifier delivers no Person after first Add.")
  63. target.Add(phone2, person2) ' Adding a new entry for phone2 with person2.
  64. ' 0 : phone1 -> person1
  65. ' 1 : phone2 -> person2
  66. target.Add(phone3, person1) ' Adding a new entry for phone3 with person1.
  67. ' 0 : phone1 -> person1
  68. ' 1 : phone2 -> person2
  69. ' 2 : phone3 -> person1
  70. Assert.IsTrue(target.Count = 3, "Each call to Add increases the Count with 1.")
  71. Assert.IsTrue(target.Capacity = 4, "Capacity doubles when insufficient.")
  72. Assert.IsTrue(target.IndexOf(phone1) = 0, "IndexOf found phone after Add.")
  73. Assert.IsTrue(target.IndexOf(phone2) = 1, "IndexOf found phone after Add.")
  74. Assert.IsTrue(target.IndexOf(phone3) = 2, "IndexOf found phone after Add.")
  75. Assert.IsTrue(target.Contains(phone1) = True, "Contains found phone after Add.")
  76. Assert.IsTrue(target.Contains(phone2) = True, "Contains found phone after Add.")
  77. Assert.IsTrue(target.Contains(phone3) = True, "Contains found phone after Add.")
  78. Assert.IsTrue(target.Item(0).Person Is person1, "Indexer delivers Entry after Add.")
  79. Assert.IsTrue(target.Item(0).Phone = phone1, "Indexer delivers Entry after Add.")
  80. Assert.IsTrue(target.Item(1).Person Is person2, "Indexer delivers Entry after Add.")
  81. Assert.IsTrue(target.Item(1).Phone = phone2, "Indexer delivers Entry after Add.")
  82. Assert.IsTrue(target.Item(2).Person Is person1, "Indexer delivers Entry after Add.")
  83. Assert.IsTrue(target.Item(2).Phone = phone3, "Indexer delivers Entry after Add.")
  84. Assert.IsTrue(target.Item(phone1) Is person1, "Quantifier delivers Person after Add.")
  85. Assert.IsTrue(target.Item(phone2) Is person2, "Quantifier delivers Person after Add.")
  86. Assert.IsTrue(target.Item(phone3) Is person1, "Quantifier delivers Person after Add.")
  87. '
  88. ' Test Scenario : Replacing a Complete Entry thru Use of Indexer :
  89. target.Item(1) = New PhonebookEntry ' Replacing complete entry at Index 1.
  90. target.Item(1).Phone = phone4
  91. target.Item(1).Person = person4
  92. ' 0 : phone1 -> person1
  93. ' 1 : phone4 -> person4
  94. ' 2 : phone3 -> person1
  95. Assert.IsTrue(target.Count = 3, "Replacing an entry does not affect Count.")
  96. Assert.IsTrue(target.IndexOf(phone2) = -1, "Entry with phone2 removed after replacing thru indexer.")
  97. Assert.IsTrue(target.IndexOf(phone4) = 1, "Entry for phone4 at index 1 after replacing thru indexer.")
  98. Assert.IsTrue(target.Item(1).Person Is person4, "Indexer delivers Entry after replacing thru indexer.")
  99. Assert.IsTrue(target.Item(1).Phone = phone4, "Indexer delivers Entry after replacing thru indexer.")
  100. Assert.IsTrue(target.Item(phone2) Is Nothing, "Quantifier delivers no Person after replacing thru indexer.")
  101. Assert.IsTrue(target.Item(phone4) Is person4, "Quantifier delivers Person after replacing thru indexer.")
  102. '
  103. ' Test Scenario : Replacing a Person on a Certain Phone thru Use of Quantifier :
  104. target.Item(phone3) = person2 ' Changing person1 to person2 for existing phone3.
  105. ' 0 : phone1 -> person1
  106. ' 1 : phone4 -> person4
  107. ' 2 : phone3 -> person2
  108. Assert.IsTrue(target.Count = 3, "Changing the person on a phone does not affect Count.")
  109. Assert.IsTrue(target.IndexOf(phone3) = 2, "IndexOf found phone after replacing thru quantifier.")
  110. Assert.IsTrue(target.Item(0).Person Is person1, "Indexer delivers Entry after replacing thru quantifier.")
  111. Assert.IsTrue(target.Item(0).Phone = phone1, "Indexer delivers Entry after replacing thru quantifier.")
  112. Assert.IsTrue(target.Item(2).Person Is person2, "Indexer delivers Entry after replacing thru quantifier.")
  113. Assert.IsTrue(target.Item(2).Phone = phone3, "Indexer delivers Entry after replacing thru quantifier.")
  114. Assert.IsTrue(target.Item(phone1) Is person1, "Quantifier delivers Person after replacing thru quantifier.")
  115. Assert.IsTrue(target.Item(phone3) Is person2, "Quantifier delivers Person after replacing thru quantifier.")
  116. '
  117. ' Test Scenario : Adding an Entry thru Use of Quantifier :
  118. target.Item(phone2) = person1 ' Adding a new entry for phone2 with person1.
  119. ' 0 : phone1 -> person1
  120. ' 1 : phone4 -> person4
  121. ' 2 : phone3 -> person2
  122. ' 3 : phone2 -> person1
  123. Assert.IsTrue(target.Count = 4, "Adding thru quantifier increases Count with 1.")
  124. Assert.IsTrue(target.Capacity = 4, "Adding thru quantifier could double Capacity.")
  125. Assert.IsTrue(target.IndexOf(phone1) = 0, "IndexOf found phone after adding thru quantifier.")
  126. Assert.IsTrue(target.IndexOf(phone4) = 1, "IndexOf found phone after adding thru quantifier.")
  127. Assert.IsTrue(target.IndexOf(phone3) = 2, "IndexOf found phone after adding thru quantifier.")
  128. Assert.IsTrue(target.IndexOf(phone2) = 3, "IndexOf found phone after adding thru quantifier.")
  129. Assert.IsTrue(target.Item(3).Person Is person1, "Indexer delivers Entry after replacing thru quantifier.")
  130. Assert.IsTrue(target.Item(3).Phone = phone2, "Indexer delivers Entry after adding thru quantifier.")
  131. '
  132. ' Test Scenario : Removing an Entry with Certain Phone :
  133. target.Remove(phone1) ' Removing complete entry with phone1.
  134. ' 0 : phone4 -> person4
  135. ' 1 : phone3 -> person2
  136. ' 2 : phone2 -> person1
  137. Assert.IsTrue(target.Count = 3, "Removing thru Remove decreases Count with 1.")
  138. Assert.IsTrue(target.Capacity = 4, "Capacity stays unchanged after Remove.")
  139. Assert.IsTrue(target.IndexOf(phone1) = -1, "IndexOf -1 for unfound phone after Remove.")
  140. Assert.IsTrue(target.IndexOf(phone4) = 0, "IndexOf found phone after Remove.")
  141. Assert.IsTrue(target.IndexOf(phone3) = 1, "IndexOf found phone after Remove.")
  142. Assert.IsTrue(target.IndexOf(phone2) = 2, "IndexOf found phone after Remove.")
  143. Assert.IsTrue(target.Contains(phone1) = False, "Does not contain phone after Remove.")
  144. '
  145. ' Test Scenario : Removing an Entry at a Specific Index :
  146. target.RemoveAt(0) ' Remove entry at Index 0.
  147. target.RemoveAt(1) ' Remove entry at Index 1.
  148. ' 0 : phone3 -> person2
  149. Assert.IsTrue(target.Count = 1, "Removing thru RemoveAt decreases Count with 1.")
  150. Assert.IsTrue(target.Capacity = 4, "Capacity stays unchanged after RemoveAt.")
  151. Assert.IsTrue(target.IndexOf(phone1) = -1, "IndexOf -1 for unfound phone after RemoveAt.")
  152. Assert.IsTrue(target.IndexOf(phone4) = -1, "IndexOf -1 for unfound phone after RemoveAt.")
  153. Assert.IsTrue(target.IndexOf(phone2) = -1, "IndexOf -1 for unfound phone after RemoveAt.")
  154. Assert.IsTrue(target.IndexOf(phone3) = 0, "IndexOf found phone after RemoveAt.")
  155. '
  156. ' Test Scenario : Removing all Entries :
  157. target.Add(phone1, person1)
  158. ' 0 : phone3 -> person2
  159. ' 1 : phone1 -> person1
  160. target.Clear() ' Removing all entries.
  161. ' ..empty..
  162. Assert.IsTrue(target.Count = 0, "Zero items after Clear.")
  163. Assert.IsTrue(target.Capacity = 2, "Initial Capacity (2) reset after Clear.")
  164. Assert.IsTrue(target.IndexOf(phone1) = -1, "IndexOf -1 for unfound phone after Clear.")
  165. '
  166. Console.WriteLine("Test Score : " & Assert.Score / 73 * 30 & " / 30")
  167. Console.WriteLine("Deze score kan nog verminderd worden door bijvoorbeeld : ")
  168. Console.WriteLine("- minder ideale implementatie (omslachtige algoritmes, teveel dataholder, ...)")
  169. Console.WriteLine("- minder ideale definitie (bijvoorbeeld vergeten van type specifiers)")
  170. Console.WriteLine("- ongewenste abstractie (bijvoorbeeld settable maken van readonly gegeven)")
  171. '
  172. Console.ReadLine()
  173. End Sub
  174. End Module
  175. Module Assert
  176. Public Score As Integer
  177. Public Sub IsTrue(ByVal condition As Boolean, ByVal message As String)
  178. If condition Then
  179. Console.Write("Assertion Correct : ")
  180. Score += 1
  181. Else
  182. Console.Write("Assertion Failed : ")
  183. End If
  184. Console.WriteLine(message)
  185. End Sub
  186. End Module


Dank bij voorbaat!

2 antwoorden

Gesponsorde links
Offline Mario - 10/01/2012 13:07
Avatar van Mario Crew algemeen Wat wil je juist bereiken cloudje, want een assert is enkel van toepassing in debug mode en zal dus niet werken in je released versie van je programma.

Is het niet beter om de meeste data gewoon te checken tijdens ingave op je form ?
Offline Cloudje - 12/01/2012 22:41
Avatar van Cloudje Lid Mario, Wat ik zou willen bereiken is het volgende:
- Een telefoonnummer (phone) hoort bij één persoon (Person).
- Phonebook is een verzameling van PhonebookEntry's die bestaat uit een koppeling van een phone aan een Person.
- We gaan ervan uit dat elke Phone in Phonebook is in principe uniek, hier hoeven geen controles ingebouwd te worden.

Alleen de klasse Phonebook zouden moeten aangevuld geraken zodanig alle onderstaande testen te laten slagen.






Gesponsorde links
Je moet ingelogd zijn om een reactie te kunnen posten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.198s