Portfolio class challenge - complete


It took a day to brew but here is my take on the challenge to create a Portfolio class leveraging the existing Stockholding class. I found sketching out the relationships with simple paper and pencil really helped to visualize what I was trying to accomplish.

Not included here is the StockHolding and ForeignStockholding classes which are identical from the initial lesson 19 which I think is one of the points of object oriented programming which is to add new classes to do new things without having to touch any existing classes.

Note, I used a float for the totalValue variable which seemed to cause a rounding error. Ah well.

First the Portfolio header file which is based on the lesson’s Asset header file an array and a float to hold the tally. There are also two methods: one to add a StockHolding object to the array and another to tally up the values of each added StockHolding object.

// Portfolio.h
// Stocks
// Created by Bill Allen on 8/20/13.
// Copyright © 2013 Bill Allen. All rights reserved.

#import <Foundation/Foundation.h>
@class StockHolding;

@interface Portfolio : NSObject
NSMutableArray *portfolio;
float totalValue;

@property (strong) NSMutableArray *portfolio;
@property float totalValue;

// Methods

  • (void)addStockObject: (StockHolding *)s;
  • (float)valueOfPortfolio;


The implementation file synthesizes the variables and implements the methods. This is based upon the Asset.m file in the lesson.

// Portfolio.m
// Stocks
// Created by Bill Allen on 8/20/13.
// Copyright © 2013 Bill Allen. All rights reserved.

#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio

@synthesize portfolio, totalValue;

// Methods

  • (void)addStockObject: (StockHolding *)s {
    // Is portfolio nil?
    if (!portfolio) {
    // Create the array
    portfolio = [[NSMutableArray alloc] init];
    [portfolio addObject:s];

  • (float)valueOfPortfolio {
    float sum = 0.0;

    for (StockHolding *a in portfolio) {
    sum += [a valueInDollars];
    return sum;

Finally the main.m file which is very simple to understand I think. It creates new Portfolio objects and adds StockHolding and ForeignStockHolding objects to the array.

In this example I created three Portfolio objects: 1. US Portfolio, 2. UK Portfolio, and 3. Combined Portfolio.

// main.m
// Stocks
// Created by Bill Allen on 8/17/13.
// Copyright © 2013 Bill Allen. All rights reserved.
// All stocks initially purchased on 2013-04-01 at closing price.
// Interesting it shows some rounding errors in the pennys.

#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHolding.h”
#import “Portfolio.h”

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

@autoreleasepool {
    // Create the main portfolio
    Portfolio *myPortfolio = [[Portfolio alloc] init];
    Portfolio *usPortfolio = [[Portfolio alloc] init];
    Portfolio *ukPortfolio = [[Portfolio alloc] init];
    // US stocks
    // Apple
    StockHolding *aapl = [[StockHolding alloc] init];
    [aapl setPurchaseSharePrice:428.91]; // 2013-04-01 - Closing price
    [aapl setNumberOfShares:100];
    [aapl setCurrentSharePrice:502.36]; // 2013-08-21
    [myPortfolio addStockObject:aapl];
    [usPortfolio addStockObject:aapl];
    // Google
    StockHolding *goog = [[StockHolding alloc] init];
    [goog setPurchaseSharePrice:801.19];
    [goog setNumberOfShares:100];
    [goog setCurrentSharePrice:869.33];
    [myPortfolio addStockObject:goog];
    [usPortfolio addStockObject:goog];
    // Earthlink
    StockHolding *elnk = [[StockHolding alloc] init];
    [elnk setPurchaseSharePrice:5.50];
    [elnk setNumberOfShares:100];
    [elnk setCurrentSharePrice:4.89];
    [myPortfolio addStockObject:elnk];
    [usPortfolio addStockObject:elnk];
    // non-US stocks - UK
    // Burberry Group plc
    ForeignStockHolding *burby = [[ForeignStockHolding alloc] init];
    [burby setPurchaseSharePrice: 40.05];
    [burby setNumberOfShares: 100];
    [burby setCurrentSharePrice: 49.60];
    [burby setConversionRate: 1.56]; // UK Pounds to Dollars on 2013-08-20

    [myPortfolio addStockObject:burby];
    [ukPortfolio addStockObject:burby];
    // Display values of portfolios.
    NSLog(@"$%.2f All Portfolios", [myPortfolio valueOfPortfolio]);
    NSLog(@"$%.2f US Portfolio", [usPortfolio valueOfPortfolio]);
    NSLog(@"$%.2f UK Portfolio", [ukPortfolio valueOfPortfolio]);
return 0;


Here is the output of the NSLog lines:

2013-08-21 14:17:22.600 Stocks[788:303] $145395.59 All Portfolios 2013-08-21 14:17:22.601 Stocks[788:303] $137658.00 US Portfolio 2013-08-21 14:17:22.601 Stocks[788:303] $7737.60 UK Portfolio

This was a very good exercise to vet out how to muck around with objects. Create objects and add properties to them. Create an array and add those objects to it and run a method on the properties of the included object properties.

From a guitarist perspective it was like learning about notes and chords.

I hope this helps someone.



Good work, and very clean too!

If you are looking for a challenge, you can spice it up a bit by reading the stocks from a text file and creating objects from them.


You haven’t really used the instance variable totalValue though. I guess you could delete that variable. Or you use valueOfPortfolio to update totalValue, and then read out totalValue in main.c.