Trouble with the Challenge


#1

I’m having some issues getting the Challenge to work - Its certainly an error on my end (trying to grasp object oriented programming…)

Here is what I have.

StockHolding.h

//
//  StockHolding.h
//  Stocks
//
//  Created by David Ellinger on 2/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
    
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

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


@end

StockHolding.m

//  StockHolding.m
//  Stocks
//
//  Created by David Ellinger on 2/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice,currentSharePrice,numberOfShares;


@end

ForeignStockHolding.h

[code]//
// ForeignStockHolding.h
// Stocks
//
// Created by David Ellinger on 2/9/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}

@property float conversionRate;

@end
[/code]

ForeignStockHolding.m

//
//  ForeignStockHolding.m
//  Stocks
//
//  Created by David Ellinger on 2/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ForeignStockHolding.h"
@implementation ForeignStockHolding
@synthesize conversionRate;

- (float)costInDollars{
    float normalCID = [super costInDollars];
    return normalCID * [self conversionRate];
}

- (float)valueInDollars{
    float normalVID = [super valueInDollars];
    return normalVID * [self conversionRate];
}



@end

Last
main.m

//
//  main.m
//  Stocks
//
//  Created by David Ellinger on 2/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

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

    @autoreleasepool {
        
        StockHolding *stockHolding1 = [[StockHolding alloc] init];
        StockHolding *stockHolding2 = [[StockHolding alloc] init];
        StockHolding *stockHolding3 = [[StockHolding alloc] init];
        ForeignStockHolding *stockHolding4 = [[ForeignStockHolding alloc] init];
        
        
        [stockHolding1 setNumberOfShares:40];
        [stockHolding1 setCurrentSharePrice:4.50];
        [stockHolding1 setPurchaseSharePrice:2.30];
        
        [stockHolding2 setNumberOfShares:90];
        [stockHolding2 setCurrentSharePrice:10.56];
        [stockHolding2 setPurchaseSharePrice:2.30];
        
        [stockHolding3 setNumberOfShares:210];
        [stockHolding3 setCurrentSharePrice:49.51];
        [stockHolding3 setPurchaseSharePrice:45.10];
        
        [stockHolding4 setNumberOfShares:300];
        [stockHolding4 setCurrentSharePrice:63.51];
        [stockHolding4 setPurchaseSharePrice:56.23];
        [stockHolding4 setConversionRate:.09];
        
        
        
        
        
        
        
        NSMutableArray *stockArray = [NSArray arrayWithObjects:stockHolding1,stockHolding2,stockHolding3,stockHolding4, nil];
        NSUInteger stockCount = [stockArray count];        
        
        for(int i = 0; i < stockCount; i++){
            StockHolding *s = [stockArray objectAtIndex: i];
            NSLog(@"For %@ the Number of Shares is %d, the current share price is %f and the Purchase Share price is %f",s,[s numberOfShares],[s currentSharePrice],[s purchaseSharePrice]);
            
        }
    }
    return 0;
}

My output is

[Switching to process 3628 thread 0x0]
2012-02-09 01:13:06.487 Stocks[3628:707] For <StockHolding: 0x100114190> the Number of Shares is 40, the current share price is 4.500000 and the Purchase Share price is 2.300000
2012-02-09 01:13:06.491 Stocks[3628:707] For <StockHolding: 0x1001141e0> the Number of Shares is 90, the current share price is 10.560000 and the Purchase Share price is 2.300000
2012-02-09 01:13:06.492 Stocks[3628:707] For <StockHolding: 0x100114200> the Number of Shares is 210, the current share price is 49.509998 and the Purchase Share price is 45.099998
2012-02-09 01:13:06.494 Stocks[3628:707] For <ForeignStockHolding: 0x100114390> the Number of Shares is 300, the current share price is 63.509998 and the Purchase Share price is 56.230000

Any hints?


#2

That looks fine to me. What displeases you?


#3

I just noticed my mental error on this. My conversion rate wasn’t working correctly on my output but I mixed up some variable names >_<

Thanks though!

I’m loving the book!