diff --git a/Chapters/ContactBook2/ContactBook.md b/Chapters/ContactBook2/ContactBook.md index 148e2bb..65fd86b 100644 --- a/Chapters/ContactBook2/ContactBook.md +++ b/Chapters/ContactBook2/ContactBook.md @@ -24,16 +24,16 @@ The model for the domain of our example is composed of two classes: Contact and The class modeling a contact is defined as follows. ``` -Object << #EgContact +Object << #Contact slots: {#name . #phone}; - package: 'EgContactBook' + package: 'ContactBook' ``` It just defines a `printOn:` method and a couple of accessors (not shown in the text). ``` -EgContact >> printOn: aStream +Contact >> printOn: aStream super printOn: aStream. aStream nextPut: $(. @@ -42,13 +42,14 @@ EgContact >> printOn: aStream ``` ``` -EgContact >> hasMatchingText: aString +Contact >> hasMatchingText: aString + ^ name includesSubstring: aString caseSensitive: false ``` ``` -EgContact class >> name: aNameString phone: aPhoneString +Contact class >> name: aNameString phone: aPhoneString ^ self new name: aNameString; @@ -59,17 +60,16 @@ EgContact class >> name: aNameString phone: aPhoneString #### ContactBook -Now we define the class modeling the contact book. -As for the contact class, it is simple and quite straightforward. +Now we define the class modeling the contact book. As for the contact class, it is simple and quite straightforward. ``` -Object << #EgContactBook +Object << #ContactBook slots: { #contacts }; - package: 'EgContactBook' + package: 'ContactBook' ``` ``` -EgContactBook >> initialize +ContactBook >> initialize super initialize. contacts := OrderedCollection new @@ -78,19 +78,22 @@ EgContactBook >> initialize We add the possibility to add and remove a contact ``` -EgContactBook >> addContact: aContact +ContactBook >> addContact: aContact + contacts add: aContact ``` ``` -EgContactBook >> removeContact: aContact +ContactBook >> removeContact: aContact + contacts remove: aContact ``` ``` -EgContactBook >> addContact: newContact after: contactAfter +ContactBook >> addContact: newContact after: contactAfter + contacts add: newContact after: contactAfter ``` @@ -98,7 +101,8 @@ EgContactBook >> addContact: newContact after: contactAfter We add a simple testing method in case one wants to write some tests \(which we urge you to do\). ``` -EgContactBook >> includesContact: aContact +ContactBook >> includesContact: aContact + ^ contacts includes: aContact ``` @@ -106,9 +110,10 @@ EgContactBook >> includesContact: aContact And now we add a method to create a contact and add it to the contact book. ``` -EgContactBook >> add: contactName phone: phone +ContactBook >> add: contactName phone: phone + | contact | - contact := EgContact new name: contactName; phone: phone. + contact := Contact new name: contactName; phone: phone. self addContact: contact. ^ contact ``` @@ -117,39 +122,35 @@ EgContactBook >> add: contactName phone: phone Finally, some facilities to query the contact book. ``` -EgContactBook >> findContactsWithText: aText +ContactBook >> findContactsWithText: aText + ^ contacts select: [ :e | e hasMatchingText: aText ] ``` ``` -EgContactBook >> size +ContactBook >> size + ^ contacts size ``` -#### Pre-filling up the contact book +``` +ContactBook >> contents + ^ contacts +``` -Since we want to have some contacts and we way to keep them without resorting -to a database or file we set some class instance variables. +#### Pre-filling up the contact book -We define two class instance variables: `family` and `coworkers` and define -some class method accessors as follows: -``` -EgContactBook class >> family - ^family ifNil: [ - family := self new - add: 'John' phone: '342 345'; - add: 'Bill' phone: '123 678'; - add: 'Marry' phone: '789 567'; - yourself] -``` +Since we want to have some contacts and we want to keep them without resorting to a database or file, we set some class instance variables. +We define a class instance variable `coworkers` and define a class method accessor as follows: ``` -EgContactBook class >> coworkers +ContactBook class >> coworkers + ^coworkers ifNil: [ coworkers := self new add: 'Stef' phone: '112 378'; @@ -163,10 +164,10 @@ We add one method to be able to reset them if necessary. The `