A few questions about the Challenge


#1

This challenge was… interesting to say the least. There was a heightened level of frustration because I didn’t see the point of the challenge at first, but now the fog has lifted. I think. Just a few questions about it though.

Here’s my code.

[code]//
// main.m
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. 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 *AAPL = [[StockHolding alloc] init];
    StockHolding *GOOG = [[StockHolding alloc] init];
    StockHolding *MSFT = [[StockHolding alloc] init];
    ForeignStockHolding *DEUT = [[ForeignStockHolding alloc] init];
    
    [AAPL setSecurityName:@"AAPL"];
    [AAPL setBuyPrice:500];
    [AAPL setCurrentPrice:550];
    [AAPL setSharesBought:5];
    
    [GOOG setSecurityName:@"GOOG"];
    [GOOG setBuyPrice:450];
    [GOOG setCurrentPrice:495];
    [GOOG setSharesBought:6];
    
    [MSFT setSecurityName:@"MSFT"];
    [MSFT setBuyPrice:100];
    [MSFT setCurrentPrice:110];
    [MSFT setSharesBought:25];
    
    [DEUT setSecurityName:@"DEUT"];
    [DEUT setBuyPrice:90];
    [DEUT setCurrentPrice:100];
    [DEUT setSharesBought:100];
    [DEUT setConversionRate:1.1];
    
    Portfolio *currentHoldings = [[Portfolio alloc] init];
    
    [currentHoldings addToPortfolio:AAPL];
    [currentHoldings addToPortfolio:GOOG];
    [currentHoldings addToPortfolio:MSFT];
    [currentHoldings addToPortfolio:DEUT];
    
    [currentHoldings totalSummary];

}
return 0;

}[/code]

No questions here :slight_smile:

[code]//
// StockHolding.h
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject {
NSString *securityName;
float buyPrice;
float currentPrice;
int sharesBought;
float costBasis;
}

@property (strong) NSString *securityName;
@property float buyPrice;
@property float currentPrice;
@property int sharesBought;

  • (float)costBasis;
  • (float)presentValue;
  • (float)netGain;

@end[/code]

Question for StockHolding.h - the (strong) doesn’t appear to be necessary here. I’ve built and run the code with and without the (strong) here, and my output wasn’t affected. Is it actually necessary?

[code]//
// StockHolding.m
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. All rights reserved.
//

#import “StockHolding.h”

@implementation StockHolding

@synthesize securityName, buyPrice, currentPrice, sharesBought;

  • (float)costBasis;
    {
    return [self buyPrice] * [self sharesBought];
    }

  • (float)presentValue;
    {
    return [self currentPrice] * [self sharesBought];
    }

  • (float)netGain;
    {
    return [self presentValue] - [self costBasis];
    }

@end[/code]

No questions here :slight_smile:

[code]//
// ForeignStockHolding.h
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. All rights reserved.
//

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

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}

@property float conversionRate;

  • (float)costBasis;
  • (float)presentValue;

@end[/code]

No questions here :slight_smile:

[code]//
// ForeignStockHolding.m
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. All rights reserved.
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

  • (float)costBasis
    {
    return [super costBasis] * [self conversionRate];
    }

  • (float)presentValue
    {
    return [super presentValue] * [self conversionRate];
    }

@end[/code]

No questions here :slight_smile:

[code]//
// Portfolio.h
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. All rights reserved.
//

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

@interface Portfolio : StockHolding
{
NSMutableArray *myPortfolio;
}

@property(strong)NSMutableArray *myPortfolio;

  • (void)addToPortfolio:(StockHolding *)s;
  • (float)totalCostBasis;
  • (float)totalPresentValue;
  • (void)totalSummary;

@end[/code]

Question for Portfolio.h - why is Portfolio a subclass of StockHolding? I would’ve thought since we’re making an NSMutableArray here it’s either a subclass of NSMutableArray or NSObject. I’ve tried changing it to a subclass of either of those others, and I get errors in Portfolio.m

[code]//
// Portfolio.m
// Challenge
//
// Created by SVEARDZE on 4/30/2012.
// Copyright © 2012 SVEARDZE. All rights reserved.
//

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

@implementation Portfolio
@synthesize myPortfolio;

  • (void)addToPortfolio:(StockHolding *)s
    {
    if (!myPortfolio)
    {
    myPortfolio = [[NSMutableArray alloc] init];
    }
    [myPortfolio addObject:s];
    }

  • (float)totalCostBasis;
    {
    float cost = 0;
    for (Portfolio *p in myPortfolio) {
    cost += [p costBasis];
    }
    return cost;
    }

  • (float)totalPresentValue;
    {
    int value = 0;
    for (Portfolio *p in myPortfolio) {
    value += [p presentValue];
    }
    return value;
    }

  • (float)totalNetGain;
    {
    return [self totalPresentValue] - [self totalCostBasis];
    }

  • (void)totalSummary;
    {
    for (Portfolio *p in myPortfolio) {
    NSLog(@"\n\nSecurity: %@\nPurchase price: %.2f\nShares bought: %i\nCost basis: %.2f\nCurrent price: %.2f\nPresent value: %.2f\nNet gain/loss: %.2f\n\n", [p securityName], [p buyPrice], [p sharesBought], [p costBasis], [p currentPrice], [p presentValue], [p netGain]); } NSLog(@"Portfolio cost basis: %.2f", [self totalCostBasis]);
    NSLog(@“Portfolio total present value: %.2f", [self totalPresentValue]); NSLog(@"Portfolio total net gain/loss: %.2f”, [self totalNetGain]);
    }

@end[/code]

Like I said, I’m wondering why the Portfolio class is a subclass of the StockHolding class instead of NSMutableArray or NSObject. The only clue I have is that when I change it to one of those, I get errors in the totalCostBasis and totalPresentValue methods unable to find pointers or something like that. Could anybody help me understand why this is? (I’m the type who isn’t satisfied knowing something works properly–I like to know WHY it’s working properly :slight_smile:

Here’s my output:

012-04-30 22:33:47.689 Challenge[528:403]

Security: AAPL
Purchase price: $500.00
Shares bought: 5
Cost basis: $2500.00
Current price: $550.00
Present value: $2750.00
Net gain/loss: $250.00

2012-04-30 22:33:47.692 Challenge[528:403]

Security: GOOG
Purchase price: $450.00
Shares bought: 6
Cost basis: $2700.00
Current price: $495.00
Present value: $2970.00
Net gain/loss: $270.00

2012-04-30 22:33:47.693 Challenge[528:403]

Security: MSFT
Purchase price: $100.00
Shares bought: 25
Cost basis: $2500.00
Current price: $110.00
Present value: $2750.00
Net gain/loss: $250.00

2012-04-30 22:33:47.695 Challenge[528:403]

Security: DEUT
Purchase price: $90.00
Shares bought: 100
Cost basis: $9900.00
Current price: $100.00
Present value: $11000.00
Net gain/loss: $1100.00

2012-04-30 22:33:47.696 Challenge[528:403] Portfolio cost basis: $17600.00
2012-04-30 22:33:47.697 Challenge[528:403] Portfolio total present value: $19470.00
2012-04-30 22:33:47.697 Challenge[528:403] Portfolio total net gain/loss: $1870.00


#2

I would make Portfolio a subclass of NSObject, not Stockholding.


#3

Like I said before, my code works beautifully as-is. When I change the Portfolio class to a subclass of something other than the StockHolding class, I get three errors in the Portfolio.m file that cause the build to fail. All three errors are of the same nature. Here’s an example:

[code]// taken from Portfolio.m

1 - (float)totalCostBasis;
2 {
3 float cost = 0;
4 for (Portfolio *p in myPortfolio) {
5 cost += [p costBasis];
6 }
7 return cost;
8 }[/code]

The error happens on line 5 because Xcode says “no visible @interface for ‘Portfolio’ declares the selector ‘costBasis’”, which makes sense because I’m not trying to call a method in Portfolio, I’m actually trying to call a method from the StockHolding class. This, I presume, is why it’s safe for me to keep the Portfolio class a subclass of StockHolding. Is there a way of making Portfolio a subclass of NSObject without these errors?


#4

Wait. Never mind. It just hit me…

[code]// taken from Portfolio.m

1 - (float)totalCostBasis;
2 {
3 float cost = 0;
4 for (Portfolio *p in myPortfolio) {
5 cost += [p costBasis];
6 }
7 return cost;
8 }[/code]

By mentioning Portfolio on line 4 instead of StockHolding, I was sending the message [p costBasis] on line 5 to that instance of the Portfolio class. Now, when I mention StockHolding on line 4 instead of Portfolio, the message [p costBasis] gets sent to that instance of StockHolding class, which is what I was intending. I am now able to designate the Portfolio class a subclass of NSObject instead of StockHolding, as I always wanted to. All better!