Challenge Solution


#1

Wow, took me a little bit to get my head wrapped around this chapter but sure glad I took the extra time. By the tenth read through it was clear I missed a lot on the first. So if your having trouble don’t give up. When I thought I was doing things all wrong I was actually very close just had a bad token in my NSString. Anyhow this solution to the challenge problem builds off my solution in the previous chapter,http://forums.bignerdranch.com/viewtopic.php?f=150&t=4083, so I won’t include the StockHolding and ForeignStockHolding Class files. Thanks for all those that share it helps immensely.

Portfolio.h

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

@interface Portfolio : NSObject
{
NSMutableArray *portfolioStockHoldings;
}

  • (void)addPortfoliosStockHoldingsObject:(StockHolding *)a;
  • (double)valueOfStockHoldings;

@end[/code]

Portfolio.m

[code]#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio

  • (void)addPortfoliosStockHoldingsObject:(StockHolding *)a
    {
    if (!portfolioStockHoldings) {
    portfolioStockHoldings = [[NSMutableArray alloc] init];
    }
    [portfolioStockHoldings addObject:a];
    }

  • (double)valueOfStockHoldings
    {
    float sum = 0;
    for (StockHolding *a in portfolioStockHoldings) {
    sum += [a valueInDollars];
    }
    return sum;
    }

@end[/code]

main.m

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

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

@autoreleasepool {
    
    // Create instances of the class StockHolding
    StockHolding *stockHolding1 = [[StockHolding alloc] init];
    StockHolding *stockHolding2 = [[StockHolding alloc] init];
    ForeignStockHolding *stockHolding3 = [[ForeignStockHolding alloc] init];
    Portfolio *portfolio = [[Portfolio alloc] init];
    
    // Create a mutalbe Array to hold instances of the StockHolding and ForeignStockHolding Classes
    NSMutableArray *stockHoldings = [[NSMutableArray alloc] init];
    
    // Add variables and methods to each instance then add that instant to the stockHoldings array
    [stockHolding1 setNumberOfShares:25];
    [stockHolding1 setPurchaseSharePrice:2.30];
    [stockHolding1 setCurrentSharePrice:4.50];
    [stockHolding1 costInDollars];
    [stockHolding1 valueInDollars];
    [stockHolding1 addSelfToArray:stockHoldings];
    [portfolio addPortfoliosStockHoldingsObject:stockHolding1];
    
    [stockHolding2 setNumberOfShares:50];
    [stockHolding2 setPurchaseSharePrice:2.30];
    [stockHolding2 setCurrentSharePrice:4.50];
    [stockHolding2 costInDollars];
    [stockHolding2 valueInDollars];
    [stockHolding2 addSelfToArray:stockHoldings];
    [portfolio addPortfoliosStockHoldingsObject:stockHolding2];
    
    [stockHolding3 setNumberOfShares:50];
    [stockHolding3 setPurchaseSharePrice:2.30];
    [stockHolding3 setCurrentSharePrice:4.50];
    [stockHolding3 costInDollars];
    [stockHolding3 valueInDollars];
    [stockHolding3 setConversionRate:0.94];
    [stockHolding3 addSelfToArray:stockHoldings];
    [portfolio addPortfoliosStockHoldingsObject:stockHolding3];
    
    [portfolio valueOfStockHoldings];
    
    // Itterate through the stockHoldings array printing out the values of each
    for (StockHolding *n in stockHoldings) {
        NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number Of Shares: %i\n Cost In Dollars: %.2f\n Value In Dollars: %.2f", [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], [n costInDollars], [n valueInDollars]);
    }
    NSLog(@"\nThe value of the portfolio is $%.2f", [portfolio valueOfStockHoldings]);
}
return 0;

}[/code]

Output
2012-04-08 13:54:42.930 Stocks00[447:403]
Purchase Price: 2.30
Current Price: 4.50
Number Of Shares: 25
Cost In Dollars: 57.50
Value In Dollars: 112.50
2012-04-08 13:54:42.932 Stocks00[447:403]
Purchase Price: 2.30
Current Price: 4.50
Number Of Shares: 50
Cost In Dollars: 115.00
Value In Dollars: 225.00
2012-04-08 13:54:42.932 Stocks00[447:403]
Purchase Price: 2.30
Current Price: 4.50
Number Of Shares: 50
Cost In Dollars: 108.10
Value In Dollars: 211.50
2012-04-08 13:54:42.933 Stocks00[447:403]
The value of the portfolio is $549.00


#2

Nice ryan!

This was a really good challenge and also took me several readings of the chapter. I came up with a similar but slightly different and less detailed answer.

Portfolio.h

[code]#import “StockHoldings.h”

@interface Portfolio : NSObject
{
NSMutableArray *myStocks;
}

@property NSMutableArray *myStocks;

  • (void)addMyStocksObject:(StockHoldings *)a;
  • (float)valueOfStocks;

@end
[/code]

Portfolio.m

[code]#import “Portfolio.h”
#import “StockHoldings.h”

@implementation Portfolio

@synthesize myStocks;

  • (void)addMyStocksObject:(StockHoldings *)a

{

if (!myStocks){

myStocks = [[NSMutableArray alloc]init];

}
[myStocks addObject:a];
}

  • (float)valueOfStocks
    {
    float sum = 0;
    for (StockHoldings *a in myStocks){
    sum += [a currentValue];
    }
    return sum;
    }

@end
[/code]

and main.m

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

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

@autoreleasepool {
    
   // Create an instance of a portfolio
    Portfolio *portfolioA = [[Portfolio alloc]init];
    Portfolio *portfolioB = [[Portfolio alloc]init];
    
    NSMutableArray *bens = [[NSMutableArray alloc]init];
    NSMutableArray *travs = [[NSMutableArray alloc]init];
    
    [portfolioA setMyStocks:bens];
    [portfolioB setMyStocks:travs];
    
    // Create 10 stocks for Ben
    for (int i = 0; i < 10; i++){
        
        
        
        // Creat a stock
        StockHoldings *benstock = [[StockHoldings alloc]init];
        
        // Give it an interesting label
        NSString *stockLabel = [NSString stringWithFormat:@"Stock %d",i];
        [benstock setCurrentSharePrice: 100 * i];
        [benstock setNumberOfShares:10 * i];
        
        [bens addObject];
       
        
        NSLog(@"Here are Ben's stocks: %@ and it is worth %f", stockLabel, [benstock currentValue]);
        
        
        }
    
    // Create 20 stocks for Travis
    for (int i = 0; i < 20; i++){
        StockHoldings *travStock = [[StockHoldings alloc]init];
        
        NSString *stockLabel = [NSString stringWithFormat:@"Stock %d",i];
        [travStock setCurrentSharePrice:100 * i];
        [travStock setNumberOfShares:10 * i];
        
        [travs addObject:travStock];
        
        NSLog(@"Here are Travis's stocks:%@ and it is worth %f", stockLabel, [travStock currentValue]);
    }
    
    
    NSLog(@"Here is the value of portfolioA:%f", [portfolioA valueOfStocks]);
    NSLog(@"Here is the value of portfolioB:%f", [portfolioB valueOfStocks]);
    
}
return 0;

}
[/code]

Output is
2012-05-02 16:56:23.327 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 0 and it is worth 0.000000
2012-05-02 16:56:23.330 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 1 and it is worth 1000.000000
2012-05-02 16:56:23.331 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 2 and it is worth 4000.000000
2012-05-02 16:56:23.332 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 3 and it is worth 9000.000000
2012-05-02 16:56:23.333 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 4 and it is worth 16000.000000
2012-05-02 16:56:23.333 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 5 and it is worth 25000.000000
2012-05-02 16:56:23.334 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 6 and it is worth 36000.000000
2012-05-02 16:56:23.335 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 7 and it is worth 49000.000000
2012-05-02 16:56:23.336 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 8 and it is worth 64000.000000
2012-05-02 16:56:23.337 Chapter 19 Challenge[2900:403] Here are Ben’s stocks: Stock 9 and it is worth 81000.000000
2012-05-02 16:56:23.337 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 0 and it is worth 0.000000
2012-05-02 16:56:23.338 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 1 and it is worth 1000.000000
2012-05-02 16:56:23.339 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 2 and it is worth 4000.000000
2012-05-02 16:56:23.340 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 3 and it is worth 9000.000000
2012-05-02 16:56:23.341 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 4 and it is worth 16000.000000
2012-05-02 16:56:23.342 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 5 and it is worth 25000.000000
2012-05-02 16:56:23.343 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 6 and it is worth 36000.000000
2012-05-02 16:56:23.347 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 7 and it is worth 49000.000000
2012-05-02 16:56:23.348 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 8 and it is worth 64000.000000
2012-05-02 16:56:23.349 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 9 and it is worth 81000.000000
2012-05-02 16:56:23.350 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 10 and it is worth 100000.000000
2012-05-02 16:56:23.351 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 11 and it is worth 121000.000000
2012-05-02 16:56:23.351 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 12 and it is worth 144000.000000
2012-05-02 16:56:23.352 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 13 and it is worth 169000.000000
2012-05-02 16:56:23.353 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 14 and it is worth 196000.000000
2012-05-02 16:56:23.354 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 15 and it is worth 225000.000000
2012-05-02 16:56:23.355 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 16 and it is worth 256000.000000
2012-05-02 16:56:23.356 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 17 and it is worth 289000.000000
2012-05-02 16:56:23.357 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 18 and it is worth 324000.000000
2012-05-02 16:56:23.358 Chapter 19 Challenge[2900:403] Here are Travis’s stocks:Stock 19 and it is worth 361000.000000
2012-05-02 16:56:23.359 Chapter 19 Challenge[2900:403] Here is the value of portfolioA:285000.000000
2012-05-02 16:56:23.360 Chapter 19 Challenge[2900:403] Here is the value of portfolioB:2470000.000000


#3

I did almost the same thing. Except… I did this instead

In main.m
Last for loop

for(StockHolding *n in theArray) {
    NSLog(@"Print the normal stuff here...);
    [portfolio addPortfolioObject:n];
}

Saved 2 lines of code, hah. But that way I don’t have to add that line of code for every single stock I want to add.