Building the Smalltalk data type
Switch to the Script Editor for the MyCanadianProvince class. Modify the class definition by adding a class variable, which will map province names to their postal abbreviations. Also add two instance variables named province and abbreviation. Make your class definition look like the following:
Object subclass: #MyCanadianProvince
instanceVariableNames: 'province abbreviation'
classVariableNames: 'AbbreviationMapping'
poolDictionaries: ''
The first methods you need are the class methods that initialize the abbreviation-to-province name mapping, creating a dictionary to hold the names and abbreviations. Implement each of these methods in MyRunSamplePartsApp:
mapping
"Answer the mapping dictionary."
AbbreviationMapping == nil
ifTrue: [self initializeMapping].
^AbbreviationMapping
initializeMapping
"Initialize the abbreviation-to-province mappings."
AbbreviationMapping := Dictionary new.
AbbreviationMapping
at: 'AB' put: 'Alberta';
at: 'BC' put: 'British Columbia';
at: 'MB' put: 'Manitoba';
at: 'NB' put: 'New Brunswick';
at: 'NF' put: 'Newfoundland and Labrador';
at: 'NT' put: 'Northwest Territories';
at: 'NS' put: 'Nova Scotia';
at: 'ON' put: 'Ontario';
at: 'PE' put: 'Prince Edward Island';
at: 'PQ' put: 'Quebec';
at: 'SK' put: 'Saskatchewan';
at: 'YT' put: 'Yukon Territory'.
Next, write two class methods that create a province object from either the province name or the abbreviation. These two methods simply search the dictionary to find the data that matches. If a match is found, each method creates and initializes a new instance of the MyCanadianProvince class. If no match is found, the methods return nil. The following class method searches by the province name:
fromProvince: aProvince
"Answer a new province object or nil"
self mapping associationsDo: [ :each |
(each value asUppercase = aProvince asUppercase)
ifTrue: [
^self new
abbreviation: each key;
province: each value]].
^nil
The following class method searches by the postal abbreviation:
fromAbbreviation: anAbbreviation
"Answer a new province object or nil"
| assoc |
assoc := self mapping
associationAt: (anAbbreviation asUppercase)
ifAbsent: [^nil].
^self new abbreviation: assoc key;
province: assoc value.
Now it's time to write the instance methods that get and set the province and abbreviation instance variables:
abbreviation
"Answer the province's abbreviation"
^abbreviation == nil
ifTrue: [ '' ]
ifFalse: [ abbreviation ]
abbreviation: abbrev
"Set the province's abbreviation"
abbreviation := abbrev.
province
"Answer the province's name"
^province == nil
ifTrue: [ '' ]
ifFalse: province ]
province: prov
"Set the province's name"
province := prov.
Because this is just a Smalltalk object, and not a VA Smalltalk part, you don't need the signalEvent: message that you normally have in the set selectors.
You'll add a few more methods later, but you have enough of the logic finished for the data converter to work properly. Before you continue, test your work using the following expressions. Type and select each expression in the System Transcript, select it, and then select Inspect from the pop-up menu:
MyCanadianProvince fromAbbreviation: 'NS'.
MyCanadianProvince fromProvince: 'Manitoba'.
If these tests did not work as you expected, go back and check each of the methods you wrote before you continue.
Last modified date: 01/29/2015