Challenge Solution


#1

Appreciate feedback on my solution. Had to look at a couple of posting to get my bearings. Kept trying to init objects using the same pointer but was not thinking about the entire sequence of events. Tried a more “verbose” route. :slight_smile:

Thanks,
J

StocHolding.h


#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    // Create two instance variables for StockHolding object
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

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

// Create Object Methods
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
@end

StockHolding.m



#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.m


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

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

    @autoreleasepool {
        
        // Create an array to hold the stock holding objects
        NSMutableArray *stockPortfolio = [[NSMutableArray alloc] init];
        
        // Define variable to track total value of stock portfolio
        float portfolioValue = 0.0;
        
        // Create three instance of StockHolding
        StockHolding *stockHolding1 = [[StockHolding alloc] init];
        StockHolding *stockHolding2 = [[StockHolding alloc] init];
        StockHolding *stockHolding3 = [[StockHolding alloc] init];
        
        // Given the three instance of StockHolding values
        [stockHolding1 setPurchaseSharePrice:2.30];
        [stockHolding1 setCurrentSharePrice: 45.0];
        [stockHolding1 setNumberOfShares:40];

        [stockHolding2 setPurchaseSharePrice:12.19];
        [stockHolding2 setCurrentSharePrice: 10.56];
        [stockHolding2 setNumberOfShares:90];

        [stockHolding3 setPurchaseSharePrice:45.10];
        [stockHolding3 setCurrentSharePrice: 49.51];
        [stockHolding3 setNumberOfShares:210];

        // Add StockHolding objects to array
        [stockPortfolio addObject:stockHolding1];
        [stockPortfolio addObject:stockHolding2];
        [stockPortfolio addObject:stockHolding3];
        
        // Iterate through Array, listing out stock information
        NSLog(@"This stock portfolio includes %lu stocks",[stockPortfolio count]);
        for (StockHolding *sp in stockPortfolio) {
            NSLog(@"The initial cost was %f and the value is %.2f",[sp costInDollars],[sp valueInDollars]);
            portfolioValue = portfolioValue + [sp valueInDollars]; // Add value of current holding to portfolio value total
        }
        NSLog(@"The total value of this stock portfolio is %.2f",portfolioValue);
    }
    return 0;
}

#2

After going through and reading the other posts on this challenge, I updated my main.m to reuse the pointer as Aaron suggested.

J

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

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

    @autoreleasepool {
        
        // Create an array to hold the stock holding objects
        NSMutableArray *stockPortfolio = [[NSMutableArray alloc] init];
        
        // Define variable to track total value of stock portfolio
        float stockPortfolioValue = 0.0;
        
        StockHolding *stock; // Define a pointer to allow for reuse
        
        stock = [[StockHolding alloc] init];
        [stock setPurchaseSharePrice:2.30];
        [stock setCurrentSharePrice:45.0];
        [stock setNumberOfShares:40];
        [stock addSelfToArray:stockPortfolio];
        
        stock = [[StockHolding alloc] init];
        [stock setPurchaseSharePrice:12.19];
        [stock setCurrentSharePrice:10.56];
        [stock setNumberOfShares:90];
        [stock addSelfToArray:stockPortfolio];
        
        stock = [[StockHolding alloc] init];
        [stock setPurchaseSharePrice:45.10];
        [stock setCurrentSharePrice:49.51];
        [stock setNumberOfShares:210];
        [stock addSelfToArray:stockPortfolio];

        // Iterate through Array, listing out stock information
        NSLog(@"This stock portfolio includes %lu stocks",[stockPortfolio count]);
        for (StockHolding *sp in stockPortfolio) {
            NSLog(@"The initial cost was %f and the value is %.2f",[sp costInDollars],[sp valueInDollars]);
            stockPortfolioValue = stockPortfolioValue + [sp valueInDollars]; // Add value of current holding to portfolio value total
        }
        NSLog(@"The total value of this stock portfolio is %.2f",stockPortfolioValue);
    }
    return 0;
}

#3

Hi,

Did you get your main.m to work? I tried your solution and kept receiving "no visible @interface for ‘StockHolding’ declares the selector ‘addSelfToArray:’

thanks.


#4

Yes, it worked great for me.

Based on the warning you shared and what I have picked up from the book, it does not look like you have defined the instance method (addSelfToArray) in the StockHolding class header file. Here is my .h file for the StockHolding class.

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    // Create three instance variables for StockHolding object
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

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

// Create Object Methods
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
- (void)addSelfToArray:(NSMutableArray *)theArray;
@end

#5

@jdpisano:
I like your solution.
Just a small remark to avoid the “trouble” with the extra declaration of “addSelfToArray”.

I’ve used the more simple “addObject” -way just replacing your addSelfToArray by: - (using your name-space)
[stockPortfolio addObject:stock]; //add values to array

like:
// create an array
NSMutableArray *shares = [NSMutableArray array];

    // Define a pointer to allow for reuse
    StockHolding *stock;

    // add values to array
    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:2.30];
    [stock setCurrentSharePrice:4.50];
    [stock setNumberOfShares:40];
    [shares addObject:stock];   //add values to array


a.s.o


#6

Nice…

I definitely like that approach as it reuses existing functionality. Thanks for sharing! I will incorporate that approach into my solution!

J