Writing Derived Methods
One kind of behavior you might want to add to your enterprise object class is the ability to perform computations based on the values of class properties. For example, studios have movies, and the total revenue of the movies times 1.5 constitutes the studio's portfolio value. To calculate a studio's portfolio value, you could have a method in Studio.java like the following:Studio.java
public Number portfolioValue() {int i,count;
double total;
NSArray revenues;
total = 0.0;
revenues = (NSArray)(movies().valueForKey("revenue"));
count = revenues.count();
for (i=0; i<count; i++) {
total +=
((Number)(revenues.objectAtIndex(i))).doubleValue();
}
return new BigDecimal(total * 1.5);
}
You can display the results of this method in the user interface by forming an association between a control and the method. That way, whenever a new studio is selected or when a selected studio's movie revenues change, its portfolio value is dynamically recalculated and displayed.
-
Add the code above to the client-side Studio.java file.
-
Add a method as a display-group property.
Display the Attributes view of the Inspector for the Studio EODisplayGroup.
Add the name of the method ( portfolioValue ) you want to use in an association.
-
Add text fields to the user interface.
Drag three text fields from the Views palette.
Make them the same size and align them in a column.
Add labels (as shown at right) to each text field.
Justify the fields' contents (as shown).
-
Associate a
method with a user interface control.
Control-drag from the Revenue text field to the Studio EODisplayGroup.
In the Connections Inspector, choose EOControlAssoc from the pop-up list at the top of the left column.
Select value in the left column.
In the right column select the method ( portfolioValue ) you want to associate with the control.
Double-click portfolioValue to connect.
Repeat the above steps, connecting the Name field to Studio's name attribute and the Budget field to the budget attribute.
-
From the DataViews palette, drag the currency formatter into the new text field.
-
Set the format.
Select the text field, and display the Formatter view of the NSTextField Inspector. Change the format as shown.
-
Build and test the application on the client.
(See " Building and Testing Your Application " for details.)
© 1999 Apple Computer, Inc. (Last Updated 13 Sep 99)