Challenge Solution


#1

Hey guys,

This one was pretty cool - it did get me stumped for a little bit. Let me quickly explain where I got “stuck”

I completed the implimentation and header files no issues. Created three instances of the class, and gave them values. I also filled the array with my stocks - I was on a roll… until it came to printing out the costInDollars and the valueInDollars.

I was trying to find a way to access these instance variables once they were in the array - I tried via the array and realised that it just doesn’t have access to the data of the objects it points to.

After much head scratching - it hit me - in my for loop I should create an instance of the class StockHolding - then I will have access to its instance variables… :smiley:

Lastly I had an issue with “naming” my stocks and printing them out with the console log - did a little bit of reading and realised a property for the NSString was needed.

Here is my code:

StockHolding.h

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

{
        //Instance variables

    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares; 
    
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int  numberOfShares;
@property  (copy) NSString *stockName;


    //Instance methods

- (float) costInDollars;       //purchaseSharePrice * numberOfShares
- (float) valueInDollars;      // currentSharePrice * numberOfShares

@end

StockHolding.m

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares, stockName;


    //Impliment the getter methods

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

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


@end

main.m

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

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

    @autoreleasepool {

            //Create three instances of  StockHolding

        StockHolding *stockOne =   [[StockHolding alloc]init];
        StockHolding *stockTwo =   [[StockHolding alloc]init];
        StockHolding *stockThree = [[StockHolding alloc]init];
        
            //Give our stocks some values

           
        [stockOne setStockName:@"Stock One"]; 
        [stockOne setPurchaseSharePrice:2.30];
        [stockOne setCurrentSharePrice:4.50];
        [stockOne setNumberOfShares:40];

            
        [stockTwo setStockName:@"Stock Two"]; 
        [stockTwo setPurchaseSharePrice:12.19];
        [stockTwo setCurrentSharePrice:10.58];
        [stockTwo setNumberOfShares:90];
        
            
        [stockThree setStockName:@ "Stock Three"]; 
        [stockThree setPurchaseSharePrice:45.10];
        [stockThree setCurrentSharePrice:49.51];
        [stockThree setNumberOfShares:210];
        
            //Add them to an array

        NSArray *stocks = [NSArray arrayWithObjects:stockOne, stockTwo, stockThree, nil];

          //iterate over the array

        for (StockHolding *holdings in stocks){

            NSLog (@ "%@ has %d shares. Cost: $%.2f. Stock value: $%.2f", [holdings stockName],
                   [holdings numberOfShares], [holdings costInDollars], [holdings valueInDollars]);
                        
        }
        
    }
    return 0;
}

Output:

Pretty happy with the results. :smiley:

I’m sure there are other ways to do this - would love to hear your feedback. :mrgreen:


#2

What does the (copy) infront of NSString do?


#3

The copy option makes a copy of an object and then changes the pointer to refer to this copy.

It’s explained in chapter 30 of the book.


#4

Hi, I was trying to do the same print the name of the stocks, but i have two questions; the first: why isn’t necessary declare the instance variable for the stockName like the others as currentSharePrice or numberOfShares; and second: is there any way to obtain the name of the pointer of the class StockHolding to use like the name of the stock and avoid the need to declare the name for each stock?

Thank you.