Challenge


#1

File: BNRStockHolding.h

[code]#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares

@end[/code]
File: BNRStockHolding.m

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)costInDollars
    {
    // using compiler generated accessors
    // without accessing explicitly the ivars

    // purchaseSharePrice * numberOfShares
    return [self purchaseSharePrice] * [self numberOfShares];
    }

  • (float)valueInDollars
    {
    // currentSharePrice * numberOfShares
    return [self currentSharePrice] * [self numberOfShares];
    }

@end
[/code]
File: BNRForeignStockHolding.h

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

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end[/code]
File: BNRForeignStockHolding.m

[code]#import “BNRForeignStockHolding.h”

@implementation BNRForeignStockHolding

  • (float)costInDollars
    {
    // using compiler generated accessors
    // without accessing explicitly the ivars

    // purchaseSharePrice * numberOfShares
    return (([super purchaseSharePrice] * [self conversionRate]) * [super numberOfShares]);
    }

  • (float)valueInDollars
    {
    // currentSharePrice * numberOfShares
    return (([super currentSharePrice] * [self conversionRate]) * [super numberOfShares]);
    }

@end[/code]
File: main.m

[code]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockHolding.h”

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

@autoreleasepool {
    
    // Challenge
    
    BNRStockHolding *stock1 = [[BNRStockHolding alloc] init];
    BNRStockHolding *stock2 = [[BNRStockHolding alloc] init];
    BNRStockHolding *stock3 = [[BNRStockHolding alloc] init];
    
    [stock1 setPurchaseSharePrice:2.30];
    [stock1 setCurrentSharePrice:4.50];
    [stock1 setNumberOfShares:40];
    
    [stock2 setPurchaseSharePrice:12.19];
    [stock2 setCurrentSharePrice:10.56];
    [stock2 setNumberOfShares:90];
    
    [stock3 setPurchaseSharePrice:45.10];
    [stock3 setCurrentSharePrice:49.51];
    [stock3 setNumberOfShares:210];
    
    BNRForeignStockHolding *fs1 = [[BNRForeignStockHolding alloc] init];
    
    fs1.purchaseSharePrice = 2.30;
    fs1.currentSharePrice = 4.50;
    fs1.numberOfShares = 40;
    fs1.conversionRate = 11.0;
    
    NSMutableArray *stockHolding = [[NSMutableArray alloc] initWithObjects:stock1, stock2, stock3, fs1, nil];
    
    for (BNRStockHolding *s in stockHolding) {
        NSLog(@"\n --Stock-- \nPurchase price: %.2f$ \nCurrent price: %.2f$ \nNumber of Shares: %i \nCost in dollars: %.2f$ \nValue in dollars: %.2f$", s.purchaseSharePrice, s.currentSharePrice, s.numberOfShares, s.costInDollars, s.valueInDollars);
    }
    
    // Log for conversionRate property
    NSLog(@"\nConversion rate (by local currency): %.2f$", fs1.conversionRate);
    
}
return 0;

}
[/code]
Output:
2014-08-22 01:00:49.731 Stocks[1077:303]
–Stock–
Purchase price: 2.30$
Current price: 4.50$
Number of Shares: 40
Cost in dollars: 92.00$
Value in dollars: 180.00$
2014-08-22 01:00:49.733 Stocks[1077:303]
–Stock–
Purchase price: 12.19$
Current price: 10.56$
Number of Shares: 90
Cost in dollars: 1097.10$
Value in dollars: 950.40$
2014-08-22 01:00:49.733 Stocks[1077:303]
–Stock–
Purchase price: 45.10$
Current price: 49.51$
Number of Shares: 210
Cost in dollars: 9471.00$
Value in dollars: 10397.10$
2014-08-22 01:00:49.733 Stocks[1077:303]
–Stock–
Purchase price: 2.30$
Current price: 4.50$
Number of Shares: 40
Cost in dollars: 1012.00$
Value in dollars: 1980.00$
2014-08-22 01:00:49.734 Stocks[1077:303]
Conversion rate (by local currency): 11.00$
Program ended with exit code: 0

Any suggestion will be appreciated.

Regards.,


#2

I mayyyy be wrong but I don’t think you need to actually use the super directive here:

File: BNRForeignStockHolding.m

It works but the way I understand it, it’s unnecessary since there is no ambiguity in which method you’re calling. The only purchaseSharePrice method that exists is in the superclass so you can just write it like this:

But I’m a noob so maybe specifying super is a better way to do it since it works and you can see from the code where the method originates.


#3

[quote=“mikeyshin”]I mayyyy be wrong but I don’t think you need to actually use the super directive here:

File: BNRForeignStockHolding.m

It works but the way I understand it, it’s unnecessary since there is no ambiguity in which method you’re calling. The only purchaseSharePrice method that exists is in the superclass so you can just write it like this:

But I’m a noob so maybe specifying super is a better way to do it since it works and you can see from the code where the method originates.[/quote]

@mikeyshin
Good point. Likely it’s right too.
Regards.