Solution to Challenge 2

Please advise…

main.m

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"

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

    @autoreleasepool {
        // Create BNRStockHoldings
        BNRStockHolding *appl = [[BNRStockHolding alloc] init];
        BNRStockHolding *msft = [[BNRStockHolding alloc] init];
        BNRStockHolding *goog = [[BNRStockHolding alloc] init];
        
        // Set Stock Variables
        [appl setPurchaseSharePrice: 60.0];
        [appl setCurrentSharePrice: 99.3];
        [appl setNumberOfShares: 10];
        [appl setName: @"APPL"];
        
        [msft setPurchaseSharePrice: 30.0];
        [msft setCurrentSharePrice: 33.5];
        [msft setNumberOfShares: 12];
        [msft setName: @"MSFT"];
        
        [goog setPurchaseSharePrice: 425.8];
        [goog setCurrentSharePrice: 400.0];
        [goog setNumberOfShares: 15];
        [goog setName: @"GOOG"];
        
        // Create Stock Portfolio and add BNRStockHoldings
        NSMutableArray *stockPortfolio = [[NSMutableArray alloc] init];
        [stockPortfolio addObject: appl];
        [stockPortfolio addObject: msft];
        [stockPortfolio addObject: goog];
        
        for(BNRStockHolding *stock in stockPortfolio)
        {
            NSString *name = [stock name];
            float purchasePrice = [stock purchaseSharePrice];
            float currentPrice = [stock currentSharePrice];
            int numberOfShares = [stock numberOfShares];

            NSLog(@"%@: ", name);
            NSLog(@"Purchase price: %.2f\n", purchasePrice);
            NSLog(@"Current Price: %.2f\n", currentPrice);
            NSLog(@"Number of shares: %d\n\n", numberOfShares);
        } 
    }
    return 0;
}

BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject
{
    NSString *_name;
    float _purchaseSharePrice;
    float _currentSharePrice;
    int _numberOfShares;
}
- (NSString *)name;
- (void)setName: (NSString *)name;
- (float)purchaseSharePrice;
- (void)setPurchaseSharePrice: (float)purchaseSharePrice;
- (float)currentSharePrice;
- (void)setCurrentSharePrice: (float)currentSharePrice;
- (int)numberOfShares;
- (void)setNumberOfShares: (int)numberOfShares;
- (float)costInDollars;
- (float)valueInDollars;

@end

BNRStockHolding.m

#import "BNRStockHolding.h"

@implementation BNRStockHolding
- (NSString *)name {
    return _name;
}

- (void)setName:(NSString *)name {
    _name = name;
}
- (float)purchaseSharePrice
{
    return _purchaseSharePrice;
}

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

- (float)currentSharePrice
{
    return _currentSharePrice;
}

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

- (int)numberOfShares
{
    return _numberOfShares;
}

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

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

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

@end

Here is my Main

[code]#import <Foundation/Foundation.h>
//Import of new Class
#import “BNRStockHolding.h”

int main(int argc, const char * argv[])
{
@autoreleasepool
{
// New instance of BNRStockHolding with values set
BNRStockHolding *holdingOne = [[BNRStockHolding alloc] init];
[holdingOne setPurchaseSharePrice:2.30];
[holdingOne setCurrentSharePrice:4.50];
[holdingOne setNumberOfShares:40];
[holdingOne costInDollars];
[holdingOne valueInDollars];

    // New instance of BNRStockHolding with values set
    BNRStockHolding *holdingTwo = [[BNRStockHolding alloc] init];
    [holdingTwo setPurchaseSharePrice:12.19];
    [holdingTwo setCurrentSharePrice:10.56];
    [holdingTwo setNumberOfShares:90];
    [holdingTwo costInDollars];
    [holdingTwo valueInDollars];
    
    // New instance of BNRStockHolding with values set
    BNRStockHolding *holdingThree = [[BNRStockHolding alloc] init];
    [holdingThree setPurchaseSharePrice:45.10];
    [holdingThree setCurrentSharePrice:49.51];
    [holdingThree setNumberOfShares:210];
    [holdingThree costInDollars];
    [holdingThree valueInDollars];
    
    // Add each share holde to Mutable array
    NSMutableArray *shares = [[NSMutableArray alloc] init];
    [shares addObject:holdingOne];
    [shares addObject:holdingTwo];
    [shares addObject:holdingThree];
    
    //Loop through each index and output value in Dollars
    for(BNRStockHolding *holder in shares)
    {
        NSLog(@"Value of stock $%.2f", [holder valueInDollars]);
    }
}

return 0;

}
[/code]

This is the header

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

@interface BNRStockHolding : NSObject
{
//properties
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
}

// Setter and getters

  • (float)purchaseSharePrice;

  • (void)setPurchaseSharePrice:(float)ssp;

  • (float)currentSharePrice;

  • (void)setCurrentSharePrice:(float)scsp;

  • (int)numberOfShares;

  • (void)setNumberOfShares:(float)snos;

// Methods

  • (float)costInDollars;
  • (float)valueInDollars;

@end[/code]

This is the implementation

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

// Setters

  • (float)purchaseSharePrice
    {
    return _purchaseSharePrice;
    }

  • (float)currentSharePrice
    {
    return _currentSharePrice;
    }

  • (int)numberOfShares
    {
    return _numberOfShares;
    }

// Getters

  • (void)setPurchaseSharePrice:(float)ssp
    {
    _purchaseSharePrice = ssp;
    }

  • (void)setCurrentSharePrice:(float)scsp
    {
    _currentSharePrice = scsp;
    }

  • (void)setNumberOfShares:(float)snos
    {
    _numberOfShares = snos;
    }

// Methods

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

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

@end[/code]

I hope this helped

What in particular do you need advice for :question:, your code works.

The only thing you could do is turn this

NSString *name = [stock name];
float purchasePrice = [stock purchaseSharePrice];
float currentPrice = [stock currentSharePrice];
int numberOfShares = [stock numberOfShares];
            
NSLog(@"%@: ", name);
NSLog(@"Purchase price: %.2f\n", purchasePrice);
NSLog(@"Current Price: %.2f\n", currentPrice);
NSLog(@"Number of shares: %d\n\n", numberOfShares);

Into this

NSLog(@"%@: ", [stock name]);
NSLog(@"Purchase price: %.2f\n", [stock purchaseSharePrice]);
 NSLog(@"Current Price: %.2f\n", [stock currentSharePrice]);
NSLog(@"Number of shares: %d\n\n", [stock numberOfShares]);

Even that is preference anyway

Here is my code, which to my mind is correct - but clearly isn’t. At the bottom you can see the results…

#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

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

@autoreleasepool {
    
    BNRStockHolding *first = [[BNRStockHolding alloc] init];
    
    [first setNumberOfShares:10];
    [first setPurchaseSharePrice:1.50];
    [first setCurrentSharePrice:1.60];
     
    BNRStockHolding *second = [[BNRStockHolding alloc] init];
    
    [second setNumberOfShares:20];
    [second setPurchaseSharePrice:1.60];
    [second setCurrentSharePrice:1.50];
    
    BNRStockHolding *third = [[BNRStockHolding alloc] init];
    
    [third setNumberOfShares:1];
    [third setPurchaseSharePrice:0.32];
    [third setCurrentSharePrice:0.43];
    
    NSMutableArray *stocks = [[NSMutableArray alloc]init];
    
    [stocks addObject:first];
    [stocks addObject:second];
    [stocks addObject:third];
    
    for (BNRStockHolding *holdings in stocks)
    {
        float p = [holdings purchaseSharePrice];
        float c = [holdings costInDollars];
        
        NSLog (@"%0.2f",p);
        NSLog (@"%0.2f",c);
        
        NSLog (@"Value %0.2f\n\n", [holdings valueInDollars]);
    }
    
}
return 0;

}

Produces these results, which are clearly not what are expected !

2014-06-29 22:03:35.519 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:35.521 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:39.283 Ch 18: Challenge 1[11622:303] Value 0.43

2014-06-29 22:03:39.284 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:39.285 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:41.467 Ch 18: Challenge 1[11622:303] Value 0.43

2014-06-29 22:03:41.467 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:41.468 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:42.081 Ch 18: Challenge 1[11622:303] Value 0.43

Please put me out of my misery! Been looking at the code too long now!!