My Solution to the Challenge


#1

Hey everyone. My first post here. I’m not a programmer by trade, but have taken a few courses. Bought this book to have a little fun. Here’s what I’ve got…

StockHolding.m

[code]
#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars
    {
    float c = [self purchaseSharePrice] * [self numberOfShares];
    return c;
    }

  • (float)valueInDollars
    {
    float v = [self currentSharePrice] * [self numberOfShares];
    return v;
    }

@end[/code]

Portfolio.m

[code]#import “Portfolio.h”

@implementation Portfolio

  • (void) addStocks:(StockHolding *)a
    {
    // Is listOfStocks nil?
    if (!listOfStocks) {

      // Create the array
      listOfStocks = [[NSMutableArray alloc] init];
    

    }
    [listOfStocks addObject:a];
    }

  • (float)portfolioValue
    {
    float value;
    for (int i = 0; i < [listOfStocks count]; i++)
    {
    value = value + [[listOfStocks objectAtIndex:i] valueInDollars];
    }
    return value;
    }

@end[/code]

main.m

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

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

@autoreleasepool {
    
    //  Create 3 Portfolios, add stockHoldings to them
    for (int i = 0; i < 3; i++) {
        Portfolio *thisPortfolio = [[Portfolio alloc] init];
        
        // Create 3 Stockholdings and add them to thisPortfolio
        
        for (int j = 0; j < 3; j++) {
        // Create stockholding
        StockHolding *Holding = [[StockHolding alloc] init];
        [Holding setPurchaseSharePrice:random() % 100 + 10];
        [Holding setCurrentSharePrice:[Holding purchaseSharePrice] + random() % 20];
        [Holding setNumberOfShares:random() % 500 + 100];
            
            // Print stock info
            NSLog(@"Portfolio %d, Stock %d, PurchaseSharePrice = %f", i, j, [Holding purchaseSharePrice]);
            NSLog(@"Portfolio %d, Stock %d, CurrentSharePrice = %f", i, j, [Holding currentSharePrice]);
            NSLog(@"Portfolio %d, Stock %d, NumberofShares = %d", i, j, [Holding numberOfShares]);
            NSLog(@"Portfolio %d, Stock %d, ValueInDollars = %f", i, j, [Holding valueInDollars]);
            
        // Add stockholding to thisPortfolio
        [thisPortfolio addStocks:Holding];
        }
        // Print Portfolio value
        NSLog(@"Porfolio %d, Value = %f", i, [thisPortfolio portfolioValue]);
    }
}
return 0;

}[/code]

Results

2013-07-27 17:27:13.929 Stocks[554:303] Portfolio 0, Stock 0, PurchaseSharePrice = 93.000000
2013-07-27 17:27:13.931 Stocks[554:303] Portfolio 0, Stock 0, CurrentSharePrice = 99.000000
2013-07-27 17:27:13.931 Stocks[554:303] Portfolio 0, Stock 0, NumberofShares = 377
2013-07-27 17:27:13.932 Stocks[554:303] Portfolio 0, Stock 0, ValueInDollars = 37323.000000
2013-07-27 17:27:13.932 Stocks[554:303] Portfolio 0, Stock 1, PurchaseSharePrice = 25.000000
2013-07-27 17:27:13.933 Stocks[554:303] Portfolio 0, Stock 1, CurrentSharePrice = 38.000000
2013-07-27 17:27:13.933 Stocks[554:303] Portfolio 0, Stock 1, NumberofShares = 435
2013-07-27 17:27:13.933 Stocks[554:303] Portfolio 0, Stock 1, ValueInDollars = 16530.000000
2013-07-27 17:27:13.934 Stocks[554:303] Portfolio 0, Stock 2, PurchaseSharePrice = 96.000000
2013-07-27 17:27:13.934 Stocks[554:303] Portfolio 0, Stock 2, CurrentSharePrice = 108.000000
2013-07-27 17:27:13.935 Stocks[554:303] Portfolio 0, Stock 2, NumberofShares = 249
2013-07-27 17:27:13.935 Stocks[554:303] Portfolio 0, Stock 2, ValueInDollars = 26892.000000
2013-07-27 17:27:13.936 Stocks[554:303] Porfolio 0, Value = 80745.000000
2013-07-27 17:27:13.936 Stocks[554:303] Portfolio 1, Stock 0, PurchaseSharePrice = 31.000000
2013-07-27 17:27:13.937 Stocks[554:303] Portfolio 1, Stock 0, CurrentSharePrice = 33.000000
2013-07-27 17:27:13.937 Stocks[554:303] Portfolio 1, Stock 0, NumberofShares = 127
2013-07-27 17:27:13.937 Stocks[554:303] Portfolio 1, Stock 0, ValueInDollars = 4191.000000
2013-07-27 17:27:13.938 Stocks[554:303] Portfolio 1, Stock 1, PurchaseSharePrice = 100.000000
2013-07-27 17:27:13.938 Stocks[554:303] Portfolio 1, Stock 1, CurrentSharePrice = 119.000000
2013-07-27 17:27:13.939 Stocks[554:303] Portfolio 1, Stock 1, NumberofShares = 363
2013-07-27 17:27:13.939 Stocks[554:303] Portfolio 1, Stock 1, ValueInDollars = 43197.000000
2013-07-27 17:27:13.940 Stocks[554:303] Portfolio 1, Stock 2, PurchaseSharePrice = 36.000000
2013-07-27 17:27:13.940 Stocks[554:303] Portfolio 1, Stock 2, CurrentSharePrice = 36.000000
2013-07-27 17:27:13.941 Stocks[554:303] Portfolio 1, Stock 2, NumberofShares = 526
2013-07-27 17:27:13.941 Stocks[554:303] Portfolio 1, Stock 2, ValueInDollars = 18936.000000
2013-07-27 17:27:13.941 Stocks[554:303] Porfolio 1, Value = 66324.000000
2013-07-27 17:27:13.942 Stocks[554:303] Portfolio 2, Stock 0, PurchaseSharePrice = 82.000000
2013-07-27 17:27:13.942 Stocks[554:303] Portfolio 2, Stock 0, CurrentSharePrice = 98.000000
2013-07-27 17:27:13.943 Stocks[554:303] Portfolio 2, Stock 0, NumberofShares = 311
2013-07-27 17:27:13.943 Stocks[554:303] Portfolio 2, Stock 0, ValueInDollars = 30478.000000
2013-07-27 17:27:13.944 Stocks[554:303] Portfolio 2, Stock 1, PurchaseSharePrice = 78.000000
2013-07-27 17:27:13.944 Stocks[554:303] Portfolio 2, Stock 1, CurrentSharePrice = 85.000000
2013-07-27 17:27:13.945 Stocks[554:303] Portfolio 2, Stock 1, NumberofShares = 529
2013-07-27 17:27:13.945 Stocks[554:303] Portfolio 2, Stock 1, ValueInDollars = 44965.000000
2013-07-27 17:27:13.945 Stocks[554:303] Portfolio 2, Stock 2, PurchaseSharePrice = 92.000000
2013-07-27 17:27:13.946 Stocks[554:303] Portfolio 2, Stock 2, CurrentSharePrice = 102.000000
2013-07-27 17:27:13.946 Stocks[554:303] Portfolio 2, Stock 2, NumberofShares = 462
2013-07-27 17:27:13.947 Stocks[554:303] Portfolio 2, Stock 2, ValueInDollars = 47124.000000
2013-07-27 17:27:13.947 Stocks[554:303] Porfolio 2, Value = 122567.000000