![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Thus, the following version of
eatOnlyVegetables
extracts name and calorie information for the
vegetables found in the foods.dta
file. Then,
eatOnlyVegetables
creates Food
instances for those
vegetables, and adds those Food
instances to an ordered collection.
That ordered collection is the answer produced by eatOnlyVegetables
:
Food method definition class eatOnlyVegetables: aFile |inputStream collection string stream | inputStream := File pathName: aFile. collection := OrderedCollection new. [inputStream atEnd] whileFalse: [(string := inputStream nextLine) notEmpty ifTrue: [(string at: 1) = $V ifTrue: [stream := string asStream. stream nextWord. collection add: (Food new name: stream nextWord; fCalories: stream nextWord asInteger; cCalories: stream nextWord asInteger; pCalories: stream nextWord asInteger)]]]. inputStream close. ^ collection Workspace (Food eatOnlyVegetables: 'c:\test\foods.dta') do: [ :element | Transcript show: element name printString; cr]. Transcript 'Broccoli' 'Carrots' 'Corn' 'Cabbage'