Challenge - Can someone check me?


#1

This challenge really kicked my butt. I think I’ve got it, but can someone check my solution to see if I’m right? I have a little redundant code in the Portfolio.m file so I could gut check what was going on.

[code]//
// Portfolio.h
// Stocks
//
// Created by James Burns on 1/23/12.
// Copyright © 2012 James Burns [design]. All rights reserved.
//

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

@interface Portfolio : StockHolding
{
NSMutableArray *myPortfolio;
}

@property (strong) NSMutableArray *myPortfolio;

  • (float)calcValue;
    -(void)addStockToPortfolio:(StockHolding *)myObj;

@end
[/code]

and the implementation:

[code]//
// Portfolio.m
// Stocks
//
// Created by James Burns on 1/23/12.
// Copyright © 2012 James Burns [design]. All rights reserved.
//

#import “Portfolio.h”

@implementation Portfolio:StockHolding

@synthesize myPortfolio;

-(void)addStockToPortfolio:(StockHolding *)myObj
{
if (!myPortfolio) {
myPortfolio=[[NSMutableArray alloc]init];
}
[myPortfolio addObject:myObj];
}

  • (float)calcValue
    {
    float myValue=0;
    float addedValue=0;

      for (StockHolding *x in myPortfolio) {
          
      //just doing this so I can track addition    
      addedValue=(x.numberOfShares * x.valueInDollars)-(x.numberOfShares * x.currentSharePrice);
          NSLog(@"Added value is: $%f",addedValue);
         
          //increment myValue by addedValue
          myValue+=addedValue;
          NSLog(@"My value is now: $%f",myValue);     
    

}
return myValue;
}

@end
[/code]

… and my main.m

[code]//
// main.m
// PortfolioStocks
//
// Created by James Burns on 1/24/12.
// Copyright © 2012 James Burns [design]. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHolding.h”
#import "Portfolio.h"
int main (int argc, const char * argv[])
{

@autoreleasepool {
    StockHolding *stock01=[[StockHolding alloc]init];
    StockHolding *stock02=[[StockHolding alloc]init];
    StockHolding *stock03=[[StockHolding alloc]init];
    ForeignStockHolding *stock04=[[ForeignStockHolding alloc]init];
    ForeignStockHolding *stock05=[[ForeignStockHolding alloc]init];
    
    Portfolio *portfolio =[[Portfolio alloc]init];
    
    stock01.purchaseSharePrice = 17.50;
    stock01.currentSharePrice =415.90;
    stock01.numberOfShares=28;
    [portfolio addStockToPortfolio:stock01];
    
    stock02.purchaseSharePrice =1.34;
    stock02.currentSharePrice=.94;
    stock02.numberOfShares=58;
    [portfolio addStockToPortfolio:stock02];
    
    stock03.purchaseSharePrice =2.44;
    stock03.currentSharePrice=1.94;
    stock03.numberOfShares=138;
    [portfolio addStockToPortfolio:stock03];
    
    stock04.purchaseSharePrice =2.00;
    stock04.currentSharePrice=3.00;
    stock04.numberOfShares=200;
    stock04.conversionRate=1.75;
    [portfolio addStockToPortfolio:stock04];

    stock05.purchaseSharePrice =1.00;
    stock05.currentSharePrice=2.00;
    stock05.numberOfShares=250;
    stock05.conversionRate=1.29;
    [portfolio addStockToPortfolio:stock05];

    NSLog(@"The value of the portfolio is $%f.",[portfolio calcValue]);

}
return 0;

}

[/code]

Thanks in advance.


#2

Hi James, I think your code is ok. The only difference with my code is that I calculated the current value of the stocks in main. That was already the case, so I did not change that. Your solution is better in the way that your portfolio.m is less dependent of the results from main. Here is my code:

//
//  Portfolio.h
//  Stocks
//
//  Created by Lucas Uylings on 13-02-12.
//  Copyright (c) 2012 Lucas Information Services. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "stockHolding.h"

@interface Portfolio : stockHolding
{
    int portfolioID;
    NSMutableArray *stockHoldings;
}

@property int portfolioID;

- (void) addStockHoldingsObject: (stockHolding *)a;
- (float) totalValueInDollars;

@end
//
//  Portfolio.m
//  Stocks
//
//  Created by Lucas Uylings on 13-02-12.
//  Copyright (c) 2012 Lucas Information Services. All rights reserved.
//

#import "Portfolio.h"

@implementation Portfolio

@synthesize portfolioID;

- (void) addStockHoldingsObject:(stockHolding *)a
{
    if (!stockHoldings)
    {
        stockHoldings=[[NSMutableArray alloc] init];
    }
    [stockHoldings addObject:a];
}

- (float) totalValueInDollars
{
    float totaal=0.0;
    for (stockHolding *a in stockHoldings)
    {
        totaal+= [a valueInDollars];
    }
    return totaal;
}
@end
//
//  main.m
//  Stocks
//
//  Created by Lucas Uylings on 09-02-12.
//  Copyright (c) 2012 Lucas Information Services. 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 array for portfolio's:
        NSMutableArray *portfolios = [NSMutableArray array];
        
        // Portfolio-object:
        Portfolio *portfolio1=[[Portfolio alloc] init];
        
        // Give the portfolio a number:
        [portfolio1 setPortfolioID:100];
         
        //Add the portfolio to the array:
        [portfolios addObject:portfolio1];
                
        // Create array for the Stocks:
        NSMutableArray *stocks = [NSMutableArray array];
        
        // Instantiate three objects:
        
        stockHolding *stock1 = [[stockHolding alloc] init];
        stockHolding *stock2 = [[stockHolding alloc] init];
        ForeignStockHolding *stock3 = [[ForeignStockHolding alloc] init];
        
        // Add some values to the stocks:
        
        [stock1 setPurchaseSharePrice:2.30];
        [stock1 setCurrentSharePrice:4.50];
        [stock1 setNumberOfShares:40];
        
        // Add stocks to portfolio:
        [portfolio1 addStockHoldingsObject:stock1];
                
        [stock2 setPurchaseSharePrice:12.19];
        [stock2 setCurrentSharePrice:10.56];
        [stock2 setNumberOfShares:90];
        
        // Add stocks to portfolio:
        [portfolio1 addStockHoldingsObject:stock2];
        
        [stock3 setPurchaseSharePrice:45.10];
        [stock3 setCurrentSharePrice:49.51];
        [stock3 setNumberOfShares:210];
        [stock3 setConversionRate:0.30];
        
        // Add stocks to portfolio:
        [portfolio1 addStockHoldingsObject:stock3];
        
        [stocks addObject:stock1];
        [stocks addObject:stock2];
        [stocks addObject:stock3];
                    
        for (stockHolding *n in stocks)
        {
            float purchasedValue=[n costInDollars];
            float currentValue=[n valueInDollars];
             
            NSLog(@"%@: Stocks bought for $%0.2f per share, current value $%0.2f per share, number of shares %i, total acquired for: $%0.2f, total current value: $%0.2f",n , [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], purchasedValue, currentValue);
        }
        float totalPortfolio=[portfolio1 totalValueInDollars];
        NSLog(@"Current value of all the stocks in portfolio #%i: $%0.2f", [portfolio1 portfolioID], totalPortfolio);
    }
    return 0;
}