01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

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.

108 CHAPTER 5 Creating classes<br />

}<br />

if (numberOfComplaints > 3) {<br />

[self increaseRentalByPercent:15 withMaximum:400];<br />

}<br />

The handleComplaint method (something you might like to add as the Rental Property<br />

application continues to grow) increases the numberOfComplaints instance variable<br />

by 1. If a rental property has had more than three formal complaints, the method<br />

also invokes the increaseRentalByPercent:withMaximum: method on the same<br />

object, as indicated by the use of self as the target for the message.<br />

5.3.4 Fleshing out the method file for the CTRentalProperty class<br />

Using your newfound knowledge of how to implement methods, you’re ready to complete<br />

the first revision of your CTRentalProperty class. Replace the contents of<br />

CTRentalProperty.m with the code in the following listing.<br />

Listing 5.2<br />

Providing an initial implementation of the CTRentalProperty class<br />

#import "CTRentalProperty.h"<br />

@implementation CTRentalProperty<br />

- (void)increaseRentalByPercent:(float)percent<br />

withMaximum:(float)max {<br />

}<br />

rentalPrice = rentalPrice * (100 + percent) / 100;<br />

rentalPrice = fmin(rentalPrice, max);<br />

- (void)decreaseRentalByPercent:(float)percent<br />

withMinimum:(float)min {<br />

}<br />

rentalPrice = rentalPrice * (100 - percent) / 100;<br />

rentalPrice = fmax(rentalPrice, min);<br />

- (void)setRentalPrice:(float)newRentalPrice {<br />

rentalPrice = newRentalPrice;<br />

}<br />

- (float)rentalPrice {<br />

return rentalPrice;<br />

}<br />

- (void)setAddress:(NSString *)newAddress {<br />

[address autorelease];<br />

address = [newAddress copy];<br />

Make a<br />

}<br />

copy<br />

- (NSString *)address {<br />

return address;<br />

}<br />

d<br />

b<br />

c<br />

Update instance<br />

variable<br />

Free previous<br />

address<br />

- (void)setPropertyType:(PropertyType)newPropertyType {<br />

propertyType = newPropertyType;<br />

}

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

Saved successfully!

Ooh no, something went wrong!