Challenge Solution and a Question on use of description


#1

Stockholding.h

[code]@interface StockHolding : NSObject

{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars;
  • (float)valueInDollars;

@end
[/code]

StockHolding.m

[code]@implementation StockHolding

@synthesize currentSharePrice, numberOfShares, purchaseSharePrice;

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

  • (float)valueInDollars
    {
    return [self currentSharePrice] * [self numberOfShares];
    }
    @end
    [/code]

main.m

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

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

    @autoreleasepool {
        
        //Create some StockHolding objects
        StockHolding *apple = [[StockHolding alloc] init];
        StockHolding *microsoft = [[StockHolding alloc] init];
        StockHolding *caterpillar = [[StockHolding alloc] init];
        
        //Create an array, pointing to the StockHolding Objects
        NSMutableArray *portfolio = [[NSMutableArray alloc] initWithObjects:apple, microsoft, caterpillar, nil];
        
        //Set a couple base values for use in the for loop to generate different values between objects
        float valueGenerator = 1.34;
        int quantityGenerator = 5;
        
        //Set some values in the within the StockHolding instances.
        for (StockHolding *sh in portfolio) {
            [sh setPurchaseSharePrice:valueGenerator];
            valueGenerator+=3.56; //3.56 is irrelevant, just using to create some dif values

            [sh setCurrentSharePrice:valueGenerator];
            valueGenerator+=2.15; //2.15 is irrelevant, just using to create some dif values
            
            [sh setNumberOfShares:quantityGenerator];
            quantityGenerator+=23;  //23 is irrelevant, just using to create some dif amounts of shares
            
            //print out the values of each StockHolding Object
            NSLog(@"%@:\n  Purchase Price:%.2f\n  Current Price: %.2f\n  %d shares owned.\n  %.2f cost in dollars\n  %.2f value in Dollars", [sh description], [sh purchaseSharePrice], [sh currentSharePrice], [sh numberOfShares], [sh costInDollars], [sh valueInDollars]);
        }
    
    }
    return 0;
}

Results

2012-06-23 11:08:54.804 Stocks[5585:403] <StockHolding: 0x7fc0c1c14220>: Purchase Price:1.34 Current Price: 4.90 5 shares owned. 6.70 cost in dollars 24.50 value in Dollars 2012-06-23 11:08:54.805 Stocks[5585:403] <StockHolding: 0x7fc0c1c14270>: Purchase Price:7.05 Current Price: 10.61 28 shares owned. 197.40 cost in dollars 297.08 value in Dollars 2012-06-23 11:08:54.805 Stocks[5585:403] <StockHolding: 0x7fc0c1c14290>: Purchase Price:12.76 Current Price: 16.32 51 shares owned. 650.76 cost in dollars 832.32 value in Dollars

I read through some of the other threads and it seems people (like me) were interested in printing out the name of the pointer to the instance of StockHolding (i.e Apple, MS, etc). I understand the fix of just adding another instance variable, but was curious if there was another method to getting that value. I tried ‘description’ and appear to be getting the class name and a memory address. Is my format string messed up or am I using ‘description’ incorrectly?

Also, if you have any thoughts on how my code could be improved, would love to hear it. Thanks in advance!