Navigation:  Tutorials > Beginners Guide to Smalltalk > Programming in Smalltalk > Wiring the game together >

Prompting for a new Animal

Previous pageReturn to chapter overviewNext page

So we saw in the last method a need to have a way of creating a new Animal by prompting for its name. Well here it is.

Animal class>>prompt

       "Prompt for a new animal to add to the game"

 

       | newName |

       [newName isNil or: [newName isEmpty]] whileTrue: [

               newName := Prompter prompt: 'Tell me, what animal were you thinking of?

It was a...' caption: 'So, you win...'].

       ^self name: newName

 

As you can see the method makes use of a class called Prompter, which can be used to prompt the user to enter a text string. This is checked for validity and then used to create a new Animal instance using our original #name: method. Remember too that it is a class method so you must have the Class mode selected in the browser when you create it.

Tip: The method is complicated slightly by the fact that we have to check that Prompter does answer a valid name. If we didn't do this you'd find that the game would pretty soon go horribly wrong.