Challenge - using NSLocale Currency as conditional operator


#1

I wanted to add a way to skip US dollar conversion if local currency found to be $ (dollar). I started to play with NSLocale to pull currency code and currency symbol. Let me know what you think. there is obviously a more thorough way to do this but trying to stick with the challenge itself without diverting too much.

Main.m


//
//  main.m
//  ObjC_CLASSES_Stocks_Challenge
//
//  Created by EvilKernel on 9/25/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import <Foundation/Foundation.h>
// #import "ZGCStockHolding.h"
#import "ZGCForeignStockHolding.h"

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

    @autoreleasepool {
        
        // Three 'stock' instances
        ZGCStockHolding *stockUS1 = [[ZGCForeignStockHolding alloc] init];
        [stockUS1 setShareName:@"US1 Stock"];
        [stockUS1 setNumberOfShares:40];
        [stockUS1 setPurchaseSharePrice:2.30];
        [stockUS1 setCurrentSharePrice:4.50];

        ZGCStockHolding *stockUS2 = [[ZGCForeignStockHolding alloc] init];
        [stockUS2 setShareName:@"US2 Stock"];
        [stockUS2 setNumberOfShares:90];
        [stockUS2 setPurchaseSharePrice:12.19];
        [stockUS2 setCurrentSharePrice:10.56];
        
        // new instsance of subclass ZGCForeignStockholding
        ZGCForeignStockHolding *stockUK = [[ZGCForeignStockHolding alloc] init];
        stockUK.shareName = @"UK Stock";
        stockUK.numberOfShares = 10;  // subclass can use variables from its SuperClass
        stockUK.purchaseSharePrice = 1.50;
        stockUK.currentSharePrice = 2.00;
        stockUK.conversionRate = 1.50; // new property via new subclass
        
        ZGCForeignStockHolding *stockCH = [[ZGCForeignStockHolding alloc] init];
        stockCH.shareName = @"China Stock";
        stockCH.numberOfShares = 10;  // subclass can use variables from its SuperClass
        stockCH.purchaseSharePrice = 1.50;
        stockCH.currentSharePrice = 2.00;
        stockCH.conversionRate = 3.55; // new property via new subclass
        
        // added a method/accessor to the CLASS as experiment (addToStock), which uses 'self' to add the object to an array
        // via the method instead of explicitely in main()
        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        [stockUS1 addToStocks:stocks];
        [stockUS2 addToStocks:stocks];
        [stockUK addToStocks:stocks]; // it can use methods from SuperClass too
        [stockCH addToStocks:stocks];
        
        // pull currency locale information
        NSLocale *userLocale = [NSLocale currentLocale];
        NSString *currencyCode = [userLocale objectForKey:NSLocaleCurrencyCode];
        NSString *currencySym = [userLocale objectForKey:NSLocaleCurrencySymbol];
        
        NSLog(@"Your Currency: (%@) %@", currencySym, currencyCode);
        
        // Iterate over array
        for (ZGCForeignStockHolding *s in stocks) { // using fast enum loop for arrays
            if ([currencySym isEqualTo:@"$"]) {
                s.conversionRate = 0;
            }else{
                s.conversionRate = s.conversionRate;
                NSLog(@"Your conversion rate from %@ to USD: %.2f", currencyCode, s.conversionRate);
            }
            float cost = [s costInDollars];
            float value = [s valueInDollars];
            NSLog(@"You paid $%.2fUSD for %d shares of %@, now valued at $%.2fUSD", cost, [s numberOfShares], [s shareName], value);

            
            
        }
    }
    return 0;
}

ZCGForeignStockHolding.m

//
//  ZCGForeignStockHolding.m
//  ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge
//
//  Created by EvilKernel on 10/1/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import "ZGCForeignStockHolding.h"

@implementation ZGCForeignStockHolding

// reimplementing 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];
    }

}
@end

ZCGForeignStockHolding.h

//
//  ZCGForeignStockHolding.h
//  ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge
//
//  Created by EvilKernel on 10/1/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

ZGStockHolding.m

//
//  ZGStockHolding.m
//  ObjC_CLASSES_Stocks_Challenge
//
//  Created by EvilKernel on 9/25/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

#import "ZGCStockHolding.h" // import declarations

// implement accessors / methods

@implementation ZGCStockHolding

- (float)purchaseSharePrice
{
    return _purchaseSharePrice;
}

- (void)setPurchaseSharePrice:(float)pp
{
    _purchaseSharePrice = pp;
}

- (float)currentSharePrice
{
    return _currentSharePrice;
}

- (void)setCurrentSharePrice:(float)cp
{
    _currentSharePrice = cp;
}

- (int)numberOfShares
{
    return _numberOfShares;
}

- (void)setNumberOfShares:(int)n
{
    _numberOfShares = n;
}

- (NSString *)shareName
{
    return _shareName;
}

- (void)setShareName:(NSString *)name
{
    _shareName = name;
}

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

- (float)costInDollars
{
    return [self purchaseSharePrice] * [self numberOfShares]; // using self instead of explicit _variables
}

- (float)valueInDollars
{
    return [self currentSharePrice] * [self numberOfShares]; // using self instead of actual _variables
}

@end

ZGStockHolding.h

//
//  ZGStockHolding.h
//  ObjC_CLASSES_Stocks_Challenge
//
//  Created by EvilKernel on 9/25/14.
//  Copyright (c) 2014 Zerogravity. All rights reserved.
//

// ZGCStockHolding CLASS

#import <Foundation/Foundation.h>

@interface ZGCStockHolding : NSObject

{
    // Instance variables (using properties is the way to go instead of this shit)
    float _purchaseSharePrice;
    float _currentSharePrice;
    int _numberOfShares;
    NSString *_shareName;
}

/* Methods / accessors declarations
   setter and getter accessors */
- (float)purchaseSharePrice;
- (void)setPurchaseSharePrice:(float)pp;
- (float)currentSharePrice;
- (void)setCurrentSharePrice:(float)cp;
- (int)numberOfShares;
- (void)setNumberOfShares:(int)n;
- (NSString *)shareName;
- (void)setShareName:(NSString *)name;
- (void)addToStocks:(NSMutableArray *)array; //accesor to add to array via method instead of in main()

// getter cost and value accessors
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOFShares



@end

**** OUTPUT ****
[color=#0000FF]2014-10-04 21:27:43.370 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[4990:303] Your Currency: ($) USD
2014-10-04 21:27:43.371 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[4990:303] You paid $92.00USD for 40 shares of US1 Stock, now valued at $180.00USD
2014-10-04 21:27:43.371 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[4990:303] You paid $1097.10USD for 90 shares of US2 Stock, now valued at $950.40USD
2014-10-04 21:27:43.371 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[4990:303] You paid $15.00USD for 10 shares of UK Stock, now valued at $20.00USD
2014-10-04 21:27:43.372 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[4990:303] You paid $15.00USD for 10 shares of China Stock, now valued at $20.00USD
Program ended with exit code: 0[/color]


[color=#8040BF]2014-10-04 21:34:31.659 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] Your Currency: (¥) JPY
2014-10-04 21:34:31.660 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] Your conversion rate from JPY to USD: 0.00
2014-10-04 21:34:31.660 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] You paid $92.00USD for 40 shares of US1 Stock, now valued at $180.00USD
2014-10-04 21:34:31.661 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] Your conversion rate from JPY to USD: 0.00
2014-10-04 21:34:31.661 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] You paid $1097.10USD for 90 shares of US2 Stock, now valued at $950.40USD
2014-10-04 21:34:31.661 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] Your conversion rate from JPY to USD: 1.50
2014-10-04 21:34:31.662 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] You paid $22.50USD for 10 shares of UK Stock, now valued at $30.00USD
2014-10-04 21:34:31.662 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] Your conversion rate from JPY to USD: 3.55
2014-10-04 21:34:31.662 ObjC_CLASSES_ZGCStockHolding_Stocks_Challenge[5069:303] You paid $53.25USD for 10 shares of China Stock, now valued at $71.00USD
Program ended with exit code: 0[/color]