29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the name is in the address book, it will be displayed in the address book field. If not a warning message<br />

will be displayed.<br />

28.4 Summary<br />

You have now created a VisualWorks GUI application. This a pplication provides you will the basics of<br />

<strong>an</strong> address book. Of course, if you quit from the window you will lose all the names <strong><strong>an</strong>d</strong> addresses you<br />

have input. We will look later at how you c<strong>an</strong> store the dictionary used for the names <strong><strong>an</strong>d</strong> addresses into<br />

a file so that you c<strong>an</strong> access it again. The method we will use is called the Binary <strong>Object</strong> Streaming<br />

Service (or BOSS for short). It is better th<strong>an</strong> saving straight ASCII, as it perseveres the objects rather<br />

th<strong>an</strong> just plain text (where you would have to reconstruct the dictionary from the text).<br />

An import<strong>an</strong>t point to consider is how we have structured this application. We have effectively<br />

bound the address book functionality into the user interface. This is not particularly good style, however<br />

for such a small app lication it seemed acceptable. A better structure would have been to construct a<br />

graphical interface which used the facilities defined in the original Org<strong>an</strong>izer class.<br />

28.5 The visual Fin<strong>an</strong>cial M<strong>an</strong>ager application<br />

This is <strong>an</strong> exercise for you the reader. This ap plication builds on the Fin<strong>an</strong>cialM<strong>an</strong>ager you have<br />

been constructing at various times throughout this book. The aim of this exercise is to provide a<br />

graphical interface to this application. The graphic interface should allow the user to:<br />

1. Add deposits to the account for a specified amount.<br />

2. Make payments (withdrawals) from the account for specified accounts.<br />

3. Get the current bal<strong>an</strong>ce <strong><strong>an</strong>d</strong> print a statement of all payments <strong><strong>an</strong>d</strong> deposits.<br />

You should define the window independently of the Fin<strong>an</strong>cialM<strong>an</strong>ager class. This me<strong>an</strong>s that you<br />

c<strong>an</strong> make the GUIFin<strong>an</strong>cialM<strong>an</strong>ager class a subclass of ApplicationModel. You should then<br />

provide <strong>an</strong> inst<strong>an</strong>ce variable in which the actual Fin<strong>an</strong>cialM<strong>an</strong>ager object will be held. This could<br />

be done in <strong>an</strong> initialize method. I have assumed that the two input/output aspects are amount<br />

<strong><strong>an</strong>d</strong> bal<strong>an</strong>ce. These could also be initialized in this method. For example:<br />

initialize<br />

super initialize.<br />

account := Fin<strong>an</strong>cialM<strong>an</strong>ager new.<br />

amount := 0.00 asValue.<br />

bal<strong>an</strong>ce := 0.00 asValue.<br />

In the above example, account is <strong>an</strong> inst<strong>an</strong>ce variable used to hold the Fin<strong>an</strong>cialM<strong>an</strong>ager<br />

object. Note that we must first send the message super initialize to ensure that the inherited<br />

initialization is performed.<br />

It should now be possible to write methods to h<strong><strong>an</strong>d</strong>le user inputs in the form of button clicks which<br />

pass the appropriate information onto the object held in account. For example (Note this assumes that<br />

the amount to be input field is called “amount”):<br />

deposit<br />

account deposit: (amount value).<br />

243

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!