Challenge Solution


#1

My solution to the object instance variables challenge. Rather than take the Stocks project and add to it, I decided to pull only the StockHolding class into a new project for this challenge. Appreciate any feedback/areas of improvement.

Trying to incorporate what I am learning in each chapter into my challenge solutions - repetition!

J

main.m

// Make a tool that creates an instance of a Portfolio class and fill it with StockHoldings
// The portfolio can tell you what its current value is

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

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

    @autoreleasepool {

        NSString *portfolioLabel = @"John's";
        int currentSharePriceMultiplier = 2;
        int numberOfSharesMultiplier = 2;
        
    // Create an instance of Portfolio to hold stock holdings
        Portfolio *portfolio = [[Portfolio alloc] init];
        [portfolio setLabel:portfolioLabel];
        
    // Create 10 stocks
    for (int i = 1; i < 11; i++) { // Start for loop at 1 instead of 0 since variable used within cacluations
            
        //Create an asset
        StockHolding *stock = [[StockHolding alloc] init];
            
        // Give the stock initial values
        NSString *currentLabel = [NSString stringWithFormat:@"Stock %i", i];
        [stock setLabel:currentLabel];
        [stock setCurrentSharePrice:i * currentSharePriceMultiplier];
        [stock setNumberOfShares:i * numberOfSharesMultiplier];
        
        // Assign the stockholdig to the portfolio
        [portfolio addStockHolding:stock];
    }
        
        NSLog(@"%@",[portfolio self]);
    }
    return 0;
}

Portfolio.m

#import "Portfolio.h"
#import "StockHolding.h"

@implementation Portfolio

@synthesize portfolioValue, label;

-(void)addStockHolding:(StockHolding *)a
{
    // Is stocks nil?
    if (!stocks) {
        // Create the array
        stocks = [[NSMutableArray alloc]init];
    }
    
    [stocks addObject:a];
}

-(NSUInteger) numberOfStockHoldings
{
    return [stocks count];
}

-(unsigned int)computePortfolioValue
{
    portfolioValue = 0;
    for (StockHolding *s in stocks) {
        portfolioValue = [s valueInDollars] + portfolioValue;
    }
    return portfolioValue;
}

- (NSString *)description
{
    return [NSString stringWithFormat:(@"There are %ld stock holdings, values at $%u in %@ portfolio."),[self numberOfStockHoldings], [self computePortfolioValue], [self label]];
}

@end

StockHolding.m

//
//  StockHolding.m
//  Stocks
//
//  Created by John Pisano on 10/25/12.
//  Copyright (c) 2012 John Pisano. All rights reserved.
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize label, purchaseSharePrice, currentSharePrice, numberOfShares;

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

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

- (NSString *)description
{
    return  [NSString stringWithFormat:(@"%@ stock: %.2f"),[self label],[self valueInDollars]];
}
@end