18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

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.

A Text user <strong>in</strong>terface 315<br />

with Class_W<strong>in</strong>dow;<br />

use Class_W<strong>in</strong>dow;<br />

package Pack_Program is<br />

procedure Ma<strong>in</strong>;<br />

private<br />

P_Result : P_W<strong>in</strong>dow;<br />

end Pack_Program;<br />

The call-back function User_Input is executed when a user has enter<strong>in</strong>g the distance <strong>in</strong> miles and then<br />

pressed Enter. This entered distance is converted to a float<strong>in</strong>g po<strong>in</strong>t number us<strong>in</strong>g the procedure get <strong>in</strong><br />

<strong>Ada</strong>.Float_Text_Io to convert a str<strong>in</strong>g <strong>in</strong>to an <strong>in</strong>stance of a Float. If the number is not valid or an error <strong>in</strong><br />

the calculation occurs, then an appropriate message is displayed to the user.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

with Class_Input_Manager, Class_W<strong>in</strong>dow, Class_Dialog;<br />

use Class_Input_Manager, Class_W<strong>in</strong>dow, Class_Dialog;<br />

package body Pack_Program is<br />

function User_Input( Cb_Mes:<strong>in</strong> Str<strong>in</strong>g ) return Str<strong>in</strong>g is<br />

Miles : Float; --Miles <strong>in</strong>put by user<br />

Last : Positive; --<br />

Str_Kms: Str<strong>in</strong>g( 1 .. 10 ); --As a str<strong>in</strong>g <strong>in</strong> Kms<br />

Str_Mls: Str<strong>in</strong>g( 1 .. 10 ); --As a str<strong>in</strong>g <strong>in</strong> Miles<br />

Beg<strong>in</strong><br />

beg<strong>in</strong><br />

Get( Cb_Mes & ".", Miles, Last );<br />

Put( Str_Kms, Miles * 1.609_344, Aft=>2, Exp=>0 );<br />

Put( Str_Mls, Miles, Aft=>2, Exp=>0 );<br />

Put( P_Result.all, "Distance <strong>in</strong> Miles = " );<br />

Put( P_Result.all, Str_Mls ); New_L<strong>in</strong>e( P_Result.all );<br />

Put( P_Result.all, "Distance <strong>in</strong> Kms = " );<br />

Put( P_Result.all, Str_Kms ); New_L<strong>in</strong>e( P_Result.all );<br />

exception<br />

when Data_Error =><br />

Put( P_Result.all, " Not a valid number" );<br />

New_L<strong>in</strong>e( P_Result.all );<br />

when others =><br />

Put( P_Result.all, " [Calculation error]" );<br />

New_L<strong>in</strong>e( P_Result.all );<br />

end;<br />

return "";<br />

end User_Input;<br />

Note:<br />

The call-back function returns a str<strong>in</strong>g as its result. This provides a mechanism for return<strong>in</strong>g<br />

<strong>in</strong>formation to the environment which called it. In this particular case, no <strong>in</strong>formation is returned.<br />

.A decimal po<strong>in</strong>t (.0) is appended to the user's <strong>in</strong>put to allow a user to enter an <strong>in</strong>teger value and still<br />

have the number processed correctly.<br />

The package <strong>Ada</strong>.Text_Io is required for the exception Data_Error.<br />

In the procedure Ma<strong>in</strong> the three w<strong>in</strong>dows that will be displayed are declared. Then the call-back function<br />

User_Input is associated with dialog w<strong>in</strong>dow and the program executes and waits for a user <strong>in</strong>teraction.<br />

© M A Smith - May not be reproduced without permission

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

Saved successfully!

Ooh no, something went wrong!