Challenge solution - with a question


#1

Hi. After a while I’e understood this chapter and worked my way towards obtaining a working solution to this chapter’s challenge. My code is:

Portfolio.h

[code]//
// Portfolio.h
// Stocks
//
// Created by Mark Bridges on 10/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@interface Portfolio : NSObject

{
NSMutableArray *stockObjects;

}

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

@end
[/code]

Portfolio.m

[code]//
// Portfolio.m
// Stocks
//
// Created by Mark Bridges on 10/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@implementation Portfolio

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

-(float)valueOfPortfolio
{
// Sum up the current value of stocks
float sum = 0;
for (StockHolding *s in stockObjects) {
sum += [s valueInDollars];
}
return sum;
}

@end
[/code]

ForeignStockHolding.h

[code]//
// ForeignStockHolding.h
// Stocks
//
// Created by Mark Bridges on 08/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@interface ForeignStockHolding : StockHolding

@property float conversionRate;

@end
[/code]

ForeignStockHolding.m

[code]//
// ForeignStockHolding.m
// Stocks
//
// Created by Mark Bridges on 08/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding
@synthesize conversionRate;

-(float)costInDollars
{
return [super costInDollars] * self.conversionRate;
}

-(float)valueInDollars
{
return [super valueInDollars] * self.conversionRate;
}

@end
[/code]

StockHolding.h

[code]//
// StockHolding.h
// Stocks
//
// Created by Mark Bridges on 08/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

{
// Declare instance variables

float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;

}

// Declare properties
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

// Declare methods for the calculation

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

@end
[/code]

StockHolding.m

[code]//
// StockHolding.m
// Stocks
//
// Created by Mark Bridges on 08/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float) costInDollars;
    {
    return self.purchaseSharePrice * self.numberOfShares;
    }

  • (float) valueInDollars;
    {
    return self.currentSharePrice * self.numberOfShares;
    }

@end
[/code]

main.m

[code]//
// main.m
// Stocks
//
// Created by Mark Bridges on 08/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

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

@autoreleasepool {
    // Create an instances of StockHolding
    StockHolding *stockHolding1 = [[StockHolding alloc] init];
    StockHolding *stockHolding2 = [[StockHolding alloc] init];
    StockHolding *stockHolding3 = [[StockHolding alloc] init];
    ForeignStockHolding *fStockHolding1 = [[ForeignStockHolding alloc] init];
    
    // Create in instance of Portfolio
    Portfolio *portfolio1 = [[Portfolio alloc] init];
    
    // give values to StockHolding variables 
    [stockHolding1 setPurchaseSharePrice:2.30]; 
    [stockHolding1 setCurrentSharePrice:4.50];
    [stockHolding1 setNumberOfShares:40];

    [stockHolding2 setPurchaseSharePrice:12.19]; 
    [stockHolding2 setCurrentSharePrice:10.56];
    [stockHolding2 setNumberOfShares:90];

    [stockHolding3 setPurchaseSharePrice:45.10]; 
    [stockHolding3 setCurrentSharePrice:49.51];
    [stockHolding3 setNumberOfShares:210];
    
    [fStockHolding1 setPurchaseSharePrice:2.30];
    [fStockHolding1 setCurrentSharePrice:4.50];
    [fStockHolding1 setNumberOfShares:40];
    [fStockHolding1 setConversionRate:0.94];
    
    // Declare NSMutableArray
    NSMutableArray *stocksList = [NSMutableArray array]; 

    // Populate array with stockHolding instances
    
    [portfolio1 addStockObject:stockHolding1];
    [portfolio1 addStockObject:stockHolding2];
    [portfolio1 addStockObject:stockHolding3];
    [portfolio1 addStockObject:fStockHolding1];
    
    
    [stocksList addObject:stockHolding1];
    [stocksList addObject:stockHolding2];
    [stocksList addObject:stockHolding3];
    [stocksList addObject:fStockHolding1];
    
    // Call methods for each stockHolding and print to console
    
    for (StockHolding *s in stocksList) {

        NSLog(@"I have %d shares.I bought them for $%.2f costing $%.2f.They are currently worth $%.2f each, $%.2f in total.",s.numberOfShares, s.purchaseSharePrice, s.costInDollars, s.currentSharePrice, s.valueInDollars);
    }
    NSLog(@"Total portfolio value: $%.2f",  portfolio1.valueOfPortfolio);
    
}
return 0;

}

[/code]

However, my solution populates MSMutableArray stocksList MSMutableArray portfolio1 with essentially the same objects. I can’t seem to work out the correct code for the line in main.m

so that I don’t need to have the stockslist array at all - surely I should just be able to use the portfolio1 array for all of my stock objects. Can anyone suggest the code for this? I’ve noticed in a few others’ solutions that other people are doing the same thing.

Thanks in advance - this book is great!