| Randy A. Ynchausti 2005-11-09, 3:58 am |
| PCG,
> An Employee object has to print itself.
> Now the output is supposed to look like this
snip
> <name>, <position>.
> <address>, <phoneNumber>, with
> <all subclass instance vars & values>,
> Current pay is <result of computePay method>
snip
> This is in my Company initialize
> | myFile nextOne |
> myFile := (Filename named: 'hw1a.txt') readWriteStream.
> 1 to: 3 do: [:each | nextOne := "(self employees add: "(myFile upTo:
> Character cr)")".
> Transcript show: nextOne printString; cr].
Note that this code does not create an employee and therefore the output is
essentially the line of text that is read from the input file. Try the
following code snippet, which should give you some hints on why the snippet
you provided was not outputing the right information in the transcript. But
note that this code will not work until you fix other problems as noted
below.
| myFile |
myFile := (Filename named: 'c:\Temp\hw1a.txt') readWriteStream.
1 to: 3 do: [:each |
| nextLine elements nextEmployee |
nextLine := myFile upTo: Character cr.
elements := nextLine tokensBasedOn: $;.
nextEmployee := HourlyEmp new.
elements do: [ :eachToken |
| tokens mutator value |
tokens := eachToken tokensBasedOn: $:.
mutator := ( ( tokens at: 1 ) trimExteriorBlanks, ':' ) asSymbol.
value := ( ( tokens at: 2) copyFrom: 3 to: ( ( tokens at: 2) size - 1 ) ).
nextEmployee perform: mutator with: value ].
Transcript show: nextEmployee printString; cr].
However, you still have several problems with the "printOn:" methods of your
Employee class and its subclasses. You also need to consider that the code
I provided does not use the information that is read from the input file to
decide which type of employee should be created. I recommend that you
create a factory method on the Employee class that selects which employee
class that needs to be created based in the input information that is read
from the file.
Anyway I hope that helps.
Regards,
Randy
|