Challenge is Challenging


#1

Here is my attempt, please don’t all rush and give me the answer. I want to understand how I need to be thinking to achieve my goal as this clearly does’t cut it.

#import <Foundation/Foundation.h>
#import "StockHolding.h"

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        // Create an instance of StockHolding
        StockHolding *stockholding = [[StockHolding alloc] init];
        
        // Create an empty array
        NSMutableArray *stockList = [NSMutableArray array];
        
        // Give the instance variables interesting values
        [stockholding setPurchaseSharePrice:2.30];
        [stockholding setCurrentSharePrice:4.50];
        [stockholding setNumberOfShares:40];
        
        [stockList addObject:stockholding];
                
        // Give the instance variables interesting values
        [stockholding setPurchaseSharePrice:12.19];
        [stockholding setCurrentSharePrice:10.56];
        [stockholding setNumberOfShares:90];
        
        [stockList addObject:stockholding];
        
        // Give the instance variables interesting values
        [stockholding setPurchaseSharePrice:45.10];
        [stockholding setCurrentSharePrice:49.51];
        [stockholding setNumberOfShares:210];
        
        [stockList addObject:stockholding];
        
        // Call the stockholding method
        float vid = [stockholding valueInDollars];
        float cid = [stockholding costInDollars];
        
        NSLog(@"stockholding Value %f cost of %f", vid,cid);
        for (NSString *s in stockList) {
            NSLog(@"Here is the list: %@", s);
        }
        
    }
    return 0;

#2

After reviewing another post I realise the error of my ways, I didn’t instantiate the class 3 times and I accessed the array incorrectly.

Could someone please explain in plain english what this part is telling me especially the pointer to n. Is that where each instance of the class StockHolding is placed in memory?:

// Call the stockholding methods
        
        for(StockHolding *n in stockList)
        {
            //Call the methods 
            float cost = [n costInDollars];
            float value = [n valueInDollars];
            NSLog(@"Bought stock for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
                  [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
        }

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

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

@autoreleasepool {
    
    // Create 3 instances of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    
    // Create an empty array
    NSMutableArray *stockList = [NSMutableArray array];
    
    // Give the instance variables interesting values
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    [stockList addObject:stockholding1];
            
    // Give the instance variables interesting values
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    [stockList addObject:stockholding2];
    
    // Give the instance variables interesting values
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    [stockList addObject:stockholding3];
    
    // Call the stockholding methods
    
    for(StockHolding *n in stockList)
    {
        //Call the methods 
        float cost = [n costInDollars];
        float value = [n valueInDollars];
        NSLog(@"Bought stock for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
              [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
    }
}
return 0;

}[/code]


#3

I wanted to add this also as it uses arrayWithObjects instead.

Is there a way of looking up the objects rather than explicitly writing each out in this part?

// Create an array with objects NSMutableArray *stockList = [NSMutableArray arrayWithObjects:stockholding1, stockholding2, stockholding3, nil];

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

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

@autoreleasepool {
    
    // Create 3 instances of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    
    // Create an empty array
    //NSMutableArray *stockList = [NSMutableArray array];
    
    // Give the instance variables interesting values
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    //[stockList addObject:stockholding1];
            
    // Give the instance variables interesting values
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    //[stockList addObject:stockholding2];
    
    // Give the instance variables interesting values
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    //[stockList addObject:stockholding3];
    
    // Create an array with objects
    NSMutableArray *stockList = [NSMutableArray arrayWithObjects:stockholding1, stockholding2, stockholding3, nil];

    // Call the stockholding methods
    
    for(StockHolding *n in stockList)
    {
        //Call the methods 
        float cost = [n costInDollars];
        float value = [n valueInDollars];
        NSLog(@"Bought stock for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
              [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
    }
}
return 0;

}[/code]


#4

I’m sorry; I know you really want this answered, but I don’t understand the question.


#5

Hi Aaron, I’m trying to understand *n in relation to the stockHolding. Is *n a pointer to each address of a stockHolding in the loop?

I’ve just completed and posted Chapter 19 challenge, it took while for it all to sink in.


#6

Yes, n is a variable that can hold the address where an instance of StockHolding lives. We say “n is a pointer to a StockHolding”.

In the loop n is assigned to point to each StockHolding in turn, until it has gone through the entire array, executing the block for each one.