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.

1. The ability to save the current statement to a file.<br />

2. The ability to load <strong>an</strong> existing statement from a file.<br />

This me<strong>an</strong>s that you should provide two new interfaces to your application, one to load a file <strong><strong>an</strong>d</strong> one to<br />

save a file. These files will contain a formatted version of the information held in the statement. The<br />

format of the text file might be: newline. For example:<br />

deposit 24.00<br />

withdraw 13.00<br />

Note you should try to use a BOSS file as this will make accessing the contents of the file much e asier.<br />

Other points to note include:<br />

• Check to see that the file exists before loading it.<br />

• Define a save: method which will save the current statement to the specified file.<br />

• Define a load: method which will load a statement into the statement collection.<br />

If you are successful then you should be able to evaluate the following in a Workspace <strong><strong>an</strong>d</strong> get the same<br />

result from both Fin<strong>an</strong>cialM<strong>an</strong>ager objects.<br />

| aFin<strong>an</strong>cialM<strong>an</strong>ager <strong>an</strong>otherFin<strong>an</strong>cialM<strong>an</strong>ager|<br />

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

aFin<strong>an</strong>cialM<strong>an</strong>ager deposit: 25.00.<br />

aFin<strong>an</strong>cialM<strong>an</strong>ager withdraw: 12.00.<br />

aFin<strong>an</strong>cialM<strong>an</strong>ager withdraw: 8.00.<br />

Tr<strong>an</strong>script show: 'The current bal<strong>an</strong>ce is ' ,<br />

aFin<strong>an</strong>cialM<strong>an</strong>ager bal<strong>an</strong>ce printString.<br />

Tr<strong>an</strong>script cr.<br />

aFin<strong>an</strong>acialM<strong>an</strong>ager statement.<br />

aFin<strong>an</strong>acialM<strong>an</strong>ager save: 'account1'.<br />

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

<strong>an</strong>otherFin<strong>an</strong>cialM<strong>an</strong>ager load: 'account1'.<br />

Tr<strong>an</strong>script show: 'The current bal<strong>an</strong>ce is ' ,<br />

<strong>an</strong>otherFin<strong>an</strong>cialM<strong>an</strong>ager bal<strong>an</strong>ce printString.<br />

Tr<strong>an</strong>script cr.<br />

<strong>an</strong>otherFin<strong>an</strong>cialM<strong>an</strong>ager statement.<br />

13.6 Summary<br />

In this chapter you have encountered streams <strong><strong>an</strong>d</strong> their use in file input <strong><strong>an</strong>d</strong> output. M<strong>an</strong>y simple<br />

<strong>Smalltalk</strong> applications never need to worry about file access, however, if you are ever going to do<br />

something where information needs to be stored or shared between images, th en you are going to need<br />

to interact with the host file system. You have now seen the basic facilities available, the Binary <strong>Object</strong><br />

Streaming Service (BOSS) approach to storing <strong><strong>an</strong>d</strong> accessing information in files is particularly useful.<br />

You should now spend some time exploring the stream <strong><strong>an</strong>d</strong> file facilities available in your <strong>Smalltalk</strong><br />

system.<br />

117

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

Saved successfully!

Ooh no, something went wrong!