My Challenge Solution - missing names


#1

Hey guys! Here’s my solution - I guess it looks just like everyone elses’:

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

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

    @autoreleasepool {
        
        StockHolding *stock1 = [[StockHolding alloc] init];
        StockHolding *stock2 = [[StockHolding alloc] init];
        StockHolding *stock3 = [[StockHolding alloc] init];
        
        [stock1 setCurrentSharePrice:5];
        [stock1 setNumberOfShares:5];
        [stock1 setPurchaseSharePrice:2];
        
        [stock2 setCurrentSharePrice:4.5];
        [stock2 setNumberOfShares:56];
        [stock2 setPurchaseSharePrice:100];
        
        [stock3 setCurrentSharePrice:45];
        [stock3 setPurchaseSharePrice:40];
        [stock3 setNumberOfShares:1000];
        
        NSArray *stocks = [[NSArray alloc] initWithObjects:stock1, stock2, stock3, nil];
        
        for (StockHolding *s in stocks) {
            NSLog(@"Stock name: %@\n Stock Purchase Price: %.2f\n Stock Current Price: %.2f\n Stock Number of Shares: %d\n Stock Total Value: %.2f", s,
                  [s purchaseSharePrice],
                  [s currentSharePrice],
                  [s numberOfShares],
                  [s valueInDollars]);
        }
        
    }
    return 0;
}

The only thing I’d like is to be able to name the stocks - I guess I’d have to give them a name property and then print that out. Oh well. I can’t wait to learn about custom initializers and be able to make each stock with the properties assigned in the initialization!