Help with Challenge


#1

Hi,

With a little help from a few posts, I’ve completed my challenge, therefore I have a question. As I didn’t want to erase the log from previous challenge, my log is printing the stocks information as well as the total value of portfolio (ch 19 challenge), the problem is I had to create a new array for that, as I couldn’t use my NSMutalbeArray.

[code]//
// Portfolio.h
// Stocks - Challenge chapter 17
//
// Created by Jorge Augusto Ferro Junior on 10/29/13.
// Copyright © 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

#import <Foundation/Foundation.h>
@class StockHolding;

@interface Portfolio : NSObject
{
NSMutableArray *portfolio;
float totalValue;
}

@property (strong) NSMutableArray *portfolio;

// Methods

  • (void)addObject: (StockHolding *)s;
  • (float)valueOfPortfolio;

@end
[/code]

[code]//
// Portfolio.m
// Stocks - Challenge chapter 17
//
// Created by Jorge Augusto Ferro Junior on 10/29/13.
// Copyright © 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

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

@implementation Portfolio

@synthesize portfolio;

// Methods

  • (void)addObject: (StockHolding *)s {
    // Is portfolio nil?
    if (!portfolio) {
    // Create the array
    portfolio = [[NSMutableArray alloc] init];
    }
    [portfolio addObject:s];
    }

  • (float)valueOfPortfolio {
    float sum = 0.0;

    for (StockHolding *a in portfolio) {
    sum += [a valueInDollars];
    }
    return sum;
    }

@end

[/code]

[code]//
// ForeignStockHolding.h
// Stocks - Challenge chapter 17
//
// Created by Jorge Augusto Ferro Junior on 10/29/13.
// Copyright © 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

#import “StockHolding.h”

@interface ForeignStockHolding : StockHolding

{
float conversionRate;
}

@property float conversionRate;

  • (float)fcost; // costinDollars * conversionRate
  • (float)fvalue; // valueinDollars * conversionRate

@end
[/code]

[code]//
// ForeignStockHolding.m
// Stocks - Challenge chapter 17
//
// Created by Jorge Augusto Ferro Junior on 10/29/13.
// Copyright © 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

  • (float)fcost
    {
    return [self costInDollars] * conversionRate;
    }

  • (float)fvalue
    {
    return [self valueInDollars] * conversionRate;
    }

@end
[/code]

[code]//
// StockHolding.h
// Stocks - Challenge chapter 17
//
// Created by Jorge Augusto Ferro Junior on 10/28/13.
// Copyright © 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

{
// Two floats - Instance variables
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
@property NSString* name;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares
    @end
    [/code]
//
//  StockHolding.m
//  Stocks - Challenge chapter 17
//
//  Created by Jorge Augusto Ferro Junior on 10/28/13.
//  Copyright (c) 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

- (float)costInDollars
{
    return [self purchaseSharePrice] * numberOfShares;
}

- (float)valueInDollars
{
    return [self currentSharePrice] * numberOfShares;
}


@end

In main.m, I have 4 arrays, that’s the problem, I want to do the same thing using only two. But if I don’t create NSMutableArray’s in main, I can’t use the methods in the other files, only the one method in portfolio.

Anyway, I’m a little confused. Any suggestions?

[code]//
// main.m
// Stocks - Challenge chapter 17
//
// Created by Jorge Augusto Ferro Junior on 10/28/13.
// Copyright © 2013 Jorge Augusto Ferro Junior. All rights reserved.
//

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

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

@autoreleasepool {
    
    // Create Stocks
    StockHolding *StockOne = [[StockHolding alloc] init];
    StockHolding *StockTwo = [[StockHolding alloc] init];
    StockHolding *StockThree = [[StockHolding alloc] init];
    ForeignStockHolding *ForeignStockOne = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *ForeignStockTwo = [[ForeignStockHolding alloc] init];
    
    // Create Portfolios - Lists
    Portfolio *StockHoldingList = [[Portfolio alloc] init];
    Portfolio *fStockHoldingList = [[Portfolio alloc] init];
    
    // Assign Values
    
    [StockOne setPurchaseSharePrice:2.30];
    [StockOne setCurrentSharePrice:4.50];
    [StockOne setNumberOfShares:40];
    [StockOne setName:@"StockOne"];
    
    [StockTwo setPurchaseSharePrice:2.30];
    [StockTwo setCurrentSharePrice:4.50];
    [StockTwo setNumberOfShares:40];
    [StockTwo setName:@"StockTwo"];
    
    [StockThree setPurchaseSharePrice:2.30];
    [StockThree setCurrentSharePrice:4.50];
    [StockThree setNumberOfShares:40];
    [StockThree setName:@"StockThree"];
    
    [ForeignStockOne setPurchaseSharePrice:2.30];
    [ForeignStockOne setCurrentSharePrice:4.50];
    [ForeignStockOne setNumberOfShares:40];
    [ForeignStockOne setConversionRate:0.94];
    [ForeignStockOne setName:@"ForeignStockOne"];
    
    [ForeignStockTwo setPurchaseSharePrice:2.30];
    [ForeignStockTwo setCurrentSharePrice:4.50];
    [ForeignStockTwo setNumberOfShares:40];
    [ForeignStockTwo setConversionRate:0.94];
    [ForeignStockTwo setName:@"ForeignStockTwo"];
    
    
    // Create empty arrays
    NSMutableArray *StockList = [NSMutableArray array];
    NSMutableArray *fStockList = [NSMutableArray array];
    
    // Add objects to the array - Local Stocks
    [StockList addObject];
    [StockList addObject];
    [StockList addObject:StockThree];
    
    // Add objects to the array - Foreign Stocks
    [fStockList addObject:ForeignStockOne];
    [fStockList addObject:ForeignStockTwo];
    
    // Print Stock numbers
    for (StockHolding *s in StockList) {
              NSLog(@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s costInDollars], [s valueInDollars]);  // or s.name in dot-notation.
    }
    for (ForeignStockHolding *s in fStockList) {
              NSLog(@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s fcost], [s fvalue]);  // or s.name in dot-notation.
    }
    
    
    // Add objects to the array - Local Stocks
    [StockHoldingList addObject];
    [StockHoldingList addObject];
    [StockHoldingList addObject:StockThree];
    
    // Add objects to the array - Foreign Stocks
    [fStockHoldingList addObject:ForeignStockOne];
    [fStockHoldingList addObject:ForeignStockTwo];
    
    // Print
    NSLog(@"$%.2f Local Portfolio", [StockHoldingList valueOfPortfolio]);
    NSLog(@"$%.2f Foreign Portfolio", [fStockHoldingList valueOfPortfolio]);
    
 
    
    return 0;
}

}

[/code]


#2

You have only two arrays, not four, in main.

An instance of Portfolio is not really an array but an object that contains an instance of an array.

[quote][code]
// Portfolio.h

#import <Foundation/Foundation.h>
@class StockHolding;

@interface Portfolio : NSObject
{
NSMutableArray *portfolio;
float totalValue;
}

@property (strong) NSMutableArray *portfolio;

// Methods

  • (void)addObject:(StockHolding *)s;
  • (float)valueOfPortfolio;

@end[/code][/quote]
Also rather than using the method name addObject: in Portfolio, better to use a more specific name such as addStockHolding:.

[code]
// Portfolio.h

#import <Foundation/Foundation.h>
@class StockHolding;

@interface Portfolio : NSObject
{
NSMutableArray *portfolio;
float totalValue;
}

@property (strong) NSMutableArray *portfolio;

// Methods

  • (void)addStockHolding:(StockHolding *)s;
  • (float)valueOfPortfolio;

@end[/code]


#3

Thanks for your reply. But that’s my question, how do I simplify my code and achieve the same output? Do I really need two arrays in main and also also the instance of an array in portfolio?

Thanks


#4

Just create an instance of Portfolio (which you have already done) and fill it with stock holdings. That’s what the challenge is asking for. There is no need to create NSMutableArray arrays.


#5

I did that. I did what the challenge was asking for. But I still don’t know how to use the same instance.

For example, the code:

for (StockHolding *s in StockList) {
                  NSLog(@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s costInDollars], [s valueInDollars]);  // or s.name in dot-notation.
        }
        for (ForeignStockHolding *s in fStockList) {
                  NSLog(@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s fcost], [s fvalue]);  // or s.name in dot-notation.
        }

this is “printing” the prices and using two NSMutableArray arrays that I’ve created. StockList and fStockList.

Suppose I don’t create these arrays in main and delete the above code. How do I achieve the same output as the above code using the instance of portfolio.

Now, using this code

// Add objects to the array - Local Stocks
        [StockHoldingList addObject];
        [StockHoldingList addObject];
        [StockHoldingList addObject:StockThree];
        
        // Add objects to the array - Foreign Stocks
        [fStockHoldingList addObject:ForeignStockOne];
        [fStockHoldingList addObject:ForeignStockTwo];
        
        // Print
        NSLog(@"$%.2f Local Portfolio", [StockHoldingList valueOfPortfolio]);
        NSLog(@"$%.2f Foreign Portfolio", [fStockHoldingList valueOfPortfolio]);
        
        return 0;

It works to return the float valueOfPortfolio. What I"m trying to do (without success), is use the same idea

NSLog(@"%.2f Local Portfolio", [StockHoldingList valueOfPortfolio]); NSLog(@"%.2f Foreign Portfolio", [fStockHoldingList valueOfPortfolio]);

to print the same output as it does when I use the arrays in main.

Maybe I’m not being clear enough. Sorry about that, I’m new to the language and I’m a little confused.

Thanks.


#6

For StockOne, the cost in dollars is 92.00 and the value in dollars is 180.00
For StockTwo, the cost in dollars is 92.00 and the value in dollars is 180.00
For StockThree, the cost in dollars is 92.00 and the value in dollars is 180.00
For ForeignStockOne, the cost in dollars is 86.48 and the value in dollars is 169.20
For ForeignStockTwo, the cost in dollars is 86.48 and the value in dollars is 169.20
$540.00 Local Portfolio
$360.00 Foreign Portfolio

This is the output using the arrays in main.

I’ve tried to create a method in portfolio

- (void)print
{
    for (Portfolio *port in portfolio) {
        NSLog(@"For %@, the value in dollars is %.2f", [port portfolio], [port valueOfPortfolio]);  // Print
        return;
}

and use it in main. But it didn’t work.


#7

[quote] for (StockHolding *s in StockList) { NSLog (@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s costInDollars], [s valueInDollars]); // or s.name in dot-notation. } for (ForeignStockHolding *s in fStockList) { NSLog (@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s fcost], [s fvalue]); // or s.name in dot-notation. }
this is “printing” the prices and using two NSMutableArray arrays that I’ve created. StockList and fStockList.

Suppose I don’t create these arrays in main and delete the above code. How do I achieve the same output as the above code using the instance of portfolio. [/quote]
That’s pretty easy to do: because you have already defined a property for the internal array in Portfolio, you can do this:

for (StockHolding *s in StockHoldingList.portfolio) { NSLog (@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s costInDollars], [s valueInDollars]); // or s.name in dot-notation. } for (ForeignStockHolding *s in fStockHoldingList.portfolio) { NSLog (@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s fcost], [s fvalue]); // or s.name in dot-notation. }
As for the print method:

[quote]- (void)print { for (Portfolio *port in portfolio) { NSLog(@"For %@, the value in dollars is %.2f", [port portfolio], [port valueOfPortfolio]); // Print return; }
[/quote]
There is a syntax error: the closing } character is missing. Also be careful about where you put the return statement though; put it outside the loop:

- (void)print
{
    for (Portfolio *port in portfolio) {
        NSLog (@"For %@, the value in dollars is %.2f", [port portfolio], [port valueOfPortfolio]);  // Print
    }
    return;
}

By the way, your communication skills are excellent.


#8

That is exactly what I was looking for. Thanks a lot for your help.