Holding Portfolio Challenge: Solution

This solution includes checks for currency conversion as well as description methods. It is comment heavy (I comment the hell out of the challenges for my own retention).

ZGCStockHolding.h

//
//  ZGCStockHolding.h
//  ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
//  Created by EvilKernel on 10/12/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ZGCStockHolding : NSObject

// declaring properties
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
@property (nonatomic, copy) NSString *symbol;

// declaring methods
- (void)addToStocks:(NSMutableArray *)array; //add to array via method instead of in main()
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOFShares

@end

ZGCStockHolding.m

//
//  ZGCStockHolding.m
//  ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
//  Created by EvilKernel on 10/12/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import "ZGCStockHolding.h"


@implementation ZGCStockHolding

- (void)addToStocks:(NSMutableArray *)array
{
    [array addObject:self];
}

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

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



@end

ZGCForeignStockHolding.h

//
//  ZGCForeignStockHolding.h
//  ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
//  Created by EvilKernel on 10/12/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import "ZGCStockHolding.h"

@interface ZGCForeignStockHolding : ZGCStockHolding

@property (nonatomic) float conversionRate;  // only one property in this subclass

@end

ZGCForeignStockHolding.m

//
//  ZGCForeignStockHolding.m
//  ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
//  Created by EvilKernel on 10/12/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import "ZGCForeignStockHolding.h"

@implementation ZGCForeignStockHolding

// overriding these methods to account for conversion of foreign stock in US dollars
- (float)costInDollars
{
    if (self.conversionRate) { // if conversionRate not nil, process subclass method, else revert to SuperClass method to avoid 0 values.
        float localCostInDollars = [super costInDollars];
        return localCostInDollars * self.conversionRate;
    }else{
        return [super costInDollars];
    }
}

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

- (NSString *)description //overriding descriptipn method
{
    // pull Currency locale information
    NSLocale *userLocale = [NSLocale currentLocale];
    NSString *currencyCode = [userLocale objectForKey:NSLocaleCurrencyCode];
    NSString *currencySym = [userLocale objectForKey:NSLocaleCurrencySymbol];
    float cost = [self costInDollars];
    float value = [self valueInDollars];
    if ([currencySym isEqualTo:@"$"]) {
        self.conversionRate = 0;
        return [NSString stringWithFormat:@"You paid $%.2fUSD for %d shares of %@, now valued at $%.2fUSD", cost, [self numberOfShares], [self symbol], value];
    }else{
        
    NSString *conversionRateLog = [NSString stringWithFormat:@"Your conversion rate from %@ to USD: %.2f", currencyCode, self.conversionRate];
    return [NSString stringWithFormat:@"%@, You paid $%.2fUSD for %d shares of %@, now valued at $%.2fUSD", conversionRateLog, cost, [self numberOfShares], [self symbol], value];
    }
}
@end

ZGCPortfolio.h

//
//  ZGCPortfolio.h
//  ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
//  Created by EvilKernel on 10/13/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import <Foundation/Foundation.h>

@class ZGCForeignStockHolding; // 

@interface ZGCPortfolio : NSObject


// declaring properties
@property (nonatomic, copy) NSMutableArray *holdings;

// declaring methods
- (float)totalValue;

@end

ZGCPortfolio.m

[code]
//
// ZGCPortfolio.m
// ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
// Created by EvilKernel on 10/13/14.
// Copyright © 2014 Zerogravity. All rights reserved.
//

#import “ZGCPortfolio.h”
#import “ZGCForeignStockHolding.h”

@implementation ZGCPortfolio

  • (float)totalValue
    {
    float sum = 0;
    for (ZGCForeignStockHolding *s in _holdings) {
    sum += [s valueInDollars];
    }
    return sum;
    }

  • (NSString )description //overriding description method
    {
    return [NSString stringWithFormat:@"\n\n
    *** Your Portfolio’s total worth is valued at $%.2fUSD ****\n\n", self.totalValue];

}
@end[/code]

main.m

[code]
//
// main.m
// ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
// Created by EvilKernel on 10/12/14.
// Copyright © 2014 Zerogravity. All rights reserved.
//

#import <Foundation/Foundation.h>

#import “ZGCForeignStockHolding.h”
#import “ZGCPortfolio.h”

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

@autoreleasepool {
    
    // Two US 'stock' instances
    
    ZGCStockHolding *stockUS1 = [[ZGCForeignStockHolding alloc] init];
    stockUS1.symbol = @"APPL";
    stockUS1.numberOfShares = 40;
    stockUS1.purchaseSharePrice = 2.30;
    stockUS1.currentSharePrice = 4.50;
    
    ZGCStockHolding *stockUS2 = [[ZGCForeignStockHolding alloc] init];
    stockUS2.symbol = @"BNNE";
    stockUS2.numberOfShares = 90;
    stockUS2.purchaseSharePrice = 12.19;
    stockUS2.currentSharePrice = 15.56;
    
    // Two Foreign stock instances
    
    ZGCForeignStockHolding *stockUK = [[ZGCForeignStockHolding alloc] init];
    stockUK.symbol = @"UKLN";
    stockUK.numberOfShares = 10;  // subclass can use variables from its SuperClass
    stockUK.purchaseSharePrice = 1.50;
    stockUK.currentSharePrice = 2.00;
    stockUK.conversionRate = 2.50; // new property via new subclass
    
    ZGCForeignStockHolding *stockCH = [[ZGCForeignStockHolding alloc] init];
    stockCH.symbol = @"CHPN";
    stockCH.numberOfShares = 10;  // subclass can use variables from its SuperClass
    stockCH.purchaseSharePrice = 1.50;
    stockCH.currentSharePrice = 2.00;
    stockCH.conversionRate = 4.50; // new property via new subclass
    
    // One Portfolio instance
    ZGCPortfolio *myPortfolio = [[ZGCPortfolio alloc] init];
    
    // set holdings for portfolio
    myPortfolio.holdings = [[NSMutableArray alloc] initWithObjects:stockUS1, stockUS2, stockUK, stockCH, nil];
    
    
    NSLog(@"%@", myPortfolio.holdings); //uses description method for ZCGForeignHolding object
    NSLog(@"%@", myPortfolio); //uses description method for portfolio

}
return 0;

}[/code]

[color=#008000]OUTPUT W/OUT CONVERSION*
2014-10-21 01:36:31.497 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[41770:5265223] (
“You paid $92.00USD for 40 shares of APPL, now valued at $180.00USD”,
“You paid $1097.10USD for 90 shares of BNNE, now valued at $1400.40USD”,
“You paid $37.50USD for 10 shares of UKLN, now valued at $50.00USD”,
“You paid $67.50USD for 10 shares of CHPN, now valued at $90.00USD”
)
2014-10-21 01:36:31.498 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[41770:5265223]

**** Your Portfolio’s total worth is valued at $1620.40USD ****

Program ended with exit code: 0[/color]

[color=#4000FF]***OUTPUT W/CONVERSION
2014-10-21 01:44:50.401 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[41845:5326478] (
“Your conversion rate from USD to USD: 0.00, You paid $92.00USD for 40 shares of APPL, now valued at $180.00USD”,
“Your conversion rate from USD to USD: 0.00, You paid $1097.10USD for 90 shares of BNNE, now valued at $1400.40USD”,
“Your conversion rate from EUR to USD: 2.50, You paid $37.50USD for 10 shares of UKLN, now valued at $50.00USD”,
“Your conversion rate from EUR to USD: 4.50, You paid $67.50USD for 10 shares of CHPN, now valued at $90.00USD”
)
2014-10-21 01:44:50.402 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[41845:5326478]

**** Your Portfolio’s total worth is valued at $1720.40USD ****

Program ended with exit code: 0
[/color]