My Challenge Solution + NSString & NSMutableArray accessors

In addition to what was requested in the challenge, I wanted a way to reference stock name. Also wanted an addToArray accessor as illustrated in the chapter. It took a bit of fiddling with the syntax due to unfamiliarity with the use of non primitive types declarations but the compiler helped. it seems to work… :slight_smile:

Declarations .h

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

// ZGStockHolding CLASS

#import <Foundation/Foundation.h>

@interface ZGStockHolding : NSObject
{
    // Instance variables
    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

implementation .m

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

#import "ZGStockHolding.h" // import declarations

// implement accessors / methods

@implementation ZGStockHolding

- (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

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 "ZGStockHolding.h"

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

    @autoreleasepool {
        
        // Three 'stock' instances
        ZGStockHolding *stockA = [[ZGStockHolding alloc] init];
        [stockA setShareName:@"stockA"];
        [stockA setNumberOfShares:40];
        [stockA setPurchaseSharePrice:2.30];
        [stockA setCurrentSharePrice:4.50];

        ZGStockHolding *stockB = [[ZGStockHolding alloc] init];
        [stockB setShareName:@"stockB"];
        [stockB setNumberOfShares:90];
        [stockB setPurchaseSharePrice:12.19];
        [stockB setCurrentSharePrice:10.56];
        
        ZGStockHolding *stockC = [[ZGStockHolding alloc] init];
        [stockC setShareName:@"stockC"];
        [stockC setNumberOfShares:210];
        [stockC setPurchaseSharePrice:45.10];
        [stockC setCurrentSharePrice:49.51];
        
        
        // 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];
        [stockA addToStocks:stocks];
        [stockB addToStocks:stocks];
        [stockC addToStocks:stocks];
        
        // Populating array
        // NSMutableArray *stocks = [NSMutableArray array];
        // [stocks addObject:stockA];
        // [stocks addObject:stockB];
        // [stocks addObject:stockC];
        
        // Iterate over array
        for (ZGStockHolding *s in stocks) { // using fast enum loop for arrays
            float cost = [s costInDollars];
            float value = [s valueInDollars];
            NSLog(@"You paid %.2f for %d shares of %@, now valued at %.2f", cost, [s numberOfShares], [s shareName], value);

        }
        
        
    }
    return 0;
}