Kickstart for the Challenge?


#1

If I have absolutely no idea on how to go about performing the chapter 19 challenge should I be starting over somewhere else in the book? Redoing a chapter perhaps? Or do you have some kind of hint, tip or trick to get me started.


#2

Well, you will create a new class (Portfolio) that will have an array of StockHolding objects. In main(), you’ll create a portfolio and a bunch of stock holdings. And you’ll put each stock holding in to the portfolio’s array. Does that help?


#3

I feel like I should be understanding this, but I’m not, very frustrating.

I don’t understand why you need a class called portfolio, couldn’t I just make another mutable array?

Either way, we need it, so when I made it and you say “It will have an array of Stockholding objects” thats a curve for me. Do I have an instance variable StockHolding *? is their a method wherein the return variable is a pointer to a stockholding object?

I feel like I’ve understood and grasped everything thus far, I just can’t seem to wrap my head around this one.

Your help is so greatly appreciated :slight_smile:

EDIT: I think I did it!

Here is my Portfolio.m file

#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio

-(void)addStockObject:(StockHolding *)s
{
if(!stocks){
stocks = [[NSMutableArray alloc] init];
}
[stocks addObject:s];
}

-(unsigned int)valueOfStocks
{
unsigned int sum = 0;

for(StockHolding *s in stocks){
    sum += [s currentSharePrice] * [s numberOfShares];
}

return sum;

}
@end

Here is my main.m

StockHolding *stock1 = [[StockHolding alloc] init];
StockHolding *stock2 = [[StockHolding alloc] init];
StockHolding *stock3 = [[StockHolding alloc] init];
ForeignStockHolding *foregin1 = [[ForeignStockHolding alloc] init];
Portfolio *myPortfolio = [[Portfolio alloc] init];

[stock1 setPurchaseSharePrice:2.30];
[stock1 setNumberOfShares:40];
[stock1 setCurrentSharePrice:4.50];

[stock2 setPurchaseSharePrice:12.19];
[stock2 setNumberOfShares:90];
[stock2 setCurrentSharePrice:10.56];

[stock3 setPurchaseSharePrice:45.10];
[stock3 setNumberOfShares:210];
[stock3 setCurrentSharePrice:49.51];

[foregin1 setPurchaseSharePrice:2.30];
[foregin1 setNumberOfShares:40];
[foregin1 setCurrentSharePrice:4.50];
[foregin1 setConversionRate:0.94];

[myPortfolio addStockObject:stock1];
[myPortfolio addStockObject:stock2];
[myPortfolio addStockObject:stock3];
[myPortfolio addStockObject];

NSLog(@"The value of all your stocks is %i", [myPortfolio valueOfStocks]);

Good?


#4

Ok, I finally got my brains around this challenge. Well, at least it works.
My code is pretty much like the one posted above (I admit, I had to peek), but I really just used it to get started. I also used quite some different variable names which got me confused, too.

I think I understood the concept, but I’m always getting confused with the * thingies.

So, for a regular variable, the * in front of a variable means, that I don’t actually know the variables value, but I know where to find it (excuse my dumb approach at describing how I understood it).

This one: “(StockHolding *)s” : means, I have an object of StockHolding and s is my arrow pointing to the object, right???

I think I’ll try to scribble my code down on paper with the single objects and pointers and such and see, if I can learn a little more.
And I hope, when I continue with the next chapter, it’ll finally sink in.