December 21, 2024

The Protocol of OrderedCollection

 

This is an illustration of OrderedCollection instance topEarners.

1 2 3 4 5 6
'Bill Crynge' 'Sian O''Driscoll' 'Bryan Walker' 'Herve Constantine' 'Phillipa Reynolds' 'Terry Cook'

 

However, the following year, another artist, Mark Wolfe, exhibiting for the first time in three years, pulls out all the stops and secures number one spot. The gallery is forced to revise topEarners

topEarners addFirst: 'Mark Wolfe' which returns the argument and the revised collection is now textually represented as
OrderedCollection ('Mark Wolfe' 'Bill Crynge' 'Sian O''Driscoll' 'Bryan Walker' 'Herve Constantine' 'Phillipa Reynolds' 'Terry Cook')

Cordelia's reservations about Bill Crynge are partially justified by the final cost of rebuilding the gallery after his last show. Crynge barely makes the cut, but still holds on to his place in topEarners. How can the collection be revised?
topEarners addLast: 'Bill Crynge' will place Bill at the end of the line, but the result will be two occurences of the string'Bill Crynge', proving that you can have duplicates in an ordered collection. The following code will put Bill in his rightful place and avoid this problem

topEarners remove: 'Bill Crynge';
           add: 'Bill Crynge' after: 'Terry Cook'

The message answer is 'Bill Crynge'

topEarners is now textually represented as
OrderedCollection ('Mark Wolfe' 'Sian O''Driscoll' 'Bryan Walker' 'Herve Constantine' 'Phillipa Reynolds' 'Terry Cook' 'Bill Crynge')

Care has to be taken when using remove:
it is safer to use remove:ifAbsent:

Further revision is needed as one more artist has turned paint into gold. In with a bullet at number five is RCA graduate Will James. There are several ways of adding him to Cordelia's chosen few.

topEarners add: 'Will James' before: 'Phillipa Reynolds' answering with 'Will James'
or
topEarners add: 'Will James' beforeIndex: 5 answers with the updated collection, the representation of which is
OrderedCollection ('Mark Wolfe' 'Sian O''Driscoll' 'Bryan Walker' 'Herve Constantine' 'Will James' 'Phillipa Reynolds' 'Terry Cook' 'Bill Crynge')

If you know the index of an element that you wish to update then at:put: can be used as in
topEarners at: 8 put: 'Claudia Beckmann' answering with 'Claudia Beckmann' the collection is now textually represented as
OrderedCollection ('Mark Wolfe' 'Sian O''Driscoll' 'Bryan Walker' 'Herve Constantine' 'Will James' 'Phillipa Reynolds' 'Terry Cook' 'Claudia Beckmann')

topEarners at: 1 answers with 'Mark Wolfe' as does

topEarners first

topEarners last answers with 'Claudia Beckmann'

Messages such as size, isEmpty, includes: are all implemented as expected

The gallery has another collection in its system and records the type of work it sells. The added string elements are 'Painting', 'Print', 'Drawing', 'Sculpture' and 'Installation'.

salesTypeCollection := OrderedCollection new.

After each sale the collection grows with the addition of messages like
salesTypeCollection add: 'Painting' and so on. At any stage the number of sales of any kind of work can be counted by a looping do: message. Again we need to initialise a temporary variable count to zero, then loop through the collection, testing each element in turn for equality with the element that we are searching for. For example

|count|
count := 0.
salesTypeCollection do: [:element| (element = 'Painting')
                                       ifTrue: [count := count + 1]].
^count

So far, all these elements have been strings, but they could be any object.

Now consider a different scenario.

Next page » The exhibition collection

Previous page « OrderedCollection

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page