Chapter 22 Challenge | Class Extensions: Solution

This is my solution to the challenge

ZGCStockHolding.h

[code]//
// ZGCStockHolding.h
// ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
// Created by EvilKernel on 10/12/14.
// Copyright © 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

[/code]

ZGCStockHolding.m

[code]//
// ZGCStockHolding.m
// ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
// Created by EvilKernel on 10/12/14.
// Copyright © 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
[/code]

ZGCForeignStockHolding.h

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

#import “ZGCStockHolding.h”

@interface ZGCForeignStockHolding : ZGCStockHolding

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

@end
[/code]

ZGCForeignStockHolding.m

[code]//
// ZGCForeignStockHolding.m
// ObjC_Classes_ZGCPortfolio_Stocks_Challenge
//
// Created by EvilKernel on 10/12/14.
// Copyright © 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
    [/code]

ZGCPortfolio.h

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

#import <Foundation/Foundation.h>

@class ZGCForeignStockHolding; //

@interface ZGCPortfolio : NSObject

// declaring properties
//@property (nonatomic, copy) NSMutableArray *holdings; <–turned into class extension

@property (nonatomic, copy) NSArray *holdings;

// declaring methods

  • (void)addHoldings:(ZGCForeignStockHolding *)h;
  • (void)removeHoldings:(ZGCForeignStockHolding *)h;
  • (float)totalValue;

@end
[/code]

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”

@interface ZGCPortfolio() //—class extension - hide mutability —
{
NSMutableArray *_holdings;
}
@end

@implementation ZGCPortfolio

  • (void)setHoldings:(NSArray *)h
    {
    _holdings = [h mutableCopy];
    }

  • (NSArray *)holdings
    {
    return [_holdings copy];
    }

  • (void)addHoldings:(ZGCForeignStockHolding *)h;
    {
    if (!_holdings){ // if no aray exists yet, build it
    _holdings = [[NSMutableArray alloc] init];
    }
    [_holdings addObject:h]; //add holding to array

}

  • (void)removeHoldings:(ZGCForeignStockHolding *)h
    {
    if (self.holdings.count < 1) {
    NSLog(@“There are no more holdings left in this Portfolio”);
    }
    [_holdings removeObject:h];
    }

  • (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
    
    ZGCForeignStockHolding *stockUS1 = [[ZGCForeignStockHolding alloc] init];
    stockUS1.symbol = @"APPL";
    stockUS1.numberOfShares = 40;
    stockUS1.purchaseSharePrice = 2.30;
    stockUS1.currentSharePrice = 4.50;
    
    ZGCForeignStockHolding *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];

    [myPortfolio addHoldings];
    [myPortfolio addHoldings];
    [myPortfolio addHoldings:stockUK];
    [myPortfolio addHoldings:stockCH];
    
    NSLog(@"%@", myPortfolio.holdings); //uses description method for ZCGForeignHolding object
    NSLog(@"%@", myPortfolio); //uses description method for portfolio
    
    // remove a stock from portfolio using new method
    NSLog(@"Removing an asset...\n\n");
    sleep(3);
    
    [myPortfolio removeHoldings];
    NSLog(@"%@", myPortfolio);
   
}
return 0;

}
[/code]

******* OUTPUT ********
[color=#4000FF]2014-10-22 00:41:42.918 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[5970:2529954] (
“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-22 00:41:42.918 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[5970:2529954]

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

2014-10-22 00:41:42.918 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[5970:2529954] Removing an asset…

2014-10-22 00:41:45.919 ObjC_Classes_ZGCPortfolio_Stocks_Challenge[5970:2529954]

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

Program ended with exit code: 0[/color]