Problem with Challenge


#1

[code]// StockHolding.h

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
NSString *stockName;
}

@property NSString *stockName;
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars;
  • (float)valueInDollars;

@end
[/code]

//  StockHolding.m

#import "StockHolding.h"

@implementation StockHolding

@synthesize currentSharePrice, purchaseSharePrice, numberOfShares, stockName;

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

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

- (NSString *)description
{
    return [NSString stringWithFormat:@"<Stock %@: $%f>", [self stockName], [self valueInDollars]];
}

@end
//  Portfolio.h

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

@interface Portfolio : NSObject
{
    NSMutableArray *stoqs;
    int portfolioID;
}

@property int portfolioID;

- (void)addStockHoldingObject:(StockHolding *)s;
- (float)valueOfPortfolio;

@end

[code]// Portfolio.m

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

@implementation Portfolio

@synthesize portfolioID;

  • (void)addStockHoldingObject:(StockHolding *)s
    {
    if (!stoqs)
    {
    NSMutableArray *stoqs = [[NSMutableArray alloc] init];
    }

    [stoqs addObject:s];
    }

  • (float)valueOfPortfolio
    {
    float sum = 0.0;
    for (StockHolding *s in stoqs)
    {
    sum += [s valueInDollars];
    NSLog(@“I have summed myself”);
    }

    return sum;
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<Portfolio %d: $%f in stocks>", [self portfolioID], [self valueOfPortfolio]];
    }

@end
[/code]

//  main.m


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

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

    @autoreleasepool {
        NSMutableArray *portfolios = [[NSMutableArray alloc] init];
        
        for (int j = 1; j < 11; j++)
        {
            Portfolio *portfolio = [[Portfolio alloc] init];
            [portfolio setPortfolioID:j];
            
            [portfolios addObject:portfolio];
        }
        
        
            
        for (int i = 0; i < 10; i++)
        {
            StockHolding *stoq = [[StockHolding alloc] init];
            [stoq setPurchaseSharePrice:100*i];
            [stoq setCurrentSharePrice:200*i];
            [stoq setNumberOfShares:10*i];
            NSString *stoqName = [NSString stringWithFormat:@"Stock %d", i];
            [stoq setStockName];
            
            NSLog(@"Stock: %@", stoq);
            
            NSUInteger randomIndex = random() % [portfolios count];
            
//            NSLog(@"Random Number: %d", randomIndex);
            
            Portfolio *randomPortfolio = [portfolios objectAtIndex:randomIndex];
            [randomPortfolio addStockHoldingObject:stoq];
        }
        
        NSLog(@"Portfolios: %@", portfolios);
        
    }
    return 0;
}

The output I get when I run this code in my project is as follows:

2013-07-05 14:24:08.294 Stoqs[789:303] Stock: <Stock Stock 0: $0.000000>
2013-07-05 14:24:08.296 Stoqs[789:303] Stock: <Stock Stock 1: $2000.000000>
2013-07-05 14:24:08.297 Stoqs[789:303] Stock: <Stock Stock 2: $8000.000000>
2013-07-05 14:24:08.297 Stoqs[789:303] Stock: <Stock Stock 3: $18000.000000>
2013-07-05 14:24:08.297 Stoqs[789:303] Stock: <Stock Stock 4: $32000.000000>
2013-07-05 14:24:08.298 Stoqs[789:303] Stock: <Stock Stock 5: $50000.000000>
2013-07-05 14:24:08.298 Stoqs[789:303] Stock: <Stock Stock 6: $72000.000000>
2013-07-05 14:24:08.299 Stoqs[789:303] Stock: <Stock Stock 7: $98000.000000>
2013-07-05 14:24:08.299 Stoqs[789:303] Stock: <Stock Stock 8: $128000.000000>
2013-07-05 14:24:08.299 Stoqs[789:303] Stock: <Stock Stock 9: $162000.000000>
2013-07-05 14:24:08.300 Stoqs[789:303] Portfolios: (
"<Portfolio 1: $0.000000 in stocks>",
"<Portfolio 2: $0.000000 in stocks>",
"<Portfolio 3: $0.000000 in stocks>",
"<Portfolio 4: $0.000000 in stocks>",
"<Portfolio 5: $0.000000 in stocks>",
"<Portfolio 6: $0.000000 in stocks>",
"<Portfolio 7: $0.000000 in stocks>",
"<Portfolio 8: $0.000000 in stocks>",
"<Portfolio 9: $0.000000 in stocks>",
"<Portfolio 10: $0.000000 in stocks>"
)

All of the amounts show up as $0.000000 and I can’t seem to figure out why.

I’m getting one warning in my project which is in Protfolio.m and is from this bit of code:

[code]- (void)addStockHoldingObject:(StockHolding *)s
{
if (!stoqs)
{
NSMutableArray *stoqs = [[NSMutableArray alloc] init];
}

[stoqs addObject:s];

}[/code]

The warning says that ‘stoqs’ is an unused variable.

Any ideas where I’m going wrong?


#2

Please ignore this. Found my mistake in the Portfolio.m file!

All is well.


#3

So what was your mistake? I’m getting the same output.