My Challenge


#1

Hope I got it right, feedback is highly appreciated

Header File


#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}
@property float purchaseSharePrice, currentSharePrice;
@property int numberOfShares;

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


@end

Implementation File

#import "StockHolding.h"

@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

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

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

@end

Main, here i come…


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

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

    @autoreleasepool {
        
        StockHolding *a = [[StockHolding alloc] init];
        StockHolding *b = [[StockHolding alloc] init];
        StockHolding *c = [[StockHolding alloc] init];
        
        [a setPurchaseSharePrice:2.30];
        [a setCurrentSharePrice:4.50];
        [a setNumberOfShares:40];
        [a costInDollars];
        [a valueInDollars];
        
        [b setPurchaseSharePrice:12.19];
        [b setCurrentSharePrice:10.56];
        [b setNumberOfShares:90];
        [b costInDollars];
        [b valueInDollars];
        
        [c setPurchaseSharePrice:45.10];
        [c setCurrentSharePrice:49.51];
        [c setNumberOfShares:210];
        [c costInDollars];
        [c valueInDollars];
        
        NSMutableArray *myStocks =[[NSMutableArray alloc] init];
        [myStocks insertObject:a atIndex:0];
        [myStocks insertObject:b atIndex:1];
        [myStocks insertObject:c atIndex:2];
        
        for(StockHolding *s in myStocks)
        {
            NSLog(@"Stock # %lu Stock % f Purchsed Share Price with a %f current share Price and a %d number of shares, its total cost in dollars is %f and their current value is %f", [myStocks indexOfObject:s] +1,
                                      [s purchaseSharePrice],
                                      [s currentSharePrice],
                                      [s numberOfShares],
                                      [s costInDollars],
                                      [s valueInDollars]);
        }
        
    }
    return 0;
}

#2

The +1 in your enumeration threw me at first, but now I see what you are doing!


#3

Why is the following statement needed?:

[myStocks indexOfObject:s] +1

Does not the previous ‘for’ statement (fast enumeration)take care of the need to increment the index?

for(StockHolding *s in myStocks)

I saw it it another post’s answer to this challenge as well, and I am just trying to figure out what it is doing.

*Edit: I think I see now why you have the +1, is it because of the first object being at index 0 (zero)? If so, do you even need that line of code?


#4

To my eyes, the primary advantage of printing the index like that (offset by one to look more “natural”) is to compensate for the fact that none of these stocks are named. If they were, the more appropriate output would be to specify the name of the stock, not the index.

In any event, the critique I have is that it would be far more efficient to keep a counter and print the index that way. By searching the array for each object each time, you’re wasting a lot of time relative to a simple integer counter.


#5

Ah yes, I see that now. It is a helpful reference.