Challenge With Explanation


#1

EDIT: I aplogize, I forgot this book uses ARC. Please ignore the @property attributes I have. Sorry.

I first amended the StockHolding class to include a property for the name of the stock (This wasn’t required and has yet to be introduced). Here is the @interface for my StockHolding class

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
    NSString *stockName;
}

@property (nonatomic, assign) float purchaseSharePrice;
@property (nonatomic, assign) float currentSharePrice;
@property (nonatomic, assign) int numberOfShares;
@property (nonatomic, copy) NSString *name;

-(float)costInDollars;
-(float)valueInDollars;

@end

Next, I overrode the -description method of my StockHolding class. This method now gives me all the critical data of any StockHolding instance.

-(NSString *) description
{
    return [NSString stringWithFormat:
            @"\n\n"
            @"Stock Name:\t\t\t%@\n"
            @"Purchase Price:\t\t%f\n"
            @"CurrentPrice:\t\t%f\n"
            @"Number Of Stocks:\t%d\n"
            @"Initial Cost:\t\t%f\n"
            @"Current Value:\t\t%f"
            @"\n\n", 
            [self name], [self purchaseSharePrice], [self currentSharePrice], 
            [self numberOfShares], [self costInDollars], [self valueInDollars]];
}

Using the @“placeStringHere” on several lines reduces to one complete string at runtime. The \t (tab) and \n (newline) characters are backspace sequences merely used for formatting purposes in the console. The tokens in the format string are replaced by the values returned from the messages placed as arguments.

Next, I created a ForeignStockHolding class that inherited from the aforementioned StockHolding class. The @interface of the ForeignStockHolding class is as follows:

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
}

@property (nonatomic, assign) float conversionRate;

The #import of Stockholding.h is necessary as the ForeignStockHolding class needs to know which methods and instance variables it is inheriting. As instructed, I created an instance variable to hold the conversion rate and declared a property as well. I then overrode the -valueInDollars, -costInDollars, and -description methods so reflect a ForeignStockHolding instance. The .m file is as follows:

#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize conversionRate;

-(float)valueInDollars
{
    return [self currentSharePrice] * [self numberOfShares] * [self conversionRate];
}

-(float) costInDollars
{
   return [self purchaseSharePrice] * [self numberOfShares] * [self conversionRate];
}

-(NSString *) description
{
   NSString *conversionRateString = [NSString stringWithFormat:@"Conversion Rate:\t%f\n\n", [self conversionRate]];

  NSString *superDescriptionString = [super description];

  return [superDescriptionString stringByAppendingString:conversionRateString];
}

@end

The -valueInDollars and -costInDollars methods are self-explanatory. I used accessors to return the current price or purchase price and the number of shares. These were then multiplied together along with the conversion rate of the particular stock. The -description method is where it gets fun.

I could have easily copy/pasted the description method from the StockHolding class and simply added a section for conversion rate. But why be redundant?

NSString *conversionRateString = [NSString stringWithFormat:@“Conversion Rate:\t%f\n\n”, [self conversionRate]]; -> Here I created a formatted string that gives me a conversion rate “section”.

[super description] -> returns the description string from the StockHolding class as this is the superclass for ForeignStockHolding.

I then appended the conversion rate string to this and returned the new string (The entire description of the ForeignStockHolding instance).

My main.m is as follows:

#import <Foundation/Foundation.h>
#import "StockHolding.h"
#import "ForeignStockHolding.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
      
        StockHolding *stockOne = [[StockHolding alloc] init];
        
        [stockOne setName:@"Stock One"];
        [stockOne setPurchaseSharePrice:2.30];
        [stockOne setCurrentSharePrice:4.50];
        [stockOne setNumberOfShares:40];
        
        StockHolding *stockTwo = [[StockHolding alloc] init];
        
        [stockTwo setName:@"Stock Two"];
        [stockTwo setPurchaseSharePrice:12.19];
        [stockTwo setCurrentSharePrice:10.56];
        [stockTwo setNumberOfShares:90];
        
        StockHolding *stockThree = [[StockHolding alloc] init];
        
        [stockThree setName:@"Stock Three"];
        [stockThree setPurchaseSharePrice:45.10];
        [stockThree setCurrentSharePrice:49.51];
        [stockThree setNumberOfShares:210];
        
        ForeignStockHolding *fStockOne = [[ForeignStockHolding alloc] init];
        
        [fStockOne setName:@"Foreign Stock One"];
        [fStockOne setConversionRate:1.4];
        [fStockOne setPurchaseSharePrice:2.30];
        [fStockOne setCurrentSharePrice:4.50];
        [fStockOne setNumberOfShares:40];
        
        ForeignStockHolding *fStockTwo = [[ForeignStockHolding alloc] init];
        
        [fStockTwo setName:@"Foreign Stock Two"];
        [fStockTwo setConversionRate:2.9];
        [fStockTwo setPurchaseSharePrice:12.19];
        [fStockTwo setCurrentSharePrice:10.56];
        [fStockTwo setNumberOfShares:90];
        
        ForeignStockHolding *fStockThree = [[ForeignStockHolding alloc] init];
        
        [fStockThree setName:@"Foreign Stock Three"];
        [fStockThree setConversionRate:1.1];
        [fStockThree setPurchaseSharePrice:45.10];
        [fStockThree setCurrentSharePrice:49.51];
        [fStockThree setNumberOfShares:210];
        
        NSMutableArray *stockHoldings = [[NSMutableArray alloc] initWithObjects:stockOne, stockTwo, stockThree, fStockOne, fStockTwo, fStockThree, nil];
        
        for (id stock in stockHoldings) {
            NSLog(@"%@", stock);
        }
    }
        
    return 0;
}

I created three types of each stock and set their values with accessors. I then created an NSMutableArray variable named stockHoldings that held references to my six stocks. I used fast enumeration to cycle through all of the stocks in the array. I used id as the types of stocks in the array were not of the same class. Using the %@ token implicitly calls the -description method on the object placed as the argument.

My console looked like this:

[code]2011-12-18 20:06:11.366 Chapter18Challenge[2961:707]

Stock Name: Stock One
Purchase Price: 2.300000
CurrentPrice: 4.500000
Number Of Stocks: 40
Initial Cost: 92.000000
Current Value: 180.000000

2011-12-18 20:06:11.368 Chapter18Challenge[2961:707]

Stock Name: Stock Two
Purchase Price: 12.190000
CurrentPrice: 10.560000
Number Of Stocks: 90
Initial Cost: 1097.099976
Current Value: 950.400024

2011-12-18 20:06:11.369 Chapter18Challenge[2961:707]

Stock Name: Stock Three
Purchase Price: 45.099998
CurrentPrice: 49.509998
Number Of Stocks: 210
Initial Cost: 9471.000000
Current Value: 10397.099609

2011-12-18 20:06:11.370 Chapter18Challenge[2961:707]

Stock Name: Foreign Stock One
Purchase Price: 2.300000
CurrentPrice: 4.500000
Number Of Stocks: 40
Initial Cost: 128.800003
Current Value: 252.000000

Conversion Rate: 1.400000

2011-12-18 20:06:11.374 Chapter18Challenge[2961:707]

Stock Name: Foreign Stock Two
Purchase Price: 12.190000
CurrentPrice: 10.560000
Number Of Stocks: 90
Initial Cost: 3181.590088
Current Value: 2756.160156

Conversion Rate: 2.900000

2011-12-18 20:06:11.375 Chapter18Challenge[2961:707]

Stock Name: Foreign Stock Three
Purchase Price: 45.099998
CurrentPrice: 49.509998
Number Of Stocks: 210
Initial Cost: 10418.100586
Current Value: 11436.809570

Conversion Rate: 1.100000
[/code]

**Although it may not appear properly aligned, the console output is aligned (This was the purpose of the \t and \n characters in the description method)