Challenge 18 Solution


#1

StockHolding.h

[code]//
// StocHolding.h
// a
//
// Created by Bernardo Mondragón Brozon on 29/07/13.
// Copyright © 2013 Bernardo Mondragón Brozon. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StocHolding : NSObject {
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
@property float purchaseSharePrice, currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars;
  • (float)valueInDollars;
    -(void)AddToArray:(NSMutableArray *)theArray;
    @end[/code]

StockHolding.m

[code]//
// StocHolding.m
// a
//
// Created by Bernardo Mondragón Brozon on 29/07/13.
// Copyright © 2013 Bernardo Mondragón Brozon. All rights reserved.
//

#import “StocHolding.h”

@implementation StocHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars {
    return purchaseSharePrice*numberOfShares;
    }
  • (float)valueInDollars {
    return currentSharePrice*numberOfShares;
    }
    -(void)AddToArray:(NSMutableArray *)theArray {
    [theArray addObject:self];
    }
    @end
    [/code]

ForeignStockHolding.h

[code]//
// ForeignStockHolding.h
// a
//
// Created by Bernardo Mondragón Brozon on 29/07/13.
// Copyright © 2013 Bernardo Mondragón Brozon. All rights reserved.
//

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

@interface ForeignStockHolding : StocHolding {
float convertionRate;
}
@property float convertionRate;
@end
[/code]

ForeighStockHolding.m

[code]//
// ForeignStockHolding.m
// a
//
// Created by Bernardo Mondragón Brozon on 29/07/13.
// Copyright © 2013 Bernardo Mondragón Brozon. All rights reserved.
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding
@synthesize convertionRate;
-(float)costInDollars {
return [super costInDollars]*convertionRate;
}
-(float)valueInDollars {
return [super valueInDollars]*convertionRate;
}
@end
[/code]

main.m

[code]//
// main.m
// a
//
// Created by Bernardo Mondragón Brozon on 29/07/13.
// Copyright © 2013 Bernardo Mondragón Brozon. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “StocHolding.h”
#import “ForeignStockHolding.h”

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

@autoreleasepool {
    NSMutableArray *theArray =[[NSMutableArray alloc]init];
    for (int i =1; i<4; i++) {
        StocHolding *stock = [[StocHolding alloc]init];
        [stock setPurchaseSharePrice:200*i];
        [stock setCurrentSharePrice:190*i];
        [stock setNumberOfShares:10+i];
        [stock AddToArray];
        ForeignStockHolding *Fstock = [[ForeignStockHolding alloc]init];
        [Fstock setConvertionRate:.9];
        [Fstock setPurchaseSharePrice:stock.purchaseSharePrice];
        [Fstock setCurrentSharePrice:stock.currentSharePrice];
        [Fstock setNumberOfShares:stock.numberOfShares];
        [Fstock AddToArray];
    }
    int o = 1;
    int stockNumber = 1;
    int FstockNumber = 1;
    for (ForeignStockHolding *sh in theArray) {
        if (o %2 == 0) {
            NSLog(@"Foreign Stock %i || Cost: %.2f Value: %.2f", FstockNumber, [sh costInDollars], [sh valueInDollars]);
            FstockNumber++;
        } else {
            NSLog(@"Local   Stock %i || Cost: %.2f Value: %.2f",stockNumber, [sh costInDollars], [sh valueInDollars]);
            stockNumber++;
        }
        o++;
    }

}
return 0;

}[/code]

This is the output:

2013-07-30 00:55:05.815 a[936:303] Local____Stock 1 || Cost: 2200.00 Value: 2090.00
2013-07-30 00:55:05.817 a[936:303] Foreign__Stock 1 || Cost: 1980.00 Value: 1881.00
2013-07-30 00:55:05.818 a[936:303] Local____Stock 2 || Cost: 4800.00 Value: 4560.00
2013-07-30 00:55:05.818 a[936:303] Foreign__Stock 2 || Cost: 4320.00 Value: 4104.00
2013-07-30 00:55:05.818 a[936:303] Local____Stock 3 || Cost: 7800.00 Value: 7410.00
2013-07-30 00:55:05.819 a[936:303] Foreign__Stock 3 || Cost: 7020.00 Value: 6669.00


#2

Hi Bernardo,
Why did you add this method Add to the Array? I don’t see how it is easier to work with than the existing instance method addObject:
Am I missing the point?


#3

[quote=“wannanerd”]Hi Bernardo,
Why did you add this method Add to the Array? I don’t see how it is easier to work with than the existing instance method addObject:
Am I missing the point?[/quote]

You are totally right.

It would have been easier to write: [theArray addObject: stock ]; and [theArray addObject: Fstock ];

Thank you !


#4

It took me too a while to figure out that I could do without the add method as shown in the example.
Next step is find a way to translate the sum method from the example to design your own method to sum up the total value of the portfolio.
That took me a while too…
Good luck!


#5

Hey Bernardo:

What’s the reason for including the loop with i? I notice that you’re adding and multiplying the value of i to the stock prices. Any particular reason? I used the values given from the book but maybe I missed something…


#6

[quote=“wannanerd”]It took me too a while to figure out that I could do without the add method as shown in the example.
Next step is find a way to translate the sum method from the example to design your own method to sum up the total value of the portfolio.
That took me a while too…
Good luck![/quote]

I have already done that. Check the next forum of chapter 19 and there is my solution. See how it fits your needs. Good luck


#7

[quote=“dpkonofa”]Hey Bernardo:

What’s the reason for including the loop with i? I notice that you’re adding and multiplying the value of i to the stock prices. Any particular reason? I used the values given from the book but maybe I missed something…[/quote]

Including the random loop with i, you can give different values to every instance variable programmatically.

Notice that I only used the accessor methods once and I have 3 instances of StockHolding class and 3 instances of ForeignStockHolding class.