My solution to challenge


#1

I know it wasn’t in the assignment, but the code as required by the assignment didn’t show who owned the stock. So I added an NSString *owner variable and a property of setOwner so the print-0ut would identify who owned the various holdings. (I hope this post comes out right, it is my first one.)

//
//  StockHolding.h
//
//  Created by Edward Metzler on 11/25/12.
//  Copyright (c) 2012 Ed Metzler. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    NSString *owner;
    float purchasePrice, currentPrice;
    int numberOfShares;
}

@property NSString *owner;
@property float purchasePrice;
@property float currentPrice;
@property int numberOfShares;

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

@end
//
//  StockHolding.m
//  AnotherTryAtStockChallenge
//
//  Created by Edward Metzler on 11/25/12.
//  Copyright (c) 2012 Ed Metzler. All rights reserved.
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize owner;
@synthesize purchasePrice;
@synthesize currentPrice;
@synthesize numberOfShares;


-(float)costInDollars
{
    return (purchasePrice * numberOfShares);
}

-(float)valueInDollars
{
    return (currentPrice * numberOfShares);
}

@end
main.m
#import <Foundation/Foundation.h>
#import "StockHolding.h"

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

    @autoreleasepool {
       
        StockHolding *oneStock = [[StockHolding alloc] init];
        StockHolding *twoStock = [[StockHolding alloc] init];
        StockHolding *threeStock = [[StockHolding alloc] init];
        
        [oneStock setOwner:@"Ed"];
        [oneStock setPurchasePrice:2.30];
        [oneStock setCurrentPrice:4.50];
        [oneStock setNumberOfShares:40];
        
        [twoStock setOwner:@"Erma"];
        [twoStock setPurchasePrice:12.19];
        [twoStock setCurrentPrice:10.56];
        [twoStock setNumberOfShares:90];
        
        [threeStock setOwner:@"Boscoe"];
        [threeStock setPurchasePrice:45.10];
        [threeStock setCurrentPrice:49.51];
        [threeStock setNumberOfShares:210];
        
        NSMutableArray *stocks = [NSMutableArray array];
        [stocks addObject];
        [stocks addObject];
        [stocks addObject:threeStock];
        
        for(StockHolding *s in stocks)
        {
            NSLog(@"Owner:%@. Purchase price was $%.2f. Current price is $%.2f. Number of shares is %d. Cost was $%.2f. Value is $%.2f.",
                  [s owner], [s purchasePrice], [s currentPrice], [s numberOfShares], [s costInDollars], [s valueInDollars]);
        }
        
        
    }
    return 0;
}

#2

Yes. Your post came out all right, code samples and all :slight_smile:
Your code is more compact than mine: I overlooked the possibility of using @property and @synthesize in the .h and .m file and just
wrote the getters and setters out myself. I will try that variation: will save me some typing and all the potential additional typos.