My attempt at the Challenge


#1

I tried to spruce it up a bit… Not sure if I passed my string for the stock symbol name correctly, Any input on how to do that properly? It compiled so I assume it’s acceptable.

main.m:

#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        //Init 4 Stockholdings with our symnbols.
        StockHolding *msft = [[StockHolding alloc] init];
        StockHolding *aapl = [[StockHolding alloc] init];
        StockHolding *ubnt = [[StockHolding alloc] init];
        StockHolding *bac  = [[StockHolding alloc] init];

        //populate stuff.
        
        [msft setSymbolOfStock:"msft"];
        [msft setPurchaseSharePrice:6.25];
        [msft setCurrentSharePrice:31.99];
        [msft setNumberOfShares:25];


        [aapl setSymbolOfStock:"aapl"];        
        [aapl setPurchaseSharePrice:377.48];
        [aapl setCurrentSharePrice:545.17];
        [aapl setNumberOfShares:30];


        
        [ubnt setSymbolOfStock:"ubnt"];
        [ubnt setPurchaseSharePrice:17.07];
        [ubnt setCurrentSharePrice:25.50];
        [ubnt setNumberOfShares:200];

        
        [bac setSymbolOfStock:"bac"];
        [bac setPurchaseSharePrice:6.98];
        [bac setCurrentSharePrice:8.05];
        [bac setNumberOfShares:2];

        
    
        NSArray *stockList = [NSArray arrayWithObjects:msft,aapl,ubnt,bac, nil];
        
        for (StockHolding *s in stockList)
        {
            NSLog(@"\n Data for Stock Symbol: %s\n Number of shares: %.0f\n Current Value: $%.2f\n Purchase Cost: $%.2f\n Change in value $%.2f\n", [s symbolOfStock], [s numberOfShares], [s valueInDollars], [s costInDollars], [s changeInValue]);
        }

    return 0;
}
}

StockHolding.h:

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    float numberOfShares;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property float numberOfShares;
@property char * symbolOfStock;

- (float)costInDollars;
- (float)valueInDollars;
- (float)changeInValue;

@end

StockHolding.m

#import "StockHolding.h"

@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares, symbolOfStock;

-(float)costInDollars  
{
    return [self purchaseSharePrice] * [self numberOfShares];
}
-(float)valueInDollars
{
    return [self currentSharePrice] * [self numberOfShares];
}
-(float)changeInValue
{
    return [self valueInDollars] - [self costInDollars];
}
@end

And the output:

2012-03-11 21:51:50.709 Stocks[15089:707] 
 Data for Stock Symbol: msft
 Number of shares: 25
 Current Value: $799.75
 Purchase Cost: $156.25
 Change in value $643.50
2012-03-11 21:51:50.711 Stocks[15089:707] 
 Data for Stock Symbol: aapl
 Number of shares: 30
 Current Value: $16355.10
 Purchase Cost: $11324.40
 Change in value $5030.70
2012-03-11 21:51:50.712 Stocks[15089:707] 
 Data for Stock Symbol: ubnt
 Number of shares: 200
 Current Value: $5100.00
 Purchase Cost: $3414.00
 Change in value $1686.00
2012-03-11 21:51:50.712 Stocks[15089:707] 
 Data for Stock Symbol: bac
 Number of shares: 2
 Current Value: $16.10
 Purchase Cost: $13.96
 Change in value $2.14

#2

Nice !

I found in your code how to give a name to the different stockholders
@property char * symbolOfStock;

I was confused with the *
It was first used in page 41, but the objective-C strings are really in chapter 34 … I guess that"s why it’s not asked for in the challenge …

Good job anyway
Fred


#3

Finally, I think it’s better if possible to use NSString rather than char which is more of a C function :

// StockHolding.h
// Stocks

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

{
// Instance variables
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
[color=#FF4000] NSString *label;[/color]
}

// Accessor methods for the instance variables
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
[color=#FF4000]@property (strong) NSString *label;[/color]

// Calculation methods

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares

@end